Cod sursa(job #2150529)

Utilizator netfreeAndrei Muntean netfree Data 3 martie 2018 16:57:06
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("iepuri.in");
ofstream fout("iepuri.out");

typedef long long matrix[4][4];
const int mod = 666013;

int t, x, y, z, a, b, c, nn;

matrix REZ, A;

void empty(matrix a){
    for(int i = 1; i<=3; ++i)
        for(int j = 1; j<=3; ++j)
            a[i][j] = 0;
}

void print(matrix a){
    for(int i = 1; i<=3; ++i, cout << "\n")
        for(int j = 1; j<=3; ++j, cout << " ")
            cout << a[i][j];
    cout << endl;
}

void copy(matrix from, matrix to){
    for(int i = 1; i<=3; ++i)
        for(int j = 1; j<=3; ++j)
            to[i][j] = from[i][j];
}

void multiply(matrix a, matrix b, matrix c){
    for(int i = 1; i<=3; ++i)
        for(int j = 1; j<=3; ++j){
            c[i][j] = 0;
            for(int k = 1; k<=3; ++k)
                c[i][j] += ((a[i][k]%mod)*(b[k][j]%mod))%mod;
            c[i][j] %= mod;
        }
}

void inmlog(int n){
    matrix temp;
    while(n){
        if(n & 1){
            multiply(REZ, A, temp);
            copy(temp, REZ);
            n--;
        }
        multiply(A, A, temp);
        copy(temp, A);
        n >>= 1;
    }
}

int main()
{
    fin >> t;
    while(t--){
        fin >> x >> y >> z >> a >> b >> c >> nn;
        empty(A); empty(REZ);
        for(int i = 1; i<=3; ++i)
            REZ[i][i] = 1;
        A[1][3] = c;
        A[2][3] = b;
        A[3][3] = a;
        A[2][1] = 1;
        A[3][2] = 1;

        inmlog(nn-2);

        //print(REZ);

        fout << ((x * REZ[1][3]) % mod + (y * REZ[2][3]) % mod + (z * REZ[3][3]) % mod) % mod<< "\n";
    }
    return 0;
}