Cod sursa(job #3166460)

Utilizator vlad_maneaManea Vlad Cristian vlad_manea Data 8 noiembrie 2023 19:34:26
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <iostream>
#include <fstream>

#define MOD 666013

using namespace std;

ifstream fin("iepuri.in");
ofstream fout("iepuri.out");

long long A[3][3], R[3][3];

void multiplication(long long a[][3], long long b[][3]) {
    long long c[3][3]={0};
    for (int i=0; i<3; i++)
        for (int j=0; j<3; j++)
            for (int d=0; d<3; d++)
                c[i][j]=(c[i][j]+(a[i][d]*b[d][j])%MOD)%MOD;
    for (int i=0; i<3; i++)
        for (int j=0; j<3; j++)
            a[i][j]=c[i][j];
}

void power(long long n) {
    if (n==0) {
        R[0][0]=1;
        R[0][1]=0;
        R[0][2]=0;
        R[1][0]=0;
        R[1][1]=1;
        R[1][2]=0;
        R[2][0]=0;
        R[2][1]=0;
        R[2][2]=1;
        return;
    }
    power(n/2);
    multiplication(R, R);
    if (n%2==1)
        multiplication(R, A);
}

void citire() {
    int x, y, z, a, b, c, n;
    fin>>x>>y>>z>>a>>b>>c>>n;
    A[0][0]=a;
    A[1][0]=1;
    A[0][1]=b;
    A[2][1]=1;
    A[0][2]=c;
    power(n-2);
    fout<<(z*R[0][0]%MOD+y*R[0][1]%MOD+x*R[0][2]%MOD)%MOD<<'\n';
}

int main() {
    int t;
    fin>>t;
    for (int i=0; i<t; i++)
        citire();
    return 0;
}