-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKitamasa.cpp
95 lines (83 loc) · 2.09 KB
/
Kitamasa.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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9+7;
const ll K = 1001;
// (0-indexed)
// a[i] = a_i, c[i] = c_i
// a_{i+k} = c_i*a_i + c_{i+1}*a_{i+1} + ... + c_{i+k-1}*a_{i+k-1}
ll c[K], a[K];
// C(N, *) -> C(N+1, *)
void nxt(ll k, ll c0[K], ll c1[K]) {
c1[0] = (c0[k - 1] * c[0]) % MOD;
for (int i = 1; i < k; i++) {
c1[i] = (c0[i - 1] + c0[k - 1] * c[i]) % MOD;
}
}
// C(N, *) -> C(2N, *)
void dbl(ll k, ll c0[K], ll c1[K]) {
ll cs[K][K];
for (int i = 0; i < k; i++) {
cs[0][i] = c0[i];
}
for (int i = 0; i < k - 1; i++) {
nxt(k, cs[i], cs[i + 1]);
}
for (int i = 0; i < k; i++) {
c1[i] = 0;
for (int j = 0; j < k; j++) {
c1[i] += cs[0][j] * cs[j][i];
c1[i] %= MOD;
}
}
}
// calculate a_m
ll solve(ll m, ll k) {
if (m == 0) {
return a[0];
}
ll c0[K], c1[K];
for (int i = 0; i < k; i++) {
c0[i] = 0;
}
c0[1] = 1;
// C(0, *) -> C(m, *) in O(k^2log(m))
int p = 62;
while (((m >> --p) & 1) == 0); // most significant 1 bit
while (p-- > 0) {
dbl(k, c0, c0);
if ((m >> p) & 1) {
nxt(k, c0, c1);
for (int j = 0; j < k; j++) {
c0[j] = c1[j];
}
}
}
// get result, a_m
ll res = 0;
for (int i = 0; i < k; i++) {
res += (c0[i] * a[i]) % MOD;
res %= MOD;
}
return res;
}
int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
// k : 점화식이 몇 개의 이전 항으로 구성되어 있는가
// n : 몇번째 항 구해야 하는가
int k, n; cin >> k >> n;
// c[], a[] 문제에 따라 적절히 구성
// 이건 https://atcoder.jp/contests/tdpc/tasks/tdpc_fibonacci 문제의 예시
for (int i = 0; i < k; i++) {
c[i] = 1;
}
for (int i = 0; i < k; i++) {
a[i] = 1;
}
// zero-based index 주의
cout << solve(n - 1, k) << '\n';
return 0;
}