-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix expo.cpp
109 lines (81 loc) · 1.69 KB
/
Matrix expo.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
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long i64;
struct Matrix
{
i64 r, c, a[20][20];
Matrix()
{
memset(a, 0,sizeof(a));
}
Matrix(i64 n, i64 m)
{
r = n;
c = m;
memset(a, 0,sizeof(a));
}
Matrix(i64 n)
{
r = c = n;
memset(a, 0, sizeof(a));
}
Matrix operator * (Matrix b)
{
Matrix res(r, b.c);
for(int i=0; i<r; i++)
{
for(int j=0; j<b.c; j++)
{
i64 temp = 0;
for(int k=0; k<c; k++)
{
temp = (temp + (a[i][k]*b.a[k][j]));
}
res.a[i][j] = temp;
}
}
return res;
}
Matrix operator ^(i64 n)
{
Matrix res(r);
res.identity();
Matrix p(r, c);
for(int i = 0; i<r; i++)
for(int j=0; j<c; j++) p.a[i][j] = a[i][j];
while(n)
{
if( n & 1 ) res = res * p;
p = p * p;
n >>= 1;
}
return res;
}
void identity()
{
memset(a, 0, sizeof(a));
for(int i=0; i<20; i++) a[i][i] = 1;
}
};
int main()
{
int test, cs = 0;
scanf("%d", &test);
while(test--)
{
i64 p, q, n;
cin>>p>>q>>n;
Matrix a(2);
a.a[0][0] = p;
a.a[0][1] = -q;
a.a[1][0] = 1;
a.a[1][1] = 0;
a = a^(n-1);
i64 ff = a.a[0][0] * p;
i64 ss = a.a[0][1] * 2;
i64 res = ff+ss;
if(n==0) res = 2;
cout<<"Case "<<++cs<<": "<<res<<endl;
}
return 0;
}