Pagini recente » Cod sursa (job #1380030) | Cod sursa (job #2273947) | Cod sursa (job #2240240) | Cod sursa (job #2288926) | Cod sursa (job #1387739)
#include <iostream>
#include <fstream>
using namespace std;
int mat[4][4];
ifstream in("iepuri.in");
ofstream out("iepuri.out");
inline void ExpLog(int n)
{
/// mat = mat^n
int put[4][4], aux[4][4];
long long nr;
int i, j, k;
for (i=1; i<=3; i++)
for (j=1; j<=3; j++)
put[i][j] = 0;
put[1][1] = put[2][2] = put[3][3] = 1;
while (n>0)
{
if (n&1)
{
for (i=1; i<=3; i++) // liniile din prima
{
for (j=1; j<=3; j++) // coloanele din a 2a
{
nr = 0LL;
for (k=1; k<=3; k++)
nr += 1LL * put[i][k] * mat[k][j];
aux[i][j] = nr%666013;
}
}
for (i=1; i<=3; i++)
for (j=1; j<=3; j++)
put[i][j] = aux[i][j];
//put = put*mat;
n--;
}
for (i=1; i<=3; i++) // liniile din prima
{
for (j=1; j<=3; j++) // coloanele din a 2a
{
nr = 0LL;
for (k=1; k<=3; k++)
nr += 1LL * mat[i][k] * mat[k][j];
aux[i][j] = nr%666013;
}
}
for (i=1; i<=3; i++)
for (j=1; j<=3; j++)
mat[i][j] = aux[i][j];
//mat = mat*mat;
n>>=1;
}
for (i=1; i<=3; i++)
for (j=1; j<=3; j++)
mat[i][j] = put[i][j];
}
inline long long Solve(int x, int y, int z, int a, int b, int c, int n)
{
int i, j;
for (i=1; i<=3; i++)
for (j=1; j<=3; j++)
mat[i][j] = 0;
mat[1][2] = mat[2][3] = 1;
mat[3][1] = c, mat[3][2] = b, mat[3][3] = a;
ExpLog(n);
long long ans;
ans = ((1LL*mat[1][1]*x) + (1LL*mat[1][2]*y) + (1LL*mat[1][3]*z))%666013;
return ans;
}
int main()
{
int t, x, y, z, a, b, c, n;
in>>t;
for(i=1; i<=t; i++)
{
in>>x>>y>>z>>a>>b>>c>>n;
out<<Solve(x, y, z, a, b, c, n)<<"\n";
}
f.close();
g.close();
return 0;
}