Mai intai trebuie sa te autentifici.

Cod sursa(job #1007719)

Utilizator harababurelPuscas Sergiu harababurel Data 9 octombrie 2013 17:23:00
Problema Iepuri Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <iostream>
#include <fstream>
#define nmax 2000000005
#define mod 666013
#define ll long long
using namespace std;

struct Matrix {
    ll val[4][4];
};

Matrix attrib(ll a, ll b, ll c, ll d, ll e, ll f, ll g, ll h, ll i) {
    Matrix A;
    A.val[1][1] = a;
    A.val[1][2] = b;
    A.val[1][3] = c;
    A.val[2][1] = d;
    A.val[2][2] = e;
    A.val[2][3] = f;
    A.val[3][1] = g;
    A.val[3][2] = h;
    A.val[3][3] = i;
    return A;
}

Matrix product(Matrix A, Matrix B) {
    Matrix C = attrib(0, 0, 0, 0, 0, 0, 0, 0, 0);
    for(int i=1; i<=3; i++)
        for(int j=1; j<=3; j++) {
            for(int h=1; h<=3; h++)
                C.val[i][j] += A.val[i][h] * B.val[h][j];
            C.val[i][j] %= mod;
        }
    return C;
}

Matrix power(Matrix A, int exp) {
    if(exp == 1) return A;

    Matrix half = power(A, exp/2);

    if(exp % 2 == 0) return product(half, half);
    return product(product(half, half), A);
}
/*
void printMatrix(Matrix A) {
    for(int i=1; i<=3; i++) {
        cout<<"(";
        for(int j=1; j<=3; j++) cout<<A.val[i][j]<<" ";
        cout<<")\n";
    }
}
*/

int t, x, y, z, a, b, c, n, sol;

int main() {
	ifstream f("iepuri.in");
	ofstream g("iepuri.out");

	f>>t;
	while(t--) {
	    f>>x>>y>>z>>a>>b>>c>>n;
        Matrix Z = attrib(0, 0, c, 1, 0, b, 0, 1, a);

        Z = power(Z, n-2);
        sol = x * Z.val[1][3] + y * Z.val[2][3] + z * Z.val[3][3];

        g<<sol%mod<<"\n";
	}

    return 0;
}