-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1096 - nth Term.cpp
98 lines (87 loc) · 2 KB
/
1096 - nth Term.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <bits/stdc++.h>
using namespace std;
int mat[4][4];
const int mod = 10007;
int n, a, b, c;
void bigmod (int e)
{
if (e <= 1) return;
bigmod (e >> 1);
int tt[4][4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
int sum = 0;
for (int k = 0; k < 4; k++) sum += (mat[i][k] * mat[k][j]) %mod;
tt[i][j] = sum %mod;
}
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
mat[i][j] = tt[i][j];
if (e %2 == 1)
{
int temp[4][4];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) temp[i][j] = 0;
temp[0][0] = a;
temp[0][1] = 0;
temp[0][2] = b;
temp[0][3] = c;
temp[1][0] = 1;
temp[2][1] = 1;
temp[3][3] = 1;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
int sum = 0;
for (int k = 0; k < 4; k++) sum += (tt[i][k] * temp[k][j]) %mod;
mat[i][j] = sum %mod;
}
}
}
/*
printf("Iteration %d\n\n",c);
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}*/
}
int main()
{
int tes, o = 0;
scanf("%d", &tes);
while(tes--)
{
o++;
scanf("%d %d %d %d", &n, &a, &b, &c);
if(n <= 2){
printf("Case %d: %d\n",o,0);
continue;
}
if(n == 3)
{
printf("Case %d: %d\n", o , c%mod);
continue;
}
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
mat[i][j] = 0;
mat[0][0] = a;
mat[0][1] = 0;
mat[0][2] = b;
mat[0][3] = c;
mat[1][0] = 1;
mat[2][1] = 1;
mat[3][3] = 1;
bigmod(n-2);
int res = mat[0][3] % mod;
printf("Case %d: %d\n", o, res % mod);
}
}