-
Notifications
You must be signed in to change notification settings - Fork 0
/
loj1096.cpp
82 lines (65 loc) · 1.92 KB
/
loj1096.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using namespace std;
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAX_N 4
#define M 10007
struct Matrix{
int mat[MAX_N][MAX_N];
};
Matrix matMul(Matrix a,Matrix b)
{
Matrix ans;
int i,j,k;
for(i=0;i<MAX_N;i++)
for(j=0;j<MAX_N;j++)
{
for(ans.mat[i][j]=k=0;k<MAX_N;k++)
{
ans.mat[i][j]=((ans.mat[i][j]%M)+(((a.mat[i][k]%M)*(b.mat[k][j]%M))%M))%M;
}
}
return ans;
}
Matrix matPow(Matrix BASE,int p)
{
if(p==1) return BASE;
Matrix X=matPow(BASE,p/2);
X=matMul(X,X);
if(p&1)X=matMul(X,BASE);
return X;
}
int main()
{
int t,a,b,n,Case=1,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&n,&a,&b,&c);
if(n<=2)
printf("Case %d: 0\n",Case++);
else{
Matrix base;
base.mat[0][0]=a;
base.mat[0][1]=0;
base.mat[0][2]=b;
base.mat[0][3]=1;
base.mat[1][0]=1;
base.mat[1][1]=0;
base.mat[1][2]=0;
base.mat[1][3]=0;
base.mat[2][0]=0;
base.mat[2][1]=1;
base.mat[2][2]=0;
base.mat[2][3]=0;
base.mat[3][0]=0;
base.mat[3][1]=0;
base.mat[3][2]=0;
base.mat[3][3]=1;
Matrix ans = matPow(base,n-2);
int output = ((ans.mat[0][3]%M)*(c%M))%M;
printf("Case %d: %d\n",Case++,output);
}
}
return 0;
}