题目链接  Hard Nim

设$f[i][j]$表示前$i$个数结束后异或和为$j$的方案数

那么$f[i][j] = f[i-1][j$ $\hat{}$ $k]$,满足$k$为不大于$m$的质数。

这个$DP$太暴力了。让我们冷静分析。

设不大于m的质数从小到大分别为$c_{1}$, $c_{2}$, ..., $c_{k}$

$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]$

我们发现后边其实就是一个异或卷积的形式。

于是就可以$FWT$了。

但是左边那一维还是巨大……

我们可以发现这个多项式乘法是满足结合律的

于是在做逆变换之前的那个乘法的时候直接快速幂即可。

#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;
}

  

BZOJ 4589 Hard Nim(FWT加速DP)的更多相关文章

  1. bzoj 4589 Hard Nim——FWT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4589 一开始异或和为0的话先手必败.有 n 堆,每堆可以填那些数,求最后异或和为0的方案数, ...

  2. bzoj 4589 Hard Nim —— FWT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4589 先手必败,是一开始所有石子的异或和为0: 生成函数 (xpri[1] + xpri[2 ...

  3. BZOJ.4589.Hard Nim(FWT)

    题目链接 FWT 题意即,从所有小于\(m\)的质数中,选出\(n\)个数,使它们异或和为\(0\)的方案数. 令\(G(x)=[x是质数]\),其实就是对\(G(x)\)做\(n\)次异或卷积后得到 ...

  4. BZOJ 4589 Hard Nim ——FWT

    [题目分析] 位运算下的卷积问题. FWT直接做. 但还是不太民白,发明者要承担泽任的. [代码] #include <cstdio> #include <cstring> # ...

  5. FWT [BZOJ 4589:Hard Nim]

    4589: Hard Nim Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 275  Solved: 152[Submit][Status][Disc ...

  6. BZOJ 4589 Hard Nim(FWT+博弈论+快速幂)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4589 [题目大意] 有n堆石子,每堆都是m以内的质数,请问后手必胜的局面有几种 [题解 ...

  7. bzoj 4589: Hard Nim【线性筛+FWT+快速幂】

    T了两次之后我突然意识到转成fwt形式之后,直接快速幂每次乘一下最后再逆回来即可,并不需要没此次都正反转化一次-- 就是根据nim的性质,先手必输是所有堆个数异或和为0,也就变成了一个裸的板子 #in ...

  8. [BZOJ 4589]Hard Nim

    Description 题库链接 两人玩 \(nim\) 游戏,\(n\) 堆石子,每堆石子初始数量是不超过 \(m\) 的质数,那么后手必胜的方案有多少种.对 \(10^9+7\) 取模. \(1\ ...

  9. bzoj 4347 [POI2016]Nim z utrudnieniem DP

    4347: [POI2016]Nim z utrudnieniem Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 733  Solved: 281[Su ...

随机推荐

  1. UasyUi的各种方法整理

    UasyUi的各种方法整理: 1.拖动 放置 droppable $('#dd').droppable({ }); 2.创建可变大小的窗口 resizable $('#rr').resizable({ ...

  2. MySQL、MongoDB、Redis 数据库之间的区别与使用(本章迭代更新)

    MySQL.MongoDB.Redis 数据库之间的区别与使用 MySQL.MongoDB.Redis 数据库之间的区别与使用(本章迭代更新) update:2019年2月20日 15:21:19(本 ...

  3. linux ubuntu开启sshd

    which ssh #查看文件 sudo apt-get install ssh #安装ssh cd /etc/init.d #切换目录 ls -l | grep ssh #执行启动脚本 sudo s ...

  4. python负数除法与模运算

    1.负数除法: >>> print 45/76>>> print -45/7-7 >>> print 45/-7-7 >>> p ...

  5. Oracle 遇到的问题:dos命令下imp导入数据时出错

    赋予用户dba权限:很多情况下会遇到没有权限需要输入用户名及密码才能导入 --已知被赋予权限的用户名为:batch --第一步 登陆 sqlplus /nolog sql>conn /as sy ...

  6. serial console

    适用于: agent_ipmitool_socat pxe_ipmitool_socat 修改driver方式:更换ironic node的driver类型 yum install -y socat ...

  7. leetcode_day01

    任务一:有效的括号 题目链接:https://leetcode-cn.com/problems/valid-parentheses/ 自己的答案: class Solution: def isVali ...

  8. html 网页注意事项

    html 知识总结; 1.内外边距 去掉浮动 *{ margin:0; padding:0; } 2.清除浮动 .clearfix:after { content:""; disp ...

  9. TypeSc­ript & Angular

    TypeSc­ript https://github.com/Microsoft/TypeScript https://www.typescriptlang.org/ https://www.type ...

  10. 在nodejs中 Object的toString()方法 querystring的stringify() JSON.stringify()

    刚学nodejs,做到一个例子:发送简单的HTTP请求.遇到一个问题,客户端给服务端发送的消息到服务端,服务端收不到消息,确切的说是“”. 以下是服务端代码:server.js const http ...