bzoj3491: PA2007 Subsets
Description
有一个集合U={1,2,…,n),要从中选择k个元素作为一个子集A。
若a∈A,则要有a*X不属于A,x是一个给定的数。
求可选方案对M取模后的值。
1< = N< = 10^18,2< = m< = 1000000,0< = K< = 1000,2< = x< = 10。
Input
第一行输入N,M,K,X
Output
如题
将不能共存的数连边,每个联通块都是一条链,计算出每个长度的链的条数,以及每个长度的链上取0~k个数的方案数,卷积可得答案。时间复杂度$O(klog(k)log(n)log_x(n))$
#include<cstdio>
#include<cmath>
#include<algorithm>
typedef unsigned long long u64;
typedef long double ld;
const ld pi=std::acos(-);
struct cplx{
ld a,b;
cplx(ld x=,ld y=):a(x),b(y){}
cplx operator+(cplx x){return cplx(a+x.a,b+x.b);}
cplx operator-(cplx x){return cplx(a-x.a,b-x.b);}
cplx operator*(cplx x){return cplx(a*x.a-b*x.b,a*x.b+b*x.a);}
cplx conj(){return cplx(a,-b);}
}A[],B[],E[][][];
u64 n,pwx[],ts[];
int m,k,x,ppx=,C[][],N,K;
int ans[],tmp[],tmp2[],rev[];
void dft(cplx*a,int t){
for(int i=;i<N;++i)if(i<rev[i])std::swap(a[i],a[rev[i]]);
for(int i=,z=;i<N;i<<=,++z){
cplx*e=E[t][z];
for(int j=;j<N;j+=i<<){
cplx*b=a+j,*c=b+i;
for(int k=;k<i;++k){
cplx x=b[k],y=c[k]*e[k];
b[k]=x+y;
c[k]=x-y;
}
}
}
if(t)for(int i=;i<N;++i)a[i].a/=N;
}
void mul(int*a,int*b){
for(int i=;i<=k;++i)B[i]=cplx(a[i],b[i]);
for(int i=k+;i<N;++i)B[i]=;
dft(B,);
B[N]=B[];
for(int i=;i<N;++i){
cplx x=B[i],y=B[N-i].conj();
A[i]=cplx(,0.25)*(y+x)*(y-x);
}
dft(A,);
for(int i=;i<=k;++i)a[i]=((long long)(A[i].a+0.49))%m;
}
int main(){
scanf("%llu%d%d%d",&n,&m,&k,&x);
C[][]=;
for(int i=;i<;++i){
for(int j=;j<=i;++j){
(C[i+][j]+=C[i][j])%=m;
(C[i+][j+]+=C[i][j])%=m;
}
}
for(pwx[ppx++]=;(pwx[ppx]=pwx[ppx-]*x)<=n;++ppx);
for(int i=ppx-;i>=;--i){
ts[i]=n/pwx[i];
ts[i]-=ts[i]/x;
}
for(N=,K=;N<k*+;N<<=,++K);
for(int i=;i<N;++i)rev[i]=rev[i>>]>>|(i&)<<K;
for(int i=,z=;i<N;i<<=,++z){
for(int j=;j<i;++j){
E[][z][j]=cplx(cos(j*pi/i),sin(j*pi/i));
E[][z][j]=E[][z][j].conj();
}
}
ans[]=;
for(int i=;i<ppx;++i){
ts[i]-=ts[i+];
for(int j=;j<=k;++j)tmp[j]=(i+-j>=?C[i+-j][j]:),tmp2[j]=;
tmp2[]=;
for(u64 t=ts[i];t;t>>=,mul(tmp,tmp))if(t&)mul(tmp2,tmp);
mul(ans,tmp2);
}
printf("%d\n",ans[k]);
return ;
}
bzoj3491: PA2007 Subsets的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...
- [LeetCode] Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [LeetCode] Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode】Subsets (Medium) ☆
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- [Leetcode] Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
随机推荐
- ps使用图层合并切图
1.新建图层 2.合并要切的图的图层和新建的空白图层 3用正方形工具圈出来要切得图 4.ctrl+c复制,ctrl+n新建,ctrl+v复制过去 5,切图
- log4j.properties与db.properties
log4j.properties与db.properties db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql:///mybatis?useUnico ...
- linux下寻找段错误的方法
为了能够快速找到发生段错误的地方,记录以下两种方法. objdump和backtrace的配合使用 :https://www.cnblogs.com/jiangyibo/p/9507555.html ...
- Go Example--超时处理
package main import ( "fmt" "time" ) func main() { c1 := make(chan string, 1) go ...
- python基础(三)——类的研究
class Employee: //定义类 以冒号结束 '所有员工的基类' //帮助信息 empCount = 0 def __init__(self, name, salary): //调用时初始化 ...
- mysql不存在插入否则更新
1.如果存在就更新,没有就插入,相当于三目运算 CREATE UNIQUE INDEX xxx ON t_msg_user_view(USER_ID); INSERT INTO xxx(user_id ...
- Singer 学习三 使用Singer进行mongodb 2 postgres 数据转换
Singer 可以方便的进行数据的etl 处理,我们可以处理的数据可以是api 接口,也可以是数据库数据,或者 是文件 备注: 测试使用docker-compose 运行&&提供数据库 ...
- Gravitee.io api gateway 试用
以前写过几篇关于整体介绍的以及 使用docker 运行的简单说明,有了docker-compose 环境我们可以 方便的进行测试使用了. 环境准备 docker-compose 文件 versio ...
- TCP/IP option data aligement issue cause system broken
1 Problem Description The field reports show that xxx panel will lockup and then reboot while d ...
- 适配器(Adapter)
Adapter:将一个Class的接口转换成另一个Class的接口,使原本因接口不兼容而不能合作的Class可以一起运作.主要有两种:迭代器适配器(Iterator Adpater)和容器适配器(Co ...