Pagini recente » Cod sursa (job #2782969) | Cod sursa (job #857837) | Cod sursa (job #1378577) | Cod sursa (job #151759) | Cod sursa (job #3348410)
#include <iostream>
#include <vector>
using namespace std;
typedef vector<vector<long long>> Matrix;
Matrix mul(Matrix A, Matrix B) {
int rows_A = A.size();
int cols_A = A[0].size();
int rows_B = B.size();
int cols_B = B[0].size();
Matrix C(rows_A, vector<long long>(cols_B, 0));
for (int i = 0; i < rows_A; i++) {
for (int j = 0; j < cols_B; j++) {
long long sum1 = 0;
for (int k = 0; k < cols_A; k++) {
sum1 = (sum1 + A[i][k] * B[k][j]) % 666013;
}
C[i][j] = sum1;
}
}
return C;
}
Matrix pow(Matrix A, long long n) {
if (n == 1) {
return A;
}
if (n % 2 == 0) {
Matrix D = pow(A, n / 2);
return mul(D, D);
} else {
return mul(pow(A, n - 1), A);
}
}
long long helper(long long x, long long y, long long z, long long a, long long b, long long c, long long n) {
if (n == 2) return z % 666013;
if (n == 1) return y % 666013;
if (n == 0) return x % 666013;
Matrix C(3, vector<long long>(3, 0));
C[1][0] = 1;
C[2][1] = 1;
C[0][2] = c % 666013;
C[1][2] = b % 666013;
C[2][2] = a % 666013;
n -= 2;
Matrix S(1, vector<long long>(3));
S[0][0] = x % 666013;
S[0][1] = y % 666013;
S[0][2] = z % 666013;
Matrix res = mul(S, pow(C, n));
return res[0][2];
}
int main() {
int T;
if (!(cin >> T)) return 0;
while (T--) {
long long x, y, z, a, b, c, n;
cin >> x >> y >> z >> a >> b >> c >> n;
cout << helper(x, y, z, a, b, c, n) << "\n";
}
return 0;
}