Pagini recente » Cod sursa (job #2695521) | Cod sursa (job #2873328) | Cod sursa (job #429310) | Cod sursa (job #236634) | Cod sursa (job #3151493)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
struct mat3x3{
int m[4][4];
} init;
int a,b,c;
mat3x3 genm(){
mat3x3 rez;
for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) rez.m[i][j] = 0;
return rez;
}
mat3x3 prod(mat3x3 m1, mat3x3 m2){
mat3x3 rez = genm();
for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++){
for(int k = 0; k < 3; k++){
rez.m[i][j] += m1.m[i][k] * m2.m[k][j];
}
}
return rez;
}
mat3x3 exp(int n){
mat3x3 rez = genm();
if(n == 1){
return init;
}
if(n & 1){
rez = exp(n - 1);
return prod(rez,init);
}
else{
rez = exp(n / 2);
return prod(rez,rez);
}
}
void print(mat3x3 x){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++) fout << x.m[i][j] << " ";
fout << "\n";
}
}
void geninit(){
init.m[0][0] = 0;
init.m[0][1] = 0;
init.m[0][2] = c;
init.m[1][0] = a;
init.m[1][1] = 0;
init.m[1][2] = b;
init.m[2][0] = 0;
init.m[2][1] = 1;
init.m[2][2] = a;
}
int main()
{
int n,i,x,y,z,t;
fin >> t;
for(int e = 1; e <= t; e++){
fin >> x >> y >> z >> a >> b >> c >> n;
geninit();
mat3x3 r = exp(n - 2);
//print(r);
fout << z * r.m[2][2] + y * r.m[1][2] + x * r.m[0][2] << "\n";
//fout << "\n\n\n";
}
return 0;
}
/*
(z0,z1,z2)
(0 0 c
1 0 b
0 1 a)
(0 0 c
1 0 b
0 1 a)
(z1,z2,z3)
z0,z1,z2
(0 c ac
0 b c+ab
1 a b+a2
z2,z3,z4
*/