Cod sursa(job #3140390)

Utilizator DariusM17Murgoci Darius DariusM17 Data 5 iulie 2023 23:21:16
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
//#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_map> 
#include <map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <cstring>
#include <assert.h>
using namespace std;
const string file = "iepuri";
ifstream cin(file + ".in");
ofstream cout(file + ".out");
#define FAST ios_base::sync_with_stdio(0), cin.tie(0),cout.tie(0) ;
const int MOD = 666013;
struct mat3x3 {
    long long  v[5][5]={ 0 };
};
struct mat3x1 {
    long long v[5][5] = { 0 };
};
mat3x3 inm(mat3x3 a, mat3x3 b) {
    mat3x3 c;
    for (int i = 1; i <= 3; ++i)
        for (int j = 1; j <= 3; ++j)
            for (int k = 1; k <= 3; ++k) c.v[i][j] += (1LL * a.v[i][k] * b.v[k][j]) % MOD;
    return c;
}
mat3x1 inm(mat3x3 a, mat3x1 b) {
    mat3x1 c;
    for (int i = 1; i <= 3; ++i)
        for (int k = 1; k <= 3; ++k) c.v[i][1] += (1LL * a.v[i][k] * b.v[k][1]) % MOD;
    return c;
}
mat3x3 ex(mat3x3 a, int pwr) {
    mat3x3 ans = a;
    pwr--;
    while (pwr) {
        if (pwr & 1) ans = inm(ans, a);
        a = inm(a, a);
        pwr >>= 1;
    }
    return ans;
}
void test_case(void) {
    int n, x, y, z, a, b, c;
    cin >> x >> y >> z >> a >> b >> c >> n;
    if (n == 1) {
        cout << x << '\n';
        return;
    }
    else if (n == 2) {
        cout << y << '\n';
        return;
    }
    else if (n == 3) {
        cout << z << '\n';
        return;
    }
    else {
        mat3x3 init;
        init.v[1][2] = 1, init.v[2][3] = 1, init.v[3][1] = c, init.v[3][2] = b, init.v[3][3] = a;
        mat3x1 aux;
        aux.v[1][1] = x, aux.v[2][1] = y, aux.v[3][1] = z;
        mat3x3 ans1 = ex(init, n - 2);
        mat3x1 ans2 = inm(ans1, aux);
        cout << ans2.v[3][1] << '\n';
    }
}
int main(void)
{
    int t;
    cin >> t;
    while (t--) test_case();
    return 0;
}