Cod sursa(job #1788355)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 25 octombrie 2016 22:08:28
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;

const int MOD = 666013;

struct MX3 {
    i64 mx[4][4];

    inline MX3() {
        memset(mx, 0x00, sizeof(mx)); }

    inline i64& operator () (int x, int y) {
        return mx[x][y]; }

    MX3 operator + (MX3 &arg) {
        MX3 res;

        for(int i=1; i<=3; ++i)
        for(int j=1; j<=3; ++j)
            res(i, j) = (mx[i][j] + arg(i, j)) % MOD;

        return res; }

    MX3 operator * (MX3 &arg) {
        MX3 res;

        for(int i=1; i<=3; ++i)
        for(int j=1; j<=3; ++j)
        for(int k=1; k<=3; ++k)
            res(i, j) = (res(i, j) + mx[i][k] * arg(k, j)) % MOD;

        return res; }

    void exp(int e) {
        MX3 base = *this;
        for(int i=1; i<=3; ++i)
        for(int j=1; j<=3; ++j)
            mx[i][j] = int(i == j);

        for(; e; e>>=1) {
            if(e & 1)
                *this =(*this) * base;
            base = base * base; } } };

int main(void) {
    ifstream fi("iepuri.in");
    ofstream fo("iepuri.out");
    int tst, a, b, c, x, y, z, n;

    fi >> tst;
    while(tst--) {
        MX3 aux;

        fi >> x >> y >> z >> a >> b >> c >> n;
        n-= 2;

        aux(1, 1) = a;
        aux(2, 1) = b;
        aux(3, 1) = c;
        aux(1, 2) = 1;
        aux(2, 3) = 1;
        aux.exp(n);

        fo << (z*aux(1, 1) + y*aux(2, 1) + x*aux(3, 1)) % MOD << '\n'; }


    return 0; }