Pagini recente » Cod sursa (job #1979502) | Cod sursa (job #141032) | Cod sursa (job #810927) | Cod sursa (job #1915846) | Cod sursa (job #1141698)
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
const int MOD = 666013;
typedef vector<vector<int>> Matrix;
Matrix log_pow(Matrix base, int P);
Matrix matrix_multiply(const Matrix &a, const Matrix &b);
int main() {
int t;
fin >> t;
int x, y, z, a, b, c, n;
Matrix M, result;
for (int i = 1; i <= t; ++i) {
fin >> x >> y >> z >> a >> b >> c >> n;
M = {{0, 1, 0},
{0, 0, 1},
{c, b, a}};
M = log_pow(M, n - 3);
result = {{x}, {y}, {z}};
result = matrix_multiply(M, result);
fout << result[2][0] % MOD << '\n';
}
return 0;
}
inline Matrix log_pow(Matrix base, int P) {
Matrix result = base;
for (; P; P >>= 1) {
if (P & 1)
result = matrix_multiply(result, base);
base = matrix_multiply(base, base);
}
return result;
}
inline Matrix matrix_multiply(const Matrix &a, const Matrix &b) {
int n = a.size();
int m = b.size();
int p = b[0].size();
Matrix result(n, vector<int>(p));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < p; ++j) {
for (int k = 0; k < m; ++k) {
result[i][j] += (1LL * a[i][k] * b[k][j]) % MOD;
}
}
}
return result;
}