Cod sursa(job #2547306)

Utilizator alexradu04Radu Alexandru alexradu04 Data 15 februarie 2020 11:06:51
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <bits/stdc++.h>

#define input "iepuri.in"
#define output "iepuri.out"
#define MOD 666013

using namespace std;
typedef long long ll;

ifstream in(input);
ofstream out(output);

class matrix
{
public:
    int N, M;
    ll m[3][3];
};

matrix operator*(matrix a, matrix b)
{
    matrix c;
    c.N = a.N, c.M = b.M;
    memset(c.m, 0, sizeof c.m);
    for(int i = 0; i < c.N; i++)
        for(int j = 0; j < c.M; j++)
            for(int k = 0; k < a.M; k++)
                c.m[i][j] = (c.m[i][j] + a.m[i][k] * b.m[k][j] % MOD) % MOD;
    return c;
}

matrix fasto_expo(matrix a, ll exp)
{
    if(exp == 1)
        return a;
    if(exp % 2 == 1)
        return a * fasto_expo(a * a, exp / 2);
    return fasto_expo(a * a, exp / 2);
}

int T;

void Read_Data()
{
    in >> T;
    while(T--)
    {
        ll X, Y, Z, A, B, C, N;
        in >> X >> Y >> Z >> A >> B >> C >> N;
        matrix SOL;
        SOL.N = 1, SOL.M = 3;
        memset(SOL.m, 0, sizeof SOL.m);
        SOL.m[0][0] = X, SOL.m[0][1] = Y, SOL.m[0][2] = Z;

        matrix Q;
        Q.N = 3, Q.M = 3;
        memset(Q.m, 0, sizeof Q.m);
        Q.m[1][0] = 1, Q.m[2][1] = 1, Q.m[0][2] = C, Q.m[1][2] = B, Q.m[2][2] = A;

        Q = fasto_expo(Q, N - 2);
        SOL = SOL * Q;
        out << SOL.m[0][2] << "\n";
    }
}

int main()
{
    Read_Data();
    return 0;
}