Pagini recente » Cod sursa (job #371407) | Cod sursa (job #2329709) | Cod sursa (job #2559160) | Cod sursa (job #428125) | Cod sursa (job #3275393)
#include <bits/stdc++.h>
#define mod 666013
using namespace std;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
int x,y,z,aa,bb,cc,n,a[3][3],b[3][3],p[3][3];
/**
z y x
f(n) = f(n-1) * a + f(n-2) * b + f(n-2) * c
F(n) = F(n-1) * M
F(n-1) = F(n-2) * M^2;
---
F(2) = F(2) * M^(n-2)
F(2) = ( z y x )
(z y x) * 0 0 a = y + x + az +yb + xc
1 0 b
0 1 c
0 0 a
a = 1 0 b
0 1 c
1 0 0
p = I3 = 0 1 0
0 0 1
p = a^n
*/
void Prod(int a[3][3], int b[3][3], int c[3][3])
{
int i,j,k;
for (i=0;i<3;i++)
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]=(c[i][j]+1LL*a[i][k]*b[k][j])%mod;
}
}
void Copie(int a[3][3],int b[3][3])
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=b[i][j];
}
void ExpoLog(int a[3][3], int n)
{
p[0][0]=p[1][1]=p[2][2]=1;
p[0][1]=p[0][2]=p[1][0]=p[1][2]=p[2][0]=p[2][1]=0;
while(n>0)
{
if(n%2==1)
{
Prod(p,a,b);
Copie(p,b);
}
n/=2;
Prod(a,a,b);
Copie(a,b);
}
}
int main()
{
int t;
fin>>t;
for(int q=1;q<=t;q++)
{
fin>>x>>y>>z>>aa>>bb>>cc>>n;
a[0][2]=cc;
a[1][2]=bb;
a[2][2]=aa;
a[1][0]=a[2][1]=1;
a[0][0]=a[0][1]=a[1][1]=a[2][0]=0;
ExpoLog(a,n-2);
fout<<(1LL*x*p[0][2] + 1LL*y*p[1][2] + 1LL*z*p[2][2])%mod<<endl;
}
return 0;
}