Cod sursa(job #2586812)

Utilizator ana_maria.milcuAna-Maria Milcu ana_maria.milcu Data 21 martie 2020 16:25:23
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <fstream>

#define LL long long
#define MOD 666013
using namespace std;

ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
LL A[4][4], sol;


void multiply(LL a[4][4], LL b[4][4], LL c[4][4]) {
    LL aux;
    for(int i = 1; i <= 3; ++i)
        for(int j = 1; j <= 3; ++j) {
            aux = 0;
            for(int k = 1; k <= 3; ++k) {
                aux += a[i][k] * b[k][j];
            }
            c[i][j] = aux % MOD;
        }
}

void power_matrix (LL C[4][4], int p, LL R[4][4]) {

    LL tmp[4][4];
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            tmp[i][j] = (i == j) ? 1 : 0;
        }
    }
    while (p != 1) {
        if (p % 2 == 0) {
            multiply(C, C, C);
            p = p / 2 ;
        } else {
            multiply(tmp, C, tmp);
            p--;
        }
    }
    multiply(C, tmp, R);
}

int main() {

    int t;
    LL x1, x2, x3, a, b , c, n;
    fin >> t;
    for (int i = 0; i < t; i++) {
        sol = 0;
        fin >> x1 >> x2 >> x3 >> a >> b >> c >> n;
        A[4][4];
        A[1][1] = 0; A[2][1] = 1; A[3][1] = 0;
        A[1][2] = 0; A[2][2] = 0; A[3][2] = 1;
        A[1][3] = c; A[2][3] = b; A[2][3] = a;
        power_matrix(A, n - 2, A);
        sol = (LL) (x1 * A[1][3] + x2 * A[2][3] + x3 * A[3][3]);
        fout << sol % MOD << endl;
    }
    fin.close();
    fout.close();
    return 0;
}