Cod sursa(job #3247457)

Utilizator stefan_dore_Stefan Dore stefan_dore_ Data 7 octombrie 2024 20:12:56
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <iostream>
#include <fstream>
using namespace std;

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

const int MOD = 666013;

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

void mul(int A[][3], int B[][3]) {
    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] += 1LL * 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 M[3][3] =
    {
        {A, 1, 0},
        {B, 0, 1},
        {C, 0, 0}
    };
    int V[3][3] =
    {
        {Z, Y, X},
        {0, 0, 0},
        {0, 0, 0}
    };

    while(n > 0) {
        if (n%2==0) {
            mul(M, M);
            n /= 2;
        } else {
            mul(V, M);
            n--;
        }
    }
    return V[0][0];
}

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