-
Notifications
You must be signed in to change notification settings - Fork 2
/
1065 - Number Sequence.cpp
77 lines (77 loc) · 1.76 KB
/
1065 - Number Sequence.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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int dim = 2;// dimensom
ll mod;
struct Matrix
{
ll val[dim+1][dim+1];
};
void init(Matrix& ob)
{
ob.val[0][0] = 0; ob.val[0][1] = 1;
ob.val[1][0] = 1; ob.val[1][1] = 1;
}
Matrix multiply(const Matrix& a, const Matrix& b)
{
Matrix ret;
for(int i=0; i<dim; i++){
for(int j=0; j<dim; j++){
ret.val[i][j] = 0;
for(int k=0; k<dim; k++){
ret.val[i][j] = (ret.val[i][j] + (a.val[i][k]*b.val[k][j])%mod)%mod;
}
}
}
return ret;
}
Matrix identity()
{
Matrix ret;
for(int i=0; i<dim; i++){
for(int j=0; j<dim; j++){
ret.val[i][j] = (i==j);
}
}
return ret;
}
//Matrix bigMod(Matrix &base, ll power)
//{
// Matrix now = identity();
// while(power>0){
// if(power%2==1){
// now = multiply(base, now);
// }
// base = multiply(base, base);
// power/=2;
// }
// return now;
//}
Matrix bigMod(Matrix base, ll power)
{
if(power==0) return identity();
Matrix x = bigMod(base, power/2);
x = multiply(x,x);
if(power%2==1) x = multiply(x,base);
return x;
}
int main()
{
Matrix mat;
int tc,cs=1,n,a,b,m;
scanf("%d",&tc);
while(tc--){
init(mat);
scanf("%d%d%d%d",&a,&b,&n,&m);
mod = 1;
for(int i=1; i<=m; i++){
mod*=10;
}
mat = bigMod(mat, n);
// cout<<mat.val[0][0]<<" "<<mat.val[0][1]<<endl;
// cout<<mat.val[1][0]<<" "<<mat.val[1][1]<<endl;
int ans = ((mat.val[0][0]*a)%mod + (mat.val[0][1]*b)%mod)%mod;
printf("Case %d: %d\n",cs++,ans);
}
return 0;
}