Cod sursa(job #3212634)

Utilizator dorufDoru Floare doruf Data 12 martie 2024 00:01:04
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <bits/stdc++.h>
using namespace std;

const int K = 3;
const int Mod = 666013;

void add(int& x, long long y) {
	y %= Mod;
	x = (x + y) % Mod;
}

void mult(int a[K][K], int b[K][K]) {
	int res[K][K];
	memset(res, 0, sizeof res);

	for (int i = 0; i < K; ++i)
		for (int j = 0; j < K; ++j)
			for (int k = 0; k < K; ++k)
				add(res[i][j], 1LL * a[i][k]*b[k][j]);

	memcpy(a, res, sizeof res);
}

void pwr(int a[K][K], int n) {
	int res[K][K];
	for (int i = 0; i < K; ++i)
		for (int j = 0; j < K; ++j)
			res[i][j] = (i == j);

	while (n) {
		if (n & 1) mult(res, a);
		mult(a, a);
		n >>= 1;
	}
	memcpy(a, res, sizeof res);
}

void solve() {
	int x, y, z, a, b, c, n;
	cin >> x >> y >> z >> a >> b >> c >> n;

	int m[K][K] = {
		{0, 1, 0},
		{0, 0, 1},
		{c, b, a}
	};
	pwr(m, n);

	int ans = 0;
	add(ans, 1LL * m[0][0] * x);
	add(ans, 1LL * m[0][1] * y);
	add(ans, 1LL * m[0][2] * z);

	cout << ans << '\n';
}

int main() {
	freopen("iepuri.in", "r", stdin);
	freopen("iepuri.out", "w", stdout);

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t; 
	for (cin >> t; t; --t)
		solve();
}