-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1155.frankr.cpp
96 lines (79 loc) · 2.03 KB
/
1155.frankr.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
/*
Prob: 1155 - Z-funkcija
Idea: Recurrencias Lineales .. Matrix Exponentiation
Ref: Charla de Artimetica de Fidel Schsposnik (VI CaribCamp)
frankr@coj
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector< vector<int> > vv;
const int MOD = 10000;
struct matrix{
vv m;
matrix(int N){
m.resize(N);
for (int i = 0 ; i < N ; i++)
m[i].resize(N, 0);
}
matrix(const matrix& T){
int N = T.m.size();
m.resize(N);
for (int i = 0 ; i < N ; i++){
m[i].resize(N, 0);
for (int j = 0 ; j < N ; j++)
m[i][j] = T.m[i][j];
}
}
matrix mult(const matrix& T)const{
int N = m.size();
matrix C(N);
for (int i = 0 ; i < N ; i++)
for (int j = 0 ; j < N ; j++)
for (int k = 0 ; k < N ; k++)
C.m[i][j] = (C.m[i][j] + m[i][k] * T.m[k][j]) % MOD;
return C;
}
vector<int> mult(const vector<int>& T)const{
int N = m.size();
vector<int> r(N, 0);
for (int i = 0 ; i < N ; i++)
for (int j = 0 ; j < N ; j++)
r[i] = (r[i] + m[i][j] * T[j]) % MOD;
return r;
}
matrix Pow(long long E) const{
if (E == 1LL){
//matrix r(this);
//return r;
return *this;
}
if (E & 1LL){
matrix r(mult(Pow(E - 1)));
return r;
}
matrix r(Pow(E >> 1LL));
return r.mult(r);
}
};
int N;
matrix R(3);
vector<int> F(3);
int main()
{
//freopen("d.in", "r", stdin);
cin >> F[2] >> F[1] >> F[0];
cin >> R.m[0][2] >> R.m[0][1] >> R.m[0][0];
cin >> N;
R.m[1][0] = 1;
R.m[2][1] = 1;
if (N <= 3){
cout << F[3 - N] % MOD << '\n';
}
else{
matrix RE = R.Pow(N - 3);
vector<int> FN = RE.mult(F);
int sol = FN[0];
cout << sol << '\n';
}
return 0;
}