Find the Seed


Problem Statement :


A company needs random numbers for its operation. N random numbers have been generated using  N numbers as seeds and the following recurrence formula:

The numbers used as seeds are F(N-1),F(N-2),...,F(1),F(0). F(K) is the Kth term of the recurrence.

Due to a failure on the servers, the company lost its seed numbers. Now they just have the recurrence formula and the previously generated N random numbers.

The company wants to recover the numbers used as seeds, so they have hired you for doing this task.

Input Format

The first line contains two space-separated integers, N and K, respectively.
The second line contains the space-separated integers describing   F(K),F(K-1),...,F(K-N+2),F(K-N+1). F(K)(all these numbers are non-negative integers < 10^9).
The third line contains the space-separated coefficients of the recurrence formula, C(1),C(2),...,C(N-1),C(N). All of these coefficients are positive integers <10^9.

Constraints

1 <= N <= 50
1 <= K <=10^9
0 <= K-N+1



Solution :



title-img


                            Solution in C :

In C++ :





#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;

ll MOD = 1000000007;
ll powe(ll a, ll b) {
    if (b == 0) return 1;
    if (b % 2 == 0) return powe( (a*a)%MOD, b/2);
    return (a*powe(a, b-1)) % MOD;
}

ll inv(ll a) {
    return powe(a, MOD-2);
}

struct matrix {
    vector<vector<ll>> M;
    matrix I() const {
        matrix ans;
        ans.M = M;
        for (int i = 0; i < M.size(); ++i)
            for (int j = 0; j < M.size(); ++j)
            ans.M[i][j] = !!(i==j);
        return ans;
    }
    matrix operator*(const matrix& rhs) const {
        matrix ans;
        ans.M = M;
        int N = M.size();
        for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) {
            ans.M[i][j] = 0;
            for (int k = 0; k < N; ++k)
                ans.M[i][j] = (ans.M[i][j] + M[i][k]*rhs.M[k][j]) % MOD;
        }
        return ans;
    }
};

matrix powm(const matrix& M, int b) {
    if (b == 0) return M.I();
    if (b % 2 == 0) return powm( M*M, b/2);
    return M*powm(M, b-1);
}

int main() {
    ll N, K;
    cin >> N >> K;
    vector<ll> F(N);
    for (ll &x : F) cin >> x;
    vector<ll> C(N);
    for (ll &x : C) cin >> x;
    matrix M;
    M.M = vector<vector<ll>>(N, vector<ll>(N));
    for (int i = 0; i < N-1; ++i)
        for (int j = 0; j < N; ++j)
            M.M[i][j] = !!((i+1) == j);
    ll cinv = inv(C.back());
    M.M[N-1][0] = cinv;
    for (int j = 1; j < N; ++j)
        M.M[N-1][j] = ((MOD - C[j-1])*cinv)%MOD;
    auto M2 = powm(M, K-N+1);
    vector<ll> ans(N);
    for (int i = 0; i < N; ++i) {
        ans[i] = 0;
        for (int j = 0; j < N; ++j)
            ans[i] = (ans[i] + F[j]*M2.M[i][j]) % MOD;
    }
    for (int i = 0; i < N; ++i) {
        if (i) printf(" ");
        printf("%lld", ans[i]);
    }
    printf("\n");
    return 0;
}








