# BZOJ 4589 Hard Nim（FWT加速DP）

$f[i][j] = ∑f[i-1][j$ $\hat{}$ $c[k]]$， 我们令$g[c[i]]$为$1$，其余为$0$。

$f[i][j] = ∑f[i-1][j$ $\hat{}$ $k] * g[k]$

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b)	for (int i(a); i >= (b); --i)
#define MP		make_pair
#define fi		first
#define se		second

typedef long long LL;

const LL mod = 1e9 + 7;
const LL rev = (mod + 1) >> 1;

int n, m;
int l;
int tot = 0;
int p[1 << 20];
LL a[1 << 20], b[1 << 20], g[1 << 20];

void pre(){
rep(i, 2, 5e4){
if (!g[i]) p[++tot] = i;
for (int j = 1; j <= tot && i * p[j] <= 5e4; ++j){
g[i * p[j]] = 1;
if (i % p[j] == 0) break;
}
}
}

void FWT(LL *a, int n){
for (int d = 1; d < n; d <<= 1)
for (int m = d << 1, i = 0; i < n; i += m)
for (int j = 0; j < d; j++){
LL x = a[i + j], y = a[i + j + d];
a[i + j] = (x + y) % mod, a[i + j + d] = (x - y + mod) % mod;

}
}

void UFWT(LL *a, int n){
for (int d = 1; d < n; d <<= 1)
for (int m = d << 1, i = 0; i < n; i += m)
for (int j = 0; j < d; j++){
LL x = a[i + j], y = a[i + j + d];
a[i + j] = 1LL * (x + y) * rev % mod, a[i + j + d] = (1LL * (x - y) * rev % mod + mod) % mod;
}
}

void solve(LL *a, LL *b, int n, int p){
a[0] = 1;
FWT(a, n);
FWT(b, n);
while (p){
if (p & 1){
rep(i, 0, n - 1) (a[i] *= b[i]) %= mod;
}

rep(i, 0, n - 1) (b[i] *= b[i]) %= mod;
p >>= 1;
}

UFWT(a, n);
}

int main(){

pre();

while (~scanf("%d%d", &n, &m)){
for (l = 1; l <= m; l <<= 1){;}

memset(a, 0, sizeof a);
memset(b, 0, sizeof b);

for (int i = 1; i <= tot && p[i] <= m; ++i){
b[p[i]] = 1;
}

solve(a, b, l, n);
printf("%lld\n", a[0]);
}

return 0;
}


posted @ 2018-03-13 23:36  cxhscst2  阅读(224)  评论(0编辑  收藏