Pagini recente » Cod sursa (job #2262937) | Cod sursa (job #415500) | Cod sursa (job #381686) | Cod sursa (job #1616916) | Cod sursa (job #2326975)
#include <fstream>
using namespace std;
ifstream in("iepuri.in");
ofstream out("iepuri.out");
const int MOD = 666013;
int x, y, z, a, b, c, n;
long long inm[4][4], rez[4][4], aux[4][4];
void init(){
inm[1][1] = a;
inm[1][2] = b;
inm[1][3] = c;
inm[2][1] = inm[3][2] = 1;
inm[2][2] = inm[2][3] = inm[3][1] = inm[3][3] = 0;
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
rez[i][j] = 0;
rez[1][1] = rez[2][2] = rez[3][3] = 1;
}
void multiplyMatrix(long long mat[][4], long long im[][4]){
///multiplies matrix "mat" with "im"
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++){
aux[i][j] = 0;
for(int p=1;p<=3;p++)
aux[i][j] = (aux[i][j] + 1LL * mat[i][p] * im[p][j]) % MOD;
}
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
mat[i][j] = aux[i][j];
}
void fastExpMatrix(int n){
///"rez" will be "inm" raised to the power of n
init();
for(;n>0;n>>=1){
if(n & 1)
multiplyMatrix(rez,inm);
multiplyMatrix(inm,inm);
}
}
int main()
{
int T;
in>>T;
for(int q=1;q<=T;q++){
in>>x>>y>>z>>a>>b>>c>>n;
fastExpMatrix(n-2);
out<<(rez[1][1] * z + rez[1][2] * y + rez[1][3] * x) % MOD<<"\n";
}
in.close();
out.close();
return 0;
}