Cod sursa(job #1842521)

Utilizator Walrus21andrei Walrus21 Data 7 ianuarie 2017 02:06:31
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <stdio.h>
#include <string.h>
#define m 666013

using namespace std;

int t, T, x, y, z, a, b, c, n;
long long A[3][3], C[3][3];

void mul(long long a[3][3], long long b[3][3])
{
    int i, j;
    long long c[3][3];
    memset(c, 0, sizeof(c));
    for(i = 0; i < 3; i++)
        for(j = 0; j < 3; j++)
            c[i][j] = (a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j]) % m;
    memcpy(a, c, sizeof(c));

}

int main()
{
    freopen("iepuri.in", "r", stdin);
    freopen("iepuri.out", "w", stdout);
    scanf("%d", &T);
    for(t = 0; t < T; t++)
    {
        scanf("%d%d%d%d%d%d%d", &x, &y, &z, &a, &b, &c, &n);
        memset(A, 0, sizeof(A));
        memset(C, 0, sizeof(C));
        A[0][0] = a;
        A[0][1] = b;
        A[0][2] = c;
        A[1][0] = A[2][1] = 1;
        C[0][0] = C[1][1] = C[2][2] = 1;
        n -= 2;
        while(n > 1)
        {
            if(n % 2)
            {
                mul(C, A);
                mul(A, A);
                n = (n - 1) / 2;
            }
            else
            {
                mul(A, A);
                n /= 2;
            }
        }
        mul(A, C);
        printf("%d\n",(A[0][0] * z + A[0][1] * y + A[0][2] * x) % m);
    }
    return 0;
}