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的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  3. Subsets

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

  4. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  5. [LeetCode] Subsets 子集合

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  6. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

  7. 【leetcode】Subsets II (middle) ☆

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  8. 【leetcode】Subsets (Medium) ☆

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  9. [Leetcode] Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

随机推荐

  1. Error Downloading Packages: yum更新出现错误

    yum  install lrzsz 失败报错: 解决思路: 1:执行yum clean all 清除缓存目录下的软件包及旧的headers: 2:接着执行 yum list 重新列出所有已经安装和可 ...

  2. Blender节点笔记

    Blender节点笔记实现复杂材质,纹理的更直观的方式就是使用节点功能. 每个节点左边作为输入,右边作为输出.节点之间通过传递值影响后者.传递的值为(Scalars,Vectors)标量与矢量.二维矢 ...

  3. LeetCode - Most Frequent Subtree Sum

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

  4. LeetCode - Trim a Binary Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  5. java黑魔法-反射机制-01

    在java的帮助文档中,java.lang包中有一个Class类,注意这里的"C“是大写,所以这个不是表示类的声明,而是一个真正的类.在java的帮助文档中,这样定义的Class类: pub ...

  6. ipfs webui 管理界面

    ipfs 内置了一个webui 默认的端口是5001 访问地址 http://ip:5001/webui 环境准备 docker-compose 文件   version: "3" ...

  7. Sql server日期函数用法

    SQL日期函数 SQL日期函数中的类型码可以为0,1,2,3,4,5,6,7,8,9,10,11,12,13,14 ,20,21,22,23,24,25,100,101,102,103,104,105 ...

  8. JCP与JSR

    JCP Java Community Process ,Java社区进程,Java标准指定组织JCP成立于1998年,官网,由社会各界Java组成的社区,规划和领导Java的发展,其成员可以在这里看到 ...

  9. Percona XtraDB Cluster 的一些使用限制(PXC 5.7)

    Percona XtraDB Cluster有众多的优秀特性,使得mysql集群得以轻松实现.但是不要忽略了它的一些限制.如果你无法接受,或者你的应用程序或数据库(比如使用了memory引擎)对限制无 ...

  10. js将foo-bar转为fooBar

    这是一道js的面试题,就是考察将普通面命转为小驼峰命名 <!DOCTYPE html> <html lang="en"> <head> < ...