Cod sursa(job #3269570)

Utilizator Alex_BerbescuBerbescu Alexandru Alex_Berbescu Data 19 ianuarie 2025 18:57:43
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.02 kb
#pragma GCC optimize("O3")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define mod 666013
using namespace std;
const int dim = 1e5 + 55;
int x, y, z, a, b, c, n, t;
int matunu[3][3], matdoi[3][3], mattrei[1][1];
inline void open(const string name)
{
#ifndef ONLINE_JUDGE
    (void)!freopen((name + ".in").c_str(), "r", stdin);
    (void)!freopen((name + ".out").c_str(), "w", stdout);
#endif // ONLINE_JUDGE
}
void inmult(int d[3][3], int e[3][3])
{
    int f[3][3] = {0};
    for(int k = 0; k <= 2; ++k)
        for(int i = 0; i <= 2; ++i)
           for(int j = 0; j <= 2; ++j)
              f[i][j] = (f[i][j] % mod + (d[i][k] * e[k][j]) % mod) % mod;

    for(int i = 0; i <= 2; ++i)
        for(int j = 0; j <= 2; ++j)
          d[i][j] = f[i][j];
}
void lgput(int d[][3], int exp)
{
    int sol[3][3] = {0};
    sol[0][0] = sol[1][1] = sol[2][2] = 1;
    while(exp)
    {
        if(exp & 1)
            inmult(sol, d);
        exp >>= 1;
        inmult(d, d);
    }
    for(int i = 0; i <= 2; ++i)
        for(int j = 0; j <= 2; ++j)
           d[i][j] = sol[i][j];
}
int32_t main(int argc, char * argv[])
{
    open("iepuri");
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while(t--)
    {
        cin >> x >> y >> z >> a >> b >> c >> n;
        matunu[0][0] = x, matunu[0][1] = y, matunu[0][2] = z;
        matdoi[0][0] = matdoi[0][1] = matdoi[1][1] = matdoi[2][0] = 0;
        matdoi[0][2] = c;
        matdoi[1][0] = matdoi[2][1] = 1;
        matdoi[1][2] = b;
        matdoi[2][2] = a;
        if(n < 3)
        {
            if(n == 0)
                cout << x % mod << '\n';
            if(n == 1)
                cout << y % mod << '\n';
            if(n == 2)
                cout << z % mod << '\n';
        }
        else
        {
            lgput(matdoi, n - 2);
            inmult(matunu, matdoi);
            cout << matunu[0][2] % mod << '\n';
        }


    }
    return 0;
}