Pagini recente » Cod sursa (job #2450845) | Cod sursa (job #2010302) | Cod sursa (job #2359098) | Cod sursa (job #289849) | Cod sursa (job #140940)
Cod sursa(job #140940)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
long long T,
X, Y, Z,
A, B, C,
N;
long long M[3][3];
void matprint(long long M[3][3]) {
for (int i(0); i < 3; ++i) {
for (int j(0); j < 3; ++j)
cout << M[i][j] << " ";
cout << endl;
}
cout << endl;
}
void matmul(long long a[3][3], long long b[3][3]) {
long long c[3][3];
for (int i(0); i < 3; ++i)
for (int j(0); j < 3; ++j) {
c[i][j] = 0;
for (int k(0); k < 3; ++k)
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % 666013;
}
memcpy(a, c, 9*sizeof(c[0][0]));
}
void bfpow(long long M[3][3], long long N) {
long long a[3][3];
memcpy(a, M, 9*sizeof(M[0][0]));
while (N--)
matmul(M, a);
}
void logpow(long long M[3][3], long long N) {
long long a[3][3];
memcpy(a, M, 9*sizeof(M[0][0]));
memset(M, 0, 9*sizeof(M[0][0]));
M[0][0] = 1;
M[1][1] = 1;
M[2][2] = 1;
long long i = (long long)1<<32;
while (i) {
matmul(M, M);
if (N & i)
matmul(M, a);
i >>= 1;
}
}
int main(int argc, char *argv[]) {
ifstream fin("iepuri.in");
fin >> T;
ofstream fout("iepuri.out");
while (T--) {
fin >> X >> Y >> Z >> A >> B >> C >> N;
M[0][0] = A; M[0][1] = B; M[0][2] = C;
M[1][0] = 1; M[1][1] = 0; M[1][2] = 0;
M[2][0] = 0; M[2][1] = 1; M[2][2] = 0;
if (N == 1)
fout << X % 666013 << endl;
else if (N == 2)
fout << Y % 666013 << endl;
else if (N == 3)
fout << Z % 666013 << endl;
else {
logpow(M, N - 2);
//matprint(M);
//cout << X << " " << Y << " " << Z << endl;
fout << (M[0][0] * Z + M[0][1] * Y + M[0][2] * X) % 666013 << endl;
}
}
fout.close();
fin.close();
return 0;
}