Cod sursa(job #2259649)

Utilizator armandpredaPreda Armand armandpreda Data 13 octombrie 2018 16:26:08
Problema Iepuri Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream cin ("iepuri.in");
ofstream cout ("iepuri.out");

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];
}

void print(long long a[][5])
{
    for(int i = 1; i <= 3; ++i)
    {
        for(int j = 1; j <= 3; ++j)
            cout << a[i][j] << ' ';
        cout << '\n';
    }
}

int main()
{
    cin >> 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;

        cin >> 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);
        }

        cout << (x * rez[3][1] + y * rez[2][1] + z * rez[1][1]) % MOD << '\n';
    }
    return 0;
}