-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVa10655.cpp
More file actions
52 lines (47 loc) · 1.15 KB
/
UVa10655.cpp
File metadata and controls
52 lines (47 loc) · 1.15 KB
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
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 2
typedef long long ll;
struct Matrix {ll mat[MAX_N][MAX_N];};
Matrix matMul(Matrix a, Matrix b)
{
Matrix ans; int i, j, k;
for (i = 0; i < MAX_N; ++i)
for (j = 0; j < MAX_N; ++j)
for (ans.mat[i][j] = k = 0; k < MAX_N; ++k)
ans.mat[i][j] += a.mat[i][k] * b.mat[k][j];
return ans;
}
Matrix matPow(Matrix base, int p)
{
Matrix ans; int i, j;
for (i = 0; i < MAX_N; ++i) for (j = 0; j < MAX_N; ++j)
ans.mat[i][j] = (i == j);
while (p)
{
if (p & 1) ans = matMul(ans, base);
base = matMul(base, base);
p >>= 1;
}
return ans;
}
int main() {
//ios_base::sync_with_stdio(false);
int p, q, n; cin >> p >> q;
while (cin >> n)
{
Matrix strt;
strt.mat[0][0] = p;
strt.mat[0][1] = -q;
strt.mat[1][0] = 1;
strt.mat[1][1] = 0;
Matrix other;
other.mat[0][0] = p;
other.mat[1][0] = 2;
strt = matPow(strt, n);
strt = matMul(strt, other);
cout << strt.mat[1][0] << endl;
cin >> p >> q;
}
return 0;
}