Cod sursa(job #3265753)

Utilizator AlexPlesescuAlexPlesescu AlexPlesescu Data 2 ianuarie 2025 22:19:38
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <bits/stdc++.h>

using namespace std;
#define int long long int
#define pb push_back
#define sz(x) (int)(x.size())
#define all(a) a.begin(), a.end()
#define nl '\n'
const int N = 3e5 + 5, INF = 1e9, mod = 666013;

struct matrix {
    int n, m, a[20][20];
};

matrix inm(matrix a, matrix b) {
    matrix rez;
    int n = a.n, m = b.m, in = a.m;
    rez.n = n, rez.m = m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            rez.a[i][j] = 0;
            for (int k = 1; k <= in; k++) {
                rez.a[i][j] += a.a[i][k] * b.a[k][j];
                rez.a[i][j] %= mod;
            }
        }
    }
    return rez;
}

matrix pw(matrix a, int n) {
    if (n == 1)
        return a;
    else {
        if (n % 2) {
            return inm(a, pw(a, n - 1));
        }
        else {
            matrix c = pw(a, n / 2);
            return inm(c, c);
        }
    }
}

void run_case(int test_case) {
    int x, y, z, a, b, c, n;
    cin >> x >> y >> z >> a >> b >> c >> n;
    matrix A, B;
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            A.a[i][j] = B.a[i][j] = 0;
        }
    }
    A.n = 3, A.m = 1, A.a[1][1] = x, A.a[2][1] = y, A.a[3][1] = z;
    B.n = 3, B.m = 3, B.a[1][2] = 1, B.a[2][3] = 1, B.a[3][3] = a, B.a[3][2] = b, B.a[3][1] = c;
    matrix rez = inm(pw(B, n - 2), A);
    cout << rez.a[3][1] << '\n';
};

signed main() {
    freopen("iepuri.in", "r", stdin);
    freopen("iepuri.out", "w", stdout); 
    int T = 1;
    cin >> T;
    for (int t = 1; t <= T; ++t) {
#ifdef _DEBUG
        cerr << "=== Subtask " << t << " ===" << '\n';
#endif
        run_case(t);
#ifdef _DEBUG
        cerr << nl;
#endif
    }
}