[BZOJ 4589]Hard Nim

题目大意

两人轮流拿n堆石子,每次拿一堆中的若干个.每堆石子的数目为小于m的质数.求使后取者胜的方案数模$10^9+7$的值.


思路

根据博弈论,可得到等价问题:有n个不大于m的质数,求异或和为0的方案数.

用FWT,异或后卷积最高次项前系数即方案数.


Code

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
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
typedef long long LL;
#define int long long
#define MOD 1000000007
const int INV = 5e8+4;
#define N 200000
int n,m,cnt,vis[N],prime[N / 5],a[N << 2];
inline void seive()
{
vis[1] = 1;
for(int i = 2;i < N;i++)
{
if(!vis[i]) prime[++cnt] = i;
for(int j = 1,k = prime[j];j <= cnt && i * (k = prime[j]) < N;j++)
{
vis[i * k] = 1;
if(!(i % k)) break;
}
}
}
inline LL qpow(LL a,int b)
{
LL ans = 1;
while(b)
{
if(b & 1) ans = 1LL * ans * a % MOD;
a = 1LL * a * a % MOD;
b >>= 1;
}
return ans;
}
inline void fwt(int *a,int n)
{
for(int i = 1;i < n;i <<= 1)
{
for(int j = 0;j < n;j += (i << 1))
{
for(int k = 0;k < i;k++)
{
int x = a[j + k],y = a[j + k + i];
a[j + k] = (x + y) % MOD;a[j + k + i] = (x - y + MOD) % MOD;
}
}
}
}
inline void ufwt(int *a,int n)
{
for(int i = 1;i < n;i <<= 1)
{
for(int j = 0;j < n;j += (i << 1))
{
for(int k = 0;k < i;k++)
{
int x = a[j + k],y = a[j + k + i];
a[j + k] = 1LL * (x + y) * INV % MOD;a[j + k + i] = 1LL * (x - y + MOD) * INV % MOD;
}
}
}
}
signed main()
{
seive();
while(~scanf("%lld %lld",&n,&m))
{
int nn = 1;while(m >= nn)nn <<= 1;
for(int i = 0;i < nn;i++) a[i] = 0;
for(int i = 1;i <= cnt && prime[i] <= m;i++) a[prime[i]] = 1;
fwt(a,nn);
for(int i = 0;i < nn;i++) a[i] = qpow(a[i],n);
ufwt(a,nn);
printf("%lld\n",a[0]);
}
return 0;
}