-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy path1065 - Number Sequence.cpp
65 lines (63 loc) · 1.42 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
#include <string.h>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
typedef int T;
#define MAX 2
int MOD;
struct Matrix{
T mat[MAX][MAX];
int N;
Matrix():N(2){}
Matrix(int n):N(n){}
Matrix multiply(Matrix B){
Matrix C;
for(int r = 0; r < N; r++)
for(int c = 0; c < N; c++){
C.mat[r][c] = 0;
for(int k = 0 ; k < N; k++)
C.mat[r][c] = (C.mat[r][c] + (mat[r][k]*B.mat[k][c])%MOD)%MOD;
}
return C;
}
Matrix power(int n){
if(n == 1) return (*this);
Matrix P;
P = power(n/2);
P = P.multiply(P);
if(n & 1) P = multiply(P);
return P;
}
void init(){
mat[0][0] = 0;
mat[0][1] = 1;
mat[1][0] = 1;
mat[1][1] = 1;
}
};
int main(){
//freopen("input.txt","r",stdin);
int a, b, n,out, m;
Matrix ans, base;
int T, tc = 1;
scanf("%d", &T);
while( T-- > 0){
scanf("%d %d %d %d", &a, &b, &n, &m);
MOD = pow(10,m);
a %= MOD;
b %= MOD;
if(n < 2){
if(n == 0) out = a;
else out = b;
}
else{
base.init();
ans = base.power(n-1);
out = ( (ans.mat[1][0]*a)%MOD + (ans.mat[1][1]*b)%MOD ) % MOD;
}
printf("Case %d: %d\n", tc++, out);
}
return 0;
}