Blog of RuSun

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

P5363 [SDOI2019]移动金币

P5363 [SDOI2019]移动金币

阶梯 Nim 。先手必败当且仅当奇数位置异或和为 $0$ ,逐位考虑,DP 即可。最后剩下的位置给偶数位或者放弃,插板即可。

查看代码
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
#include <cstdio>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
if (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('0' + x % 10);
}
typedef long long L;
const int N = 2e5 + 10, mod = 1e9 + 9;
int n, m, f[2][N];
int inv[N], fact[N], ifact[N];
void add (int &x, L k) { x = (x + k) % mod; }
void init ()
{
inv[1] = 1;
for (int i = 2; i < N; ++i)
inv[i] = -(L)(mod / i) * inv[mod % i] % mod;
fact[0] = ifact[0] = 1;
for (int i = 1; i < N; ++i)
{
fact[i] = (L)fact[i - 1] * i % mod;
ifact[i] = (L)ifact[i - 1] * inv[i] % mod;
}
}
int C (int a, int b) { return a < b ? 0 : (L)fact[a] * ifact[b] % mod * ifact[a - b] % mod; }
int main ()
{
init();
read(n, m); n -= m;
int a = m + 1 >> 1, b = m - a;
int res = C(n + m, m);
bool op = false;
f[op][n] = 1;
for (int k = 1; k <= n; k <<= 1, op ^= 1)
{
for (int i = 0; i <= n; ++i) f[op ^ 1][i] = 0;
for (int i = 0; i <= n; ++i) if (f[op][i])
for (int j = 0; j * k <= i && j <= a; j += 2)
add(f[op ^ 1][i - j * k], (L)f[op][i] * C(a, j));
}
for (int i = 0; i <= n; ++i)
add(res, -(L)f[op][i] * C(i + b, b));
write((res + mod) % mod);
return 0;
}