Description
Claris和NanoApe在玩石子游戏,他们有n堆石子,规则如下:
- Claris和NanoApe两个人轮流拿石子,Claris先拿。
- 每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜。
不同的初始局面,决定了最终的获胜者,有些局面下先拿的Claris会赢,其余的局面Claris会负。
Claris很好奇,如果这n堆石子满足每堆石子的初始数量是不超过m的质数,而且他们都会按照最优策略玩游戏,那么NanoApe能获胜的局面有多少种。
Sample Input
3 7
4 13
Sample Output
6
120
FWT裸题,存个板子。
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 50001;
const int mod = 1e9 + 7;
int _max(int x, int y) {return x > y ? x : y;}
int read() {
int s = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * f;
}
bool v[N];
int n, m, M, plen, p[N];
LL inv2, a[N * 4], ans[N * 4];
void get_p() {
for(int i = 2; i < N; i++) {
if(!v[i]) p[++plen] = i;
for(int j = 1; j <= plen && (LL)p[j] * i < N; j++) {
v[i * p[j]] = 1;
if(i % p[j] == 0) break;
}
}
}
LL pow_mod(LL a, int k) {
LL ans = 1;
while(k) {
if(k & 1) (ans *= a) %= mod;
(a *= a) %= mod; k /= 2;
} return ans;
}
void FWT(LL *y, int len, int o) {
for(int i = 1; i < len; i *= 2) {
for(int j = 0; j < len; j += i * 2) {
for(int k = 0; k < i; k++) {
LL u = y[j + k], v = y[j + k + i];
y[j + k] = (u + v) % mod, y[j + k + i] = (u - v + mod) % mod;
if(o == -1) y[j + k] = y[j + k] * inv2 % mod, y[j + k + i] = y[j + k + i] * inv2 % mod;
}
}
}
}
int main() {
n, m; get_p();
inv2 = pow_mod(2, mod - 2);
while(scanf("%d%d", &n, &m) != EOF) {
memset(a, 0, sizeof(a)), memset(ans, 0, sizeof(ans));
for(int j = 1; j <= plen && p[j] <= m; j++) a[p[j]]++;
ans[0] = 1;
for(M = 1; M<= m; M *= 2);
FWT(ans, M, 1), FWT(a, M, 1);
for(int i = 0; i < M; i++) ans[i] = ans[i] * pow_mod(a[i], n) % mod;
FWT(ans, M, -1);
printf("%d\n", ans[0]);
}
return 0;
}