Cod sursa(job #2382505)

Utilizator ovidiuz98Zamfir Ovidiu ovidiuz98 Data 18 martie 2019 14:03:11
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <fstream>
#define MOD 666013
 
using namespace std;
 
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
 
int T, N;
 
int A, B, C, X, Y, Z;
 
void setM(int M[3][3],int A,int B,int C){
 
    for(int i = 0; i < 3; i ++)
        for(int j = 0; j < 3; j ++)
            M[i][j] = 0;
    M[1][0] = M[2][1] = 1;
    M[0][2] = C;
    M[1][2] = B;
    M[2][2] = A;
 
}
 
void setV(int M[3][3], int A,int B, int C){
 
    for(int i = 0; i < 3; i ++)
        for(int j = 0; j < 3; j ++)
            M[i][j] = 0;
 
    M[0][0] = A;
    M[0][1] = B;
    M[0][2] = C;
 
}
 
void multiply(int A[3][3], int B[3][3], int C[3][3]){
 
    for(int i = 0; i < 3; i ++)
        for(int j = 0; j < 3; j ++){
            C[i][j] = 0;
            for(int k = 0; k < 3 ; k ++)
                C[i][j] = (C[i][j] + ( 1LL * A[i][k] * B[k][j]) % MOD ) % MOD;
        }
}
 
void copy(int source[3][3], int destination[3][3]){
 
    for(int i = 0; i < 3; i ++)
        for(int j = 0; j < 3; j ++)
            destination[i][j] = source[i][j];
}
 
int M[3][3], V[3][3], aux[3][3];
 
int main(){
 
    fin >> T;
 
    while(T --){
 
        fin >> X >> Y >> Z >> A >> B >> C >> N;
 
        N -= 2;
 
        setM(M, A, B, C);
        setV(V, X, Y, Z);
 
        while(N){
 
            if(N & 1){
                multiply(V, M, aux);
                copy(aux, V);
            }
 
            multiply(M, M, aux);
            copy(aux, M);
 
            N /= 2;
 
        }
 
        fout << V[0][2] << "\n";
 
    }
 
    fin.close();
    fout.close();
 
    return 0;
 
}