Cod sursa(job #2753280)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 22 mai 2021 10:31:03
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.63 kb
#include <bits/stdc++.h>
//#pragma GCC optimize ("03")
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define FILES freopen("iepuri.in" , "r" , stdin) , freopen("iepuri.out" , "w" , stdout)
#define ll long long
#define ull unsigned long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define qwerty1 first
#define qwerty2 second
#define qwerty3 -> first
#define qwerty4 -> second
#define umap unordered_map
#define uset unordered_set
#define pii pair < ll , ll >
#define pq priority_queue
#define dbg(x) cerr << #x << ": " << x << '\n'

namespace FastRead
{
    char __buff[5000];ll __lg = 0 , __p = 0;
    char nc()
    {
        if(__lg == __p){__lg = fread(__buff , 1 , 5000 , stdin);__p = 0;if(!__lg) return EOF;}
        return __buff[__p++];
    }
    template<class T>void read(T&__x)
    {
        T __sgn = 1; char __c;while(!isdigit(__c = nc()))if(__c == '-')__sgn = -1;
        __x = __c - '0';while(isdigit(__c = nc()))__x = __x * 10 + __c - '0';__x *= __sgn;
    }
}

using namespace FastRead;
using namespace std;

const ll N = 1e5 + 10;
const ll M = 666013;
const ld PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll A , B , C , X , Y , Z , n;
ll a[5][5] , ans[5][5] , b[5][5] , f[6];

void mul(ll a[][5] , ll b[][5] , ll c[][5])
{
    ll j;

    for(ll i = 1 ; i <= 3 ; i++)
        for(j = 1  , c[i][j] = 0 ; j <= 3 ; j++ , c[i][j] = 0)
            for(ll t = 1 ; t <= 3 ; t++)
                c[i][j] = (c[i][j] + a[i][t] * b[t][j] % M) % M;
}

void pw(ll p)
{
    for( ; p ; p >>= 1 , mul(a , a , a))
        if(p & 1)
            mul(ans , a , ans);
}

void Test()
{
    cin >> f[0] >> f[1] >> f[2] >> C >> B >> A >> n;

    f[3] = (A * f[0] % M + B * f[1] % M + C * f[2] % M) % M;
    f[4] = (A * f[1] % M + B * f[2] % M + C * f[3] % M) % M;

    if(n < 5)
        return cout << f[n] << '\n' , void();

    a[1][1] = A , a[1][2] = B , a[1][3] = C;
    a[2][1] = A , a[2][2] = B , a[2][3] = C;
    a[3][1] = A , a[3][2] = B , a[3][3] = C;

    ans[1][1] = 1 , ans[1][2] = 1 , ans[1][3] = 1;
    ans[2][1] = 1 , ans[2][2] = 1 , ans[2][3] = 1;
    ans[3][1] = 1 , ans[3][2] = 1 , ans[3][3] = 1;

    b[1][1] = f[4] , b[1][2] = f[3] , b[1][3] = f[2];
    b[2][1] = f[3] , b[1][2] = f[2] , b[1][3] = f[1];
    b[3][1] = f[2] , b[1][2] = f[1] , b[1][3] = f[0];

    pw(n - 5);

    cout << (ans[1][1] * f[4] + ans[1][2] * f[3] + ans[1][3] * f[2]) % M << '\n';
}

signed main()
{
	#ifndef ONLINE_JUDGE
		FastIO , FILES;
	#endif

	ll q; cin >> q;
	while(q--) Test();

    return 0;
}