Future Failure CodeForces - 838C (博弈论,子集卷积)
大意: 两人轮流操作一个长$n$, 只含前$k$种小写字母的串, 每次操作删除一个字符或者将整个串重排, 每次操作后得到的串不能和之前出现过的串相同, 求多少种串能使先手必胜.
找下规律发现$n$为奇数必胜, 否则假设$a_i$为字符$i$出现次数, 如果$\frac{n!}{a_1!a_2!...a_k!}$为奇数则必败
$n!$中$2$的幂次为n-__builtin_popcount(n)
所以必败就等价于$a_1+...+a_n=a_1|...|a_n$
设$f_{i,j}$表示前$i$个字符, 状态为$j$的方案数除以总字符数的阶乘
可以得到转移为$f_{i,S}=\sum \frac{1}{x!} f_{i-1,S\oplus x}$
做$O(\log k)$次子集卷积即可, 复杂度是$O(n\log ^2n\log k)$
我写的好像常数太大的没卡过去, 先这样吧
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
const int N = 1e6+10;
int n,k,P,fac[N],ifac[N],cnt[N];
int dp[N],f[20][N],g[20][N],h[20][N];
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} void FMT(int *a, int n, int tp) {
int mx = (1<<n)-1;
REP(i,0,n-1) REP(j,0,mx) {
if (j>>i&1) a[j]=(a[j]+tp*a[j^1<<i])%P;
}
} void mul(int *a, int *b, int *c, int n) {
int mx = (1<<n)-1;
REP(i,0,n) REP(j,0,mx) f[i][j]=g[i][j]=h[i][j]=0;
REP(i,0,mx) {
f[cnt[i]][i] = a[i];
g[cnt[i]][i] = b[i];
}
REP(i,0,n) FMT(f[i],n,1),FMT(g[i],n,1);
REP(i,0,n) {
REP(j,0,i) REP(k,0,mx) {
h[i][k] = (h[i][k]+(ll)f[j][k]*g[i-j][k])%P;
}
FMT(h[i],n,-1);
REP(k,0,mx) if (cnt[k]==i) c[k] = h[i][k];
}
} int main() {
REP(i,0,N-1) cnt[i] = __builtin_popcount(i);
scanf("%d%d%d",&n,&k,&P);
fac[0] = 1;
REP(i,1,N-1) fac[i]=(ll)fac[i-1]*i%P;
ifac[N-1] = inv(fac[N-1]);
PER(i,0,N-2) ifac[i]=(ll)ifac[i+1]*(i+1)%P;
int tot = qpow(k,n);
if (n&1) return printf("%d\n",tot),0;
int len = 1;
while ((1<<len)<=n) ++len;
dp[0] = 1;
for (; k; mul(ifac,ifac,ifac,len),k>>=1) {
if (k&1) mul(dp,ifac,dp,len);
}
int ans = (tot-(ll)dp[n]*fac[n])%P;
if (ans<0) ans += P;
printf("%d\n", ans);
}
Future Failure CodeForces - 838C (博弈论,子集卷积)的更多相关文章
- CF 914G Sum the Fibonacci——子集卷积
题目:http://codeforces.com/contest/914/problem/G 第一个括号可以子集卷积:第三个括号可以用 FWT 异或卷积:这样算出选两个数组成 x 的方案数:三个部分的 ...
- CF 914 G Sum the Fibonacci —— 子集卷积,FWT
题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...
- CF838C(博弈+FWT子集卷积+多项式ln、exp)
传送门: http://codeforces.com/problemset/problem/838/C 题解: 如果一个字符串的排列数是偶数,则先手必胜,因为如果下一层有后手必赢态,直接转移过去,不然 ...
- CF914G Sum the Fibonacci FWT、子集卷积
传送门 一道良心的练习FWT和子集卷积的板子-- 具体来说就是先把所有满足\(s_a \& s_b = 0\)的\(s_a \mid s_b\)的值用子集卷积算出来,将所有\(s_a \opl ...
- UOJ 348 【WC2018】州区划分——子集卷积
题目:http://uoj.ac/problem/348 参考:https://www.cnblogs.com/NaVi-Awson/p/9242645.html#%E5%AD%90%E9%9B%86 ...
- hdu 6057 Kanade's convolution(子集卷积)
题解: 然后就是接下来如何fwt 也就是如何处理bit(x) - bit(y) = bit(k)这个条件. 其实就是子集卷积. 把bit(x)和bit(y)划分成两个集合,然后就是子集卷积的形式. 这 ...
- UOJ #348 州区划分 —— 状压DP+子集卷积
题目:http://uoj.ac/problem/348 一开始可以 3^n 子集DP,枚举一种状态的最后一个集合是什么来转移: 设 \( f[s] \) 表示 \( s \) 集合内的点都划分好了, ...
- 【学习笔记】fwt&&fmt&&子集卷积
前言:yyb神仙的博客 FWT 基本思路:将多项式变成点值表达,点值相乘之后再逆变换回来得到特定形式的卷积: 多项式的次数界都为\(2^n\)的形式,\(A_0\)定义为前一半多项式(下标二进制第一位 ...
- UOJ348 WC2018 州区划分 状压DP、欧拉回路、子集卷积
传送门 应该都会判欧拉回路吧(雾 考虑状压DP:设\(W_i\)表示集合\(i\)的点的权值和,\(route_i\)表示点集\(i\)的导出子图中是否存在欧拉回路,\(f_i\)表示前若干个城市包含 ...
随机推荐
- JavaScript高级程序编程(三)
2017-06-24 更新 北京连续三天下雨啦 乘性操作符 1.ECMA中定义了三种操作符,乘法 除法 和求模 并与其他语言相应操作符相同,再计算之前如果不是数值,会先去调用number()方法转 ...
- Spark在美团的实践
https://tech.meituan.com/2016/03/31/spark-in-meituan.html 本文已发表在<程序员>杂志2016年4月期. 前言 美团是数据驱动的互联 ...
- Centos分区/超过2T的磁盘
centos分区大于2TB 用parted分区工具分区 fdisk -l 查看要分的区(我这里是/dev/vdb) parted /dev/vdb #进入/dev/vdb进行分区 mktabl ...
- ssh修改默认远程端口
---------------------centos6-----------------1.查看系统版本cat /etc/redhot-releose 2.编辑sshd配置,修改默认的端口vim / ...
- DB proxy, mysql proxy
db proxy 在大型互联网站的数据库部署中,部署最多的数据库为MySQL.随着MySQL中Innodb存储引擎对事物的支持,MySQL在互联网公司部署中,应用量越来越多.典型应用MySQL的公司有 ...
- Attention U-Net: Learning Where to Look for the Pancreas
Attention U-Net: Learning Where to Look for the Pancreas 2019-09-10 09:50:43 Paper: https://arxiv.or ...
- jmeter BeanShell断言(四)
Bean Shell常用内置变量 JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下: log:写入信息到jmeber.log ...
- kubectl -n ingress-nginx exec nginx-ingress-controller-78bd49949c-t22bl -- cat /etc/nginx/nginx.conf
kubectl -n ingress-nginx exec nginx-ingress-controller-78bd49949c-t22bl -- cat /etc/nginx/nginx.conf
- activiti 自定义用户
https://blog.csdn.net/meng564764406/article/details/53789958 此文目的: 对网络上的关于对activiti 使用做一个总结,因为很难找到一个 ...
- typescript - 6.泛型
泛型类 class MinClas<T>{ public list:T[]=[]; add(value:T):void{ this.list.push(value); } min():T{ ...