Cod sursa(job #2882685)

Utilizator BorodiBorodi Bogdan Borodi Data 31 martie 2022 18:26:20
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <fstream>
#include <string>
#include <bitset>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <stack>
#include <bitset>
#include <stack>
#include <unordered_map>
#define MOD 666013
#define Dmax 4
using namespace std;

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

typedef vector < pair < int, int > > VIP;
typedef vector < int > VI;

int test_case, x, y, z, a, b, c, n;
int sol[Dmax][Dmax], matrix[Dmax][Dmax], I[Dmax][Dmax];

void Produs(int S[][Dmax], int rez[][Dmax], int matrix[][Dmax]){
    for(int row = 1; row <= 3; ++row)
        for(int column = 1; column <= 3; ++column)
            S[row][column] =  ( rez[row][1] * matrix[1][column] + rez[row][2] * matrix[2][column] + rez[row][3] * matrix[3][column] ) % MOD;
}
void Copy(int A[Dmax][Dmax], int B[Dmax][Dmax]){
    for(int i = 1; i <= 3; ++i)
        for(int j = 1; j <= 3; ++j)
            A[i][j] = B[i][j];
}
void M_la_N(int rez[Dmax][Dmax], int matrix[Dmax][Dmax], int n){
    int S[Dmax][Dmax];
    memset(S, 0, sizeof(S));

    while(n > 0){
        if(n % 2 == 1)
            Produs(S, rez, matrix),
            Copy(rez, S);
        
        Produs(S, matrix, matrix);
        Copy(matrix, S);

        n /= 2;
    }
}

int main() {
    fin >> test_case;

    while(test_case--){
        fin >> x >> y >> z >> a >> b >> c >> n;

        sol[3][1] = x;
        sol[3][2] = y;
        sol[3][3] = z;
        
        matrix[2][1] = matrix[3][2] = 1;
        matrix[1][3] = c;
        matrix[2][3] = b;
        matrix[3][3] = a;
        matrix[1][1] = matrix[1][2] = matrix[2][2] = matrix[3][1] = 0;
        memset(I, 0, sizeof(I));
        I[1][1] = I[2][2] = I[3][3] = 1;

        //! sol[3][3] * matrix ^ n va da matricea rezultatelor in ziua n + 2 
        //! sol[3][1] este raspunsul

        int s[Dmax][Dmax];
        
        memset(s, 0, sizeof(s));
        M_la_N(I, matrix, n - 2);
        Produs(s, sol, I);

        fout << s[3][3] << '\n';
    }

    return 0;   
}