Pagini recente » Diferente pentru monthly-2012/runda-9/solutii intre reviziile 15 si 14 | Cod sursa (job #360147)
Cod sursa(job #360147)
#include <stdio.h>
#define NR 666013
#define N 1<<2
int t,x,y,z,a,b,c,n;
long long mat[N][N],rez[N][N];
void read()
{
scanf("%d%d%d%d%d%d%d",&x,&y,&z,&a,&b,&c,&n);
mat[1][1]=0; mat[1][2]=1; mat[1][3]=0;
mat[2][1]=0; mat[2][2]=0; mat[2][3]=1;
mat[3][1]=c; mat[3][2]=b; mat[3][3]=a;
}
void init()
{
int i,j;
for (i=1; i<=3; i++)
for (j=1; j<=3; j++)
rez[i][j]=(i==j) ? 1 : 0;
}
long long calcul(int x,int y,long long A[N][N],long long B[N][N])
{
int i;
long long s=0;
for (i=1; i<=3; i++)
{
s+=A[x][i]*B[i][y];
if (s>=NR)
s%=NR;
}
return s;
}
void mul(long long A[N][N],long long B[N][N])
{
int i,j,C[N][N];
for (i=1; i<=3; i++)
for (j=1; j<=3; j++)
C[i][j]=calcul(i,j,A,B);
for (i=1; i<=3; i++)
for (j=1; j<=3; j++)
A[i][j]=C[i][j];
}
void solve()
{
init();
while (n)
{
if (n&1)
mul(rez,mat);
n>>=1;
mul(mat,mat);
}
}
void mul2()
{
long long s=0;
s+=rez[1][1]*x;
if (s>=NR)
s%=NR;
s+=rez[1][2]*y;
if (s>=NR)
s%=NR;
s+=rez[1][3]*z;
if (s>=NR)
s%=NR;
printf("%lld\n",s);
}
int main()
{
freopen("iepuri.in","r",stdin);
freopen("iepuri.out","w",stdout);
scanf("%d",&t);
int i;
for (i=1; i<=t; i++)
{
read();
solve();
mul2();
}
return 0;
}