Cod sursa(job #3138729)

Utilizator TeodoraMaria123Serban Teodora Maria TeodoraMaria123 Data 21 iunie 2023 19:19:09
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.25 kb
#include <bits/stdc++.h>

using namespace std;

const int MOD = 666013;
int sol[3][3], matrix[3][3];

void initialise(int a[][3])
{
    for(int i = 0; i < 3; i ++)
        for(int j = 0; j < 3; j ++)
            a[i][j] = 0;
}

void print(int a[][3])
{
    for(int i = 0; i < 3; i ++)
    {
        for(int j = 0; j < 3; j ++)
            cout << a[i][j] << " ";
        cout << "\n";
    }
    cout << "\n";
}

void copyMatrix(int a[][3], int b[][3])
{
    for(int i = 0; i < 3; i ++)
        for(int j = 0; j < 3; j ++)
            a[i][j] = b[i][j];
}

void multiply(int a[][3], int b[][3])
{
    int c[3][3];
    initialise(c);
    for(int i = 0; i < 3; i ++)
        for(int j = 0; j < 3; j ++)
        {
            for(int k = 0; k < 3; k ++)
            {
               c[i][j] +=  (a[i][k] * b[k][j]) % MOD;
               if(c[i][j] >= MOD)
                  c[i][j] -= MOD;
            }
        }

    copyMatrix(a, c);
}

void multiply(int a[3], int b[][3])
{
    int c[3];
    for(int i = 0; i < 3; i ++)
        c[i] = 0;
    for(int j = 0; j < 3; j ++)
    {
        for(int k = 0; k < 3; k ++)
        {
            c[j] += (a[k] * b[k][j]) % MOD;
            if(c[j] >= MOD)
                  c[j] -= MOD;
        }
    }

    for(int i = 0; i < 3; i ++)
       a[i] = c[i];
}

void pow(int ans[][3], int base[][3], int exp)
{
    while(exp)
    {
        if(exp % 2)
            multiply(ans, base);
        multiply(base, base);
        exp >>= 1;
    }
}

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

    freopen("iepuri.in", "r", stdin);
    freopen("iepuri.out", "w", stdout);

    int t;
    cin >> t;
    while(t --)
    {
        int v[3], a, b, c, n;
        cin >> v[2] >> v[1] >> v[0] >> a >> b >> c >> n;
        initialise(sol);
        for(int i = 0; i < 3; i ++)
            sol[i][i] = 1;
        matrix[0][0] = a;
        matrix[0][1] = 1;
        matrix[0][2] = 0;
        matrix[1][0] = b;
        matrix[1][1] = 0;
        matrix[1][2] = 1;
        matrix[2][0] = c;
        matrix[2][1] = 0;
        matrix[2][2] = 0;
        pow(sol, matrix, n - 2);
        multiply(v, sol);
        cout << v[0] << "\n";
    }
    return 0;
}