Cod sursa(job #772150)

Utilizator SchumiDumitru Andrei Georgian Schumi Data 28 iulie 2012 13:59:20
Problema Iepuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long int64;

const int MOD = 666013;

int n, t, x, y, z, a, b, c;
int64 mat[4][4], mat2[4][4];

void inmulteste(int64 a[][4], int64 b[][4])
{
    int64 c[4][4];

    memset(c, 0, sizeof(c));

    for (int i = 1; i <= 3; ++i)
        for (int j = 1; j <= 3; ++j)
            for (int k = 1; k <= 3; ++k)
                c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % MOD;

    memcpy(a, c, sizeof(c));
}

void lgput(int x)
{
    if (x == 1)
        return;

    if (!(x & 1)) {
        lgput(x / 2);
        inmulteste(mat, mat);
    }
    else {
        lgput(x / 2);
        inmulteste(mat, mat);
        inmulteste(mat, mat2);
    }
}

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

    scanf("%d", &t);
    
    for (int i = 1; i <= t; ++i) {
        scanf("%d %d %d %d %d %d %d", &x, &y, &z, &a, &b, &c, &n);

        memset(mat, 0, sizeof(mat));
        mat[1][2] = mat[2][3] = 1;
        mat[3][1] = c, mat[3][2] = b, mat[3][3] = a;
        memcpy(mat2, mat, sizeof(mat));

        lgput(n - 2);

        printf("%lld\n", mat[3][1] * x + mat[3][2] * y + mat[3][3] * z);
    }
}