Cod sursa(job #3262058)

Utilizator CalinHanguCalinHangu CalinHangu Data 8 decembrie 2024 15:10:11
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.02 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>

#define ll long long
#define pii pair<int, int>
#define x first
#define y second

#define int ll

using namespace std;

ifstream in("file.in");
ofstream out("file.out");

const int MOD = 666013;
const char nl = '\n';
const int NMAX = 2 * 1e5;
const int MAX = 1e5;
const int INF = 1e9;

int v[NMAX], mat[5][5], mat2[5][5], aux[5][5];

void reset(int m[5][5]){
    for(int i = 0; i < 3; ++i) {
        for(int j = 0; j < 3; ++j) {
            m[i][j] = 0;
        }
    }
}

void multiplyMatrix(int a[5][5], int b[5][5], int c[5][5]) {
    for (int i = 0; i < 3; ++i) {
		for (int j = 0; j < 3; ++j) {
			c[i][j] = 0;
		}
	}

	for (int i = 0; i < 3; ++i) {
		for (int j = 0; j < 3; ++j) {
			for (int k = 0; k < 3; ++k) {
				c[i][j] += a[i][k] * b[k][j] % MOD;
				c[i][j] %= MOD;
			}
		}
	}
}

void logPow(int p){
    while(p) {
        if(p & 1) {
            multiplyMatrix(mat, mat2, aux);
            for(int i = 0; i < 3; ++i){
                for(int j = 0; j < 3; ++j){
                    mat2[i][j] = aux[i][j];
                }
            }
        }

        multiplyMatrix(mat, mat, aux);
        for(int i = 0; i < 3; ++i){
            for(int j = 0; j < 3; ++j){
                    mat[i][j] = aux[i][j];
            }
        }
        p >>= 1;
    }
}

void solve(){
    int x, y, z, a, b, c, n;
    in >> x >> y >> z >> a >> b >> c >> n;
    mat[0][0] = 0; mat[0][1] = 0; mat[0][2] = c;
    mat[1][0] = 1; mat[1][1] = 0; mat[1][2] = b;
    mat[2][0] = 0; mat[2][1] = 1; mat[2][2] = a;

    for(int i = 0; i < 3; ++i) {
        mat2[i][i] = 1;
    }

    logPow(n);

    out << (((x * mat2[0][0]) % MOD + (y * mat2[1][0]) % MOD) % MOD + (z * mat2[2][0]) % MOD) % MOD << '\n';

    reset(mat2);

}

signed main() {
    int t;
    in >> t;
    //t = 1;
    while(t--)
        solve();
}