Cod sursa(job #2076746)

Utilizator dariusdariusMarian Darius dariusdarius Data 27 noiembrie 2017 01:33:31
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <cstring>

#include <algorithm>
#include <fstream>
#include <iostream>
using namespace std;

const int MOD = 666013;

int matrix[4][4], answer[4][4];
int aux[4][4];

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

int main() {
    ifstream cin("iepuri.in");
    ofstream cout("iepuri.out");
    int tests;
    for (cin >> tests; tests; -- tests) {
        int x, y, z, a, b, c, n;
        cin >> x >> y >> z >> a >> b >> c >> n;
        matrix[1][1] = 0; matrix[1][2] = 0; matrix[1][3] = c;
        matrix[2][1] = 1; matrix[2][2] = 0; matrix[2][3] = b;
        matrix[3][1] = 0; matrix[3][2] = 1; matrix[3][3] = a;
        memset(answer, 0, sizeof answer);
        for (int i = 1; i <= 3; ++ i) {
            answer[i][i] = 1;
        }
        n -= 2;
        for (; n; n >>= 1) {
            if (n & 1) {
                multiply(answer, matrix);
            }
            multiply(matrix, matrix);
        }
        cout << (1LL * x * answer[1][3] + 1LL * y * answer[2][3] + 1LL * z * answer[3][3]) % MOD << "\n";
    }
    return 0;
}