Cod sursa(job #3153257)

Utilizator TudorMMPopescu Tudor Mihai TudorMM Data 28 septembrie 2023 19:18:17
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define int long long

using namespace std;
#define cin in
#define cout out
ifstream in ("iepuri.in");
ofstream out ("iepuri.out");

#define mod 666013
struct matrix {int mat[4][4]; int n; int m;};
matrix I; matrix A; matrix B;

matrix multi(matrix X, matrix Y)
{
    matrix ans; ans.n=X.n; ans.m=Y.m;

    for (int i=0; i<X.n; i++)
        for (int j=0; j<Y.m; j++)
        {
            ans.mat[i][j]=0;
            for (int t=0; t<X.m; t++)
            {
                ans.mat[i][j]+=(X.mat[i][t]*Y.mat[t][j])%mod;
                ans.mat[i][j]%=mod;
            }
        }
    return ans;
}

matrix expon(matrix base, int exp)
{
    if (exp==0) return I; if (exp==1) return base;
    matrix aux=expon(base, exp/2);

    return (exp%2==0 ? multi(aux, aux) : multi(multi(aux, aux), base));
}

signed main()
{
    I.n=3; I.m=3; I.mat[0][0]=1; I.mat[1][1]=1; I.mat[2][2]=1;
    int t; cin>>t;
    while (t--)
    {
        int x,y,z,a,b,c,n; cin>>x>>y>>z>>a>>b>>c>>n;

        for (int i=0; i<3; i++)
            for (int j=0; j<3; j++)
            {
                A.mat[i][j]=0;
            }
        A.n=3; A.m=3; A.mat[1][0]=1; A.mat[2][1]=1; A.mat[0][2]=c; A.mat[1][2]=b; A.mat[2][2]=a;
        A=expon(A, n-2);

        B.n=1; B.m=3;  B.mat[0][0]=x; B.mat[0][1]=y; B.mat[0][2]=z;
        B=multi(B, A);
        cout<<B.mat[0][2]<<"\n";
    }
    return 0;
}