Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P5364 [SNOI2017]礼物

P5364 [SNOI2017]礼物

记 $s _ i$ 为答案的前缀和,那么有 $s _ i = 2 s _ {i - 1} + i ^ k$ 。考虑如何加快转移,如果将可以将 $i ^ k$ 以线性递推的方式表达出来即可矩阵转移。注意到 $(i + 1) ^ k = \sum _ {j = 0} ^ k \binom k j i ^ j$ ,那么同时维护 $i ^ 0, i ^ 1, \ldots i ^ k$ ,转移矩阵中为 $binom k j$ ,可以矩阵递推了。

查看代码
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
#include <cstdio>
using namespace std;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...rest>
void read(Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write(Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
typedef long long LL;
const int N = 20, mod = 1e9 + 7;
struct Matrix
{
int n, m, w[N][N];
Matrix (int _n, int _m)
{
n = _n, m = _m;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
w[i][j] = i == j;
}
Matrix operator * (Matrix _) const
{
Matrix res(n, _.m);
for (int i = 0; i < n; ++i)
for (int j = 0; j < _.m; ++j)
{
res.w[i][j] = 0;
for (int k = 0; k < m; ++k)
res.w[i][j] = (res.w[i][j] + (LL)w[i][k] * _.w[k][j]) % mod;
}
return res;
}
};
LL n;
int m, C[N][N];
void init ()
{
for (int i = 0; i <= m; ++i) C[i][0] = 1;
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= m; ++j)
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
Matrix binpow (Matrix b, LL k)
{
Matrix res(b.n, b.m);
for (; k; k >>= 1, b = b * b)
if (k & 1) res = res * b;
return res;
}
int main ()
{
read(n, m);
init();
static Matrix A(1, m + 2), B(m + 2, m + 2);
A.w[0][m + 1] = 0;
for (int i = 0; i <= m; ++i) A.w[0][i] = 1;
for (int i = 0; i <= m; ++i)
for (int j = i; j <= m; ++j)
B.w[i][j] = C[j][i];
B.w[m][m + 1] = 1, B.w[m + 1][m + 1] = 2;
write(((A * binpow(B, n)).w[0][m + 1] - (A * binpow(B, n - 1)).w[0][m + 1] + mod) % mod);
return 0;
}