Cod sursa(job #3165625)

Utilizator MilitaruMihai2022Millitaru Mihai MilitaruMihai2022 Data 6 noiembrie 2023 16:59:44
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <fstream>
#include <cstring>
using namespace std;
const int MOD = 666013;
int X, Y, Z, A, B, C;
ifstream f("iepuri.in");
ofstream g("iepuri.out");

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

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

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