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: ...
随机推荐
- 小米 OJ 编程比赛 03 月常规赛
A.数学等式 数据比较小,可以暴力+折半枚举. #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(in ...
- CF1132.Educational Codeforces Round 61(简单题解)
A .Regular Bracket Sequence 题意:给定“((” , “()” , “)(”, “))”四种,问是否可以组成合法括号匹配 思路:设四种是ABCD,B可以不用管,而C在A或 ...
- jquery选择后代以及toggle,toggleClass用法
$("#id").find(".classname") 选择id为xx下的类名为xx的元素 toggle $(this).next(".c ...
- python猜数字(多种实现方法)
设定一个理想数字比如:66,让用户输入数字,如果比66⼤,则显示猜测的结果⼤了:如果比66⼩,则显示猜测的结果小了;只有等于66,显示猜测结果 第一种方式(最简单的方式实现) n = 66 # 理想数 ...
- Oracle数据库的学习
复制数据库结构到另外一数据库的的语句,首先在数据库创建链接,比如我在131数据库,dblink_018 的018为链接名称,随便取 ,可在此表查看数据库链接 select * from dba_db_ ...
- Linux 修改yum源
第一种方式: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo 第二种方式: ...
- python-xlsxwriter模块绘制表格
#coding: utf-8 import xlsxwriter workbook=xlsxwriter.Workbook('chart.xlsx') worksheet=workbook.add_w ...
- linux系统常用监控系统状态信息命令
[root@bogon ~]# uptime #平均负载 23:33:21 up 2:09, 3 users, load average: 0.02, 0.05, 0.05 [root@bogon ~ ...
- linux网络编程概念(一)
AF表示地址族(address family) PF表示协议族(protocol family) domain参数 AF_UNIX 内核中通信 sockaddr_un AF_INET 通过ipv4 s ...
- Serv-U设置被动模式(FTP工具)
FTP服务器在公司内网,通过端口映射把21端口映射出去. 公司一些机器也在各个省的机房内网.好在这些机器可以访问公网.由于各个地区的机器托管在各个地区机房. 我有公司防火墙的权限,可以做防火墙上做端口 ...