Cod sursa(job #2764420)

Utilizator DragosC1Dragos DragosC1 Data 20 iulie 2021 20:46:42
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <fstream>
#include <iostream>
using namespace std;

int T;
const int MOD = 666013;

ifstream f;
ofstream g;

struct matrix {
    int a[3][3];
    matrix operator* (matrix b) {
        matrix rez;
        int i, j, k;
        for (i = 0; i < 3; i++)
            for (j = 0; j < 3; j++)
                rez.a[i][j] = 0;
        for (i = 0; i < 3; i++)
            for (k = 0; k < 3; k++)
                for (j = 0; j < 3; j++) 
                    rez.a[i][j] = (rez.a[i][j] + 1LL * a[i][k] * b.a[k][j]) % MOD;
        return rez;
    }
};

matrix exp(matrix a, int b) {
    matrix P;
    int i, j;
    for (i = 0; i < 3; i++)
        for (j = 0; j < 3; j++)
            if (i == j)
                P.a[i][j] = 1;
            else P.a[i][j] = 0;

    while (b) {
        if (b & 1)
            P = P * a;
        a = a * a;
        b /= 2;
    }
    return P;
}

void solve() {
    int x, y, z, A, B, C, n;
    f >> x >> y >> z >> A >> B >> C >> n;
    matrix m1;
    m1.a[0][0] = 0, m1.a[0][1] = 1, m1.a[0][2] = 0;
    m1.a[1][0] = 0, m1.a[1][1] = 0, m1.a[1][2] = 1; 
    m1.a[2][0] = C, m1.a[2][1] = B, m1.a[2][2] = A;
    m1 = exp(m1, n - 2);
    g << (1LL * x * m1.a[2][0]) % MOD + (1LL * y * m1.a[2][1]) % MOD + (1LL * z * m1.a[2][2]) % MOD << '\n';
}

void read() {
    f.open("iepuri.in");
    g.open("iepuri.out");
    f >> T;
    while (T--)
        solve();
    f.close();
    g.close();
}


int main() {
    read();
    return 0;
}