Cod sursa(job #1456034)

Utilizator fluture.godlikeGafton Mihnea Alexandru fluture.godlike Data 29 iunie 2015 18:14:31
Problema Iepuri Scor 100
Compilator cpp Status done
Runda mpractice1 Marime 1.69 kb
#include <cstdio>
#define MOD 666013
using namespace std;
FILE *fin, *fout;
int t, x1, y1, z1, a1, b1, c1, n, r;
struct matrice
{
    int a[4][4];
} aux, nul, ans;
matrice inmultire(matrice temp1, matrice temp2)
{
    matrice temp3;
    for(int i = 1; i<= 3; i++)
    {
        for(int j = 1; j<= 3; j++)
        {
            temp3.a[i][j] = 0;
            for(int k = 1; k<= 3; k++)
            {
                temp3.a[i][j] = (temp3.a[i][j] + 1LL*temp1.a[i][k]*temp2.a[k][j])%MOD;
            }
        }
    }
    return temp3;
}
matrice power(matrice temp, int b)
{
    matrice sol = nul, tmp;
    for(;b;b>>=1)
    {
        if(b&1)
        {
            tmp = inmultire(sol, temp);
            sol = tmp;
        }
        tmp = inmultire(temp, temp);
        temp = tmp;
    }
    return sol;
}
int main()
{
    fin = freopen("iepuri.in", "r", stdin);
    fout = freopen("iepuri.out", "w", stdout);
    scanf("%d", &t);
    nul.a[1][1] = 1;
    nul.a[1][2] = 0;
    nul.a[1][3] = 0;
    nul.a[2][1] = 0;
    nul.a[2][2] = 1;
    nul.a[2][3] = 0;
    nul.a[3][1] = 0;
    nul.a[3][2] = 0;
    nul.a[3][3] = 1;
    for(int i = 1; i<= t; i++)
    {
        scanf("%d%d%d%d%d%d%d", &x1, &y1, &z1, &a1, &b1, &c1, &n);
        aux.a[1][1] = 0;
        aux.a[1][2] = 0;
        aux.a[1][3] = c1;
        aux.a[2][1] = 1;
        aux.a[2][2] = 0;
        aux.a[2][3] = b1;
        aux.a[3][1] = 0;
        aux.a[3][2] = 1;
        aux.a[3][3] = a1;
        ans = power(aux, n-2);
        r = (1LL * ans.a[1][3] * x1 + 1LL * ans.a[2][3] * y1 +  1LL * ans.a[3][3] * z1)%MOD;
        printf("%d\n", r);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}