Cod sursa(job #1504426)

Utilizator radu_uniculeu sunt radu radu_unicul Data 17 octombrie 2015 18:41:56
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include<stdio.h>
#include <cstring>

using namespace std;
const int NMax = 5;
const int MOD = 666013;

int m[NMax][NMax], answer[NMax][NMax], v[NMax][NMax];

void power(int a[NMax][NMax], int b[NMax][NMax]){
    memset(v, 0, sizeof(v));
    for(int k = 1; k <= 3; k++){
        for(int i = 1; i <= 3; i++){
            for(int j = 1; j <= 3; j++){
                v[i][j] = (1LL * v[i][j] + 1LL * a[i][k] * b[k][j]) % MOD;
            }
        }
    }
    memcpy(a, v, sizeof(v));
}

int main(){
    int t, x, y, z, a, b, c, n;
    freopen("iepuri.in","r",stdin);
    freopen("iepuri.out","w",stdout);
    scanf("%d",&t);
    while(t--){
        scanf("%d %d %d %d %d %d %d ",&x,&y,&z,&a,&b,&c,&n);
        m[1][1] = 0; m[1][2] = 0; m[1][3] = c;
        m[2][1] = 1; m[2][2] = 0; m[2][3] = b;
        m[3][1] = 0; m[3][2] = 1; m[3][3] = a;
        memset(answer, 0, sizeof(answer));
        for(int i = 1; i <= 3; i++){
            answer[i][i] = 1;
        }
        n -= 2;
        while(n){
            if(n % 2){
                power(answer, m);
            }
            power(m, m);
            n >>= 1;
        }
        printf("%d\n",(1LL * x * answer[1][3] + 1LL * y * answer[2][3] + 1LL * z * answer[3][3]) % MOD);
    }
    return 0;
}