forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
73 lines (60 loc) · 1.44 KB
/
B.cpp
File metadata and controls
73 lines (60 loc) · 1.44 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
const int BASE = int(1e9) + 9;
int sz, m, c[11];
void addMod(long long &x, long long y)
{
x = (x + y) % BASE;
}
struct Matrix
{
long long a[88][88];
Matrix()
{
memset(a, 0, sizeof a);
}
Matrix operator * (const Matrix &u)
{
Matrix res;
for (int i = 0; i < sz; i++)
for (int j = 0; j < sz; j++)
for (int k = 0; k < sz; k++)
addMod(res.a[i][j], a[i][k] * u.a[k][j]);
return res;
}
} matrix[66];
void addCoef(int dif, int cur, int prev, long long val)
{
addMod(matrix[0].a[(dif - 1) * m + prev][cur], val);
}
int main()
{
long long n;
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> c[i];
sz = m * m;
// add 2 x 1
addCoef(1, 0, 0, c[2]);
// add 1 x i and 1 x j to (u, 0)
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++)
addCoef(min(i, j), abs(i - j), 0, 1LL * c[i] * c[j] % BASE);
// add 1 x j to (u, i)
for (int i = 1; i < m; i++)
for (int j = 1; j <= m; j++)
addCoef(min(i, j), abs(i - j), i, c[j]);
for (int i = m; i < sz; i++)
addMod(matrix[0].a[i - m][i], 1);
for (int i = 1; i <= 60; i++)
matrix[i] = matrix[i - 1] * matrix[i - 1];
Matrix ans;
ans.a[0][0] = 1;
for (int i = 60; i >= 0; i--)
if (n >> i & 1)
{
ans = ans * matrix[i];
n ^= 1LL << i;
}
cout << ans.a[0][0] << endl;
}