Cod sursa(job #1819158)

Utilizator andreistanStan Andrei andreistan Data 30 noiembrie 2016 12:18:29
Problema Iepuri Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <fstream>

using namespace std;
const int MOD = 666013;

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

int X, Y, Z, AA, BB, CC;

void mul(int A[3][3], int B[3][3]) // A <-- A*B
{
    long long C[3][3];
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
        {
            C[i][j] = 0;
            for(int k = 0; k < 3; k++)
                C[i][j] += (long long)A[i][k] * B[k][j];
        }
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            A[i][j] = C[i][j] % MOD;
}

int putere(int n)
{
    int A[3][3] = {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}};
    int P[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
    A[0][2] = CC;
    A[1][2] = BB;
    A[2][2] = AA;
    while(n > 0)
    {
        while(n % 2 == 0)
        {
            mul(A, A);
            n /= 2;
        }
        mul(P, A);
        n--;
    }
    return (X * P[0][2] + Y * P[1][2] + Z * P[2][2]) % MOD;
}

int main()
{
    int T, N;
    f >> T;
    while(T--)
    {
        f >> X >> Y >> Z >> AA >> BB >> CC;
        f >> N;
        g << putere(N - 2) << '\n';
    }
    f.close();
    g.close();
    return 0;
}