Cod sursa(job #2259655)

Utilizator armandpredaPreda Armand armandpreda Data 13 octombrie 2018 16:30:59
Problema Iepuri Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <cstdio>
#include <cstring>

using namespace std;

const int MOD = 666013;
int t, x, y, z, n;
long long mat[5][5], rez[5][5];

void multiply(long long a[][5], long long b[][5])
{
    int c[5][5];
    for(int i = 1; i <= 3; ++i)
        for(int j = 1; j <= 3; ++j)
            c[i][j] = (a[i][1] * b[1][j] + a[i][2] * b[2][j] + a[i][3] * b[3][j]) % MOD;
    for(int i = 1; i <= 3; ++i)
        for(int j = 1; j <= 3; ++j)
            a[i][j] = c[i][j];
}

int main()
{
    freopen("iepuri.in", "r", stdin);
    freopen("iepuri.out", "w", stdout);

    scanf("%d", &t);
    while(t--)
    {
        rez[1][2] = rez[1][3] = rez[2][1] = rez[2][3] = rez[3][1] = rez[3][2] = 0;
        rez[1][1] = rez[2][2] = rez[3][3] = 1;

        mat[2][2] = mat[3][2] = mat[1][3] = mat[3][3] = 0;
        mat[1][2] = mat[2][3] = 1;

        scanf("%d%d%d%lld%lld%lld%d", &x, &y, &z, &mat[1][1], &mat[2][1], &mat[3][1], &n);

        n -= 2;
        for(int mask = 1; mask <= n; mask <<= 1)
        {
            if(n & mask)
                multiply(rez, mat);
            multiply(mat, mat);
        }

        printf("%d\n", (x * rez[3][1] + y * rez[2][1] + z * rez[1][1]) % MOD);
    }
    return 0;
}