Mai intai trebuie sa te autentifici.

Cod sursa(job #2450820)

Utilizator Senth30Denis-Florin Cringanu Senth30 Data 24 august 2019 17:07:10
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.25 kb
#include <bits/stdc++.h>
#define MAX 131072
#define MOD 666013

using namespace std;

FILE *IN;

int T;
int X, Y, Z, A, B, C, N;

int sign, pos;
char f[MAX];

inline void Read(int &nr){
    sign = 0;
    nr = 0;
    while(f[pos] < '0' || f[pos] > '9'){
        if(f[pos] == '-') sign=1;
        pos++;
        if(pos == MAX)
            fread(f, MAX, 1, IN), pos = 0;
    }
    while(f[pos] >= '0' && f[pos] <= '9'){
        nr = 10 * nr + f[pos++] - '0';
        if(pos == MAX)
            fread(f, MAX, 1, IN), pos = 0;
    }
    if(sign) nr =- nr;
}

struct Matrix{
    int L, W, mat[3][3];

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

    void Set_Size(int x, int y){
        L = x;
        W = y;
    }

    void Identity(){
        this->Clear();
        for(int i = 0; i < L; i++)
            mat[i][i] = 1;
    }

    void Multiply(Matrix A){
        Matrix aux;
        aux.Set_Size(L, A.W);
        aux.Clear();
        for(int i = 0; i < L; i++)
            for(int j = 0; j < A.W; j++)
                for(int k = 0; k < L; k++)
                    aux.mat[i][j] = (aux.mat[i][j] + 1LL * mat[i][k] * A.mat[k][j]) % MOD;
        *this = aux;
    }

    void LgPow(int exp){
        Matrix ans, base;
        base = *this;
        ans.Set_Size(L, W);
        ans.Identity();
        for(int i = 0; i < 31; i++){
            if((1 << i) & exp)
                ans.Multiply(base);
            base.Multiply(base);
        }
        *this = ans;
    }
}Alfa, Beta;

int main(){

    IN = fopen("iepuri.in", "r");
    freopen("iepuri.out", "w", stdout);

    Read(T);
    Alfa.Set_Size(3,3);
    Beta.Set_Size(3,1);
    for(int i = 1; i <= T; i++){
        Read(X); Read(Y); Read(Z);
        Read(A); Read(B); Read(C);
        Read(N);
        Alfa.Set_Size(3,3);
        Alfa.Clear(); Beta.Clear();
        Beta.mat[0][0] = X; Beta.mat[1][0] = Y; Beta.mat[2][0] = Z;
        Alfa.mat[0][1] = Alfa.mat[1][2] = 1;
        Alfa.mat[2][0]=C; Alfa.mat[2][1]=B; Alfa.mat[2][2]=A;
        Alfa.LgPow(N);
        Alfa.Multiply(Beta);
        printf("%d\n", Alfa.mat[0][0]);
    }

    return 0;
}