-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12728_exponentation_square.cpp
73 lines (72 loc) · 1.38 KB
/
12728_exponentation_square.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
// 210214 #12728, #12925 Numbers Platinum I
// Platinum I ~ Diamond V
// exponentation_square, Number of Theory
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
const int MOD = 1000;
ll N = 2, B;
// matrix exponentation_sqaure(o(logn))
class Matrix {
public:
vector<vector<int>> v;
Matrix() {
v = vector<vector<int>>(N, vector<int>(N, 0));
}
Matrix operator *(const Matrix O) const {
Matrix R;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
R.v[i][j] += v[i][k] * O.v[k][j];
R.v[i][j] %= MOD;
}
}
}
return R;
}
};
Matrix power(Matrix M, ll k) {
Matrix R;
if (!k) {
for (int i = 0; i < N; i++) {
R.v[i][i] = 1;
return R;
}
}
if (k == 1)
return M;
R = power(M, k / 2);
R = R * R;
if (k % 2)
R = R * M;
return R;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int t;
cin >> t;
for (int i = 1; i <= t; i++) {
cout << "Case #" << i << ": ";
ll n;
cin >> n;
string ans;
Matrix A;
// like pibonacci
A.v[0][0] = 6; A.v[0][1] = -4;
A.v[1][0] = 1; A.v[1][1] = 0;
A = power(A, n - 1);
ll num = A.v[1][0] * 28 + A.v[1][1] * 6;
// % 1000, + 1000 % 1000
ans = to_string(((num - 1) % MOD + MOD )% MOD);
for (int j = 0; j < 3 - ans.length(); j++) {
cout << "0";
}
cout << ans << "\n";
}
}