-
Notifications
You must be signed in to change notification settings - Fork 2
/
1096 - nth term.cpp
111 lines (91 loc) · 2.15 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
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <iostream>
#include <map>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
#include <deque>
#include <vector>
#include <stdlib.h>
#include <string>
#include <string.h>
using namespace std;
#define ll long long
#define sl(n) scanf("%lld", &n)
#define si(n) scanf("%d", &n)
#define ss(n) scanf("%s", n)
ll A[4][4], res[4][4], temp[4][4];
void pwr(ll p)
{
ll i, j, k, s;
if (p == 0)
{
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
res[i][j] = 0;
res[0][0] = res[1][1] = res[2][2] = res[3][3] = 1;
return ;
}
pwr(p/2);
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
temp[i][j] = res[i][j];
if (p%2 == 0)
{
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
s = 0;
for (k = 0; k < 4; k++)
s = (s + (temp[i][k]%10007*(temp[k][j]%10007))%10007)%10007;
res[i][j] = s;
}
}
else
{
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
s = 0;
for (k = 0; k < 4; k++)
s = (s + (temp[i][k]%10007*(temp[k][j]%10007))%10007)%10007;
res[i][j] = s;
}
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
temp[i][j] = res[i][j];
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
s = 0;
for (k = 0; k < 4; k++)
s = (s + (A[i][k]%10007*(temp[k][j]%10007))%10007)%10007;
res[i][j] = s;
}
}
}
int main ()
{
ll cs, t, i, j, k, n, x, y, ans, q, a, b, c;
A[0][3] = A[1][0] = A[2][1] = A[3][3] = 1;
sl(t);
for (cs = 1; cs <= t; cs++)
{
sl(n);
sl(a);
sl(b);
sl(c);
A[0][0] = a;
A[0][2] = b;
if (n <= 2)
ans = 0;
else
{
pwr(n - 2);
ans = (res[0][3]%10007*(c%10007))%10007;
}
printf("Case %lld: %lld\n", cs, ans);
}
return 0;
}