Cod sursa(job #2291073)

Utilizator andreisavulescuSavulescu Andrei andreisavulescu Data 27 noiembrie 2018 14:57:03
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <fstream>
#include <cstring>
using namespace std;
const int MOD = 666013;

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

int T, X, Y, Z, A, B, C, N;

void multmat(int a[][3], int b[][3])
{
    int 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] += 1LL * a[i][k] * b[k][j] % MOD;
            c[i][j] %= MOD;
        }
    memcpy(a, c, sizeof(c));
}

int puteremat(int p)
{
    int M[3][3] = {{A, 1, 0}, {B, 0, 1}, {C, 0, 0}};
    int I[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
    while(p > 0)
    {
        if(p % 2 == 0)
        {
            multmat(M, M);
            p /= 2;
        }
        else
        {
            multmat(I, M);
            p--;
        }
    }
    return (1LL * Z * I[0][0] + 1LL * Y * I[1][0] + 1LL * X * I[2][0]) % MOD;
}

int main()
{
    f >> T;
    while(T--)
    {
        f >> X >> Y >> Z >> A >> B >> C >> N;
        g << puteremat(N - 2) << '\n';
    }
    return 0;
}