In Java :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static final int Q = 1000000007;

    /** Assume 1 <= a < M and (a, M) = 1.  Return b such that 1 <= b < M and a*b = 1(mod M). */
    static int inv(int a, int M) {
        //assert a >= 1 && a < M;
        int t = 0;
        int r = M;
        int newt = 1;
        int newr = (int) a;
        while (newr != 0) {
            int q = r / newr;
            int temp = t - q * newt;
            t = newt;
            newt = temp;
            temp = r - q * newr;
            r = newr;
            newr = temp;
        }
        int result = (t < 0) ? t + M : t;
        //assert (a * result) % M == 1 && result >= 1 && result < M;
        return result;
    }

    static long[][] multiply(long[][] m1, long[][] m2) {
        int N = m1.length;
        long[][] m3 = new long[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                for (int k = 0; k < N; k++) {
                    m3[i][j] = (m3[i][j] + ((m1[i][k] * m2[k][j]) % Q)) % Q;
                }
            }
        }
        return m3;
    }

    /** Assume b >= 1 and each a[i][j] * (Q - 1) is in long range. */
    static long[][] pow(long[][] a, long b) {
        if (b == 1L) {
            return a;
        }
        long[][] half = pow(a, b / 2);
        long[][] almost = multiply(half, half);
        return (b % 2 == 0) ? almost : multiply(almost, a);
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        long k = in.nextLong();
        long[] f = new long[n];
        int i;
        for (i = 0; i < n; i++) {
            f[i] = in.nextLong();
        }
        int[] c = new int[n];
        for (i = 0; i < n; i++) {
            c[i] = in.nextInt();
        }

        StringBuilder builder = new StringBuilder();
        if (k - n + 1 == 0L) {
            for (i = 0; i < n; i++) {
                if (i != 0) {
                    builder.append(' ');
                }
                builder.append(f[i]);
            }
        }
        else {
            long[][] mat = new long[n][n];
            for (i = 0; i < n - 1; i++) {
                mat[i + 1][i] = 1L;
            }
            mat[0][n - 1] = (long) inv(c[n - 1], Q);
            for (i = 1; i < n; i++) {
                mat[i][n - 1] = (Q - ((c[i - 1] * mat[0][n - 1]) % Q)) % Q;
            }
            long[][] power = pow(mat, k - n + 1);
            int j;
            long a;
            for (i = 0; i < n; i++) {
                a = 0L;
                for (j = 0; j < n; j++) {
                    a = (a + f[j] * power[j][i]) % Q;
                }
                if (i != 0) {
                    builder.append(' ');
                }
                builder.append(a);
            }
        }
        System.out.println(builder);
    }
}








In C :





#include <stdio.h>
#include <stdlib.h>
#define MOD 1000000007
long long modInverse(long long a,long long mod);
void one(long long*a,int SIZE);
void mul(long long*a,long long*b,int SIZE);
void powm(long long*a,int n,long long*res,int SIZE);
int F[50],C[50];
long long a[50][50],ans[50][50],A[50];

int main(){
  int N,K,i,j;
  scanf("%d%d",&N,&K);
  for(i=0;i<N;i++)
    scanf("%d",F+i);
  for(i=0;i<N;i++)
    scanf("%d",C+i);
  for(i=0;i<N-1;i++)
    for(j=0;j<N;j++)
      if(i==j-1)
        a[i][j]=1;
      else
        a[i][j]=0;
  a[N-1][0]=modInverse(C[N-1],MOD);
  for(i=1;i<N;i++)
    a[N-1][i]=(MOD-C[i-1])*a[N-1][0]%MOD;
  powm(&a[0][0],K-N+1,&ans[0][0],50);
  for(i=0;i<N;i++)
    for(j=0,A[i]=0;j<N;j++)
      A[i]=(A[i]+F[j]*ans[i][j])%MOD;
  for(i=0;i<N;i++)
    printf("%lld ",A[i]);
  return 0;
}
long long modInverse(long long a,long long mod){
	long long b0 = mod, t, q;
	long long x0 = 0, x1 = 1;
	while (a > 1) {
		q = a / mod;
		t = mod; mod = a % mod; a = t;
		t = x0; x0 = x1 - q * x0; x1 = t;
	}
	if (x1 < 0) x1 += b0;
	return x1;
}
void one(long long*a,int SIZE){
    int i,j;
    for (i = 0; i < SIZE; i++)
        for (j = 0; j < SIZE; j++)
            a[i*SIZE+j] = (i == j);
    return;
}
void mul(long long*a,long long*b,int SIZE){
    int i,j,k;
    long long res[SIZE][SIZE];
    for(i=0;i<SIZE;i++)
      for(j=0;j<SIZE;j++)
        res[i][j]=0;
    for (i = 0; i < SIZE; i++)
        for (j = 0; j < SIZE; j++)
            for (k = 0; k < SIZE; k++)
                res[i][j] =(res[i][j] + a[i*SIZE+k] * b[k*SIZE+j])%MOD;
    for (i = 0; i < SIZE; i++)
        for (j = 0; j < SIZE; j++)
            a[i*SIZE+j] = res[i][j];
    return;
}
void powm(long long*a,int n,long long*res,int SIZE){
    one(res,SIZE);
    while (n > 0) {
        if (n % 2 == 0)
        {
            mul(a, a,SIZE);
            n /= 2;
        }
        else {
            mul(res, a,SIZE);
            n--;
        }
    }
}








In Python3 :





#http://stackoverflow.com/questions/24701490/modular-matrix-inversion-with-large-number?lq=1
MOD = 1000000007

def generalizedEuclidianAlgorithm(a, b):
    if b > a:
        return generalizedEuclidianAlgorithm(b,a);
    elif b == 0:
        return (1, 0);
    else:
        (x, y) = generalizedEuclidianAlgorithm(b, a % b);
        return (y, x - (a // b) * y)

def inversemodp(a, p):
    a = a % p
    if (a == 0):
        # print "a is 0 mod p"
        return 0
    (x,y) = generalizedEuclidianAlgorithm(p, a % p);
    return y % p

def identitymatrix(n):
    return [[int(x == y) for x in range(0, n)] for y in range(0, n)]

def multiply_vector_scalar(vector, scalar, q):
    kq = []
    for i in range (0, len(vector)):
        kq.append (vector[i] * scalar %q)
    return kq

def minus_vector_scalar1(vector1, scalar, vector2, q):
    kq = []
    for i in range (0, len(vector1)):
        kq.append ((vector1[i] - scalar * vector2[i]) %q)
    return kq

def inversematrix1(matrix, q):
    n = len(matrix)

    A =[]
    for j in range (0, n):
        temp = []
        for i in range (0, n):
            temp.append (matrix[j][i])
        A.append(temp)

    Ainv = identitymatrix(n)

    for i in range(0, n):
        factor = inversemodp(A[i][i], q)
        A[i] = multiply_vector_scalar(A[i],factor,q)
        Ainv[i] = multiply_vector_scalar(Ainv[i],factor,q)
        for j in range(0, n):
            if (i != j):
                factor = A[j][i]
                A[j] = minus_vector_scalar1(A[j],factor,A[i],q)
                Ainv[j] = minus_vector_scalar1(Ainv[j],factor,Ainv[i],q)
    return Ainv

def mult(x, y):
	c = [[0 for _ in range(len(y[0]))] for _ in range(len(x))]

	for i in range(len(x)):
		for j in range(len(y[0])):
			for k in range(len(x)):
				c[i][j] += x[i][k] * y[k][j]
				c[i][j] = c[i][j] % MOD
	return c

def matpow(b, p):
	if p == 0: return identitymatrix(n)
	if p == 1: return b
	if p % 2 == 1:
		return mult(b, matpow(b, p - 1))
	ret = matpow(b, p // 2)
	return mult(ret, ret)

n, k = map(int, input().split())
arrk = list(map(int, input().split()))
arrc = list(map(int, input().split()))

left = [[x] for x in arrk];
middle = [[0 for _ in range(n)] for _ in range(n)]
middle[0] = list(arrc)
for i in range(1,n):
	middle[i][i-1] = 1

inv = inversematrix1(middle, MOD)
inv = [[int(x) for x in y] for y in inv]

ans = matpow(inv, k - n + 1)
ans = mult(ans, left)

print(' '.join(map(lambda x : str(x[0]), ans)))
                        








View More Similar Problems

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →