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. c++输入输出流加速器

      发现同样是cin,cout,其他大佬(orz)的耗时短很多.看了他们的代码,我发现他们加了一个很神奇的匿名函数(Lambda捕获)提高了cin,cout效率,因此去百度了解了一下.以下是大佬所使用 ...

  2. 【Wannafly挑战赛14C可达性】【Tarjan缩点】

    链接:https://www.nowcoder.com/acm/contest/81/C来源:牛客网 题目描述 给出一个 0 ≤ N ≤ 105 点数.0 ≤ M ≤ 105 边数的有向图,输出一个尽 ...

  3. 原生的js轮播图

    图片会照常循环播放,当然也可以通过按钮来进行切换,当切出当前的页面时,等到你在回到当前页面时该轮播的图片还是停留在你之前所切出去的的那张图片的状态. HTML部分: <html> < ...

  4. LeetCode – Lemonade Change

    At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...

  5. 【java编程】java中什么是bridge method(桥接方法)

    https://blog.csdn.net/mhmyqn/article/details/47342577 https://www.cnblogs.com/strinkbug/p/5019453.ht ...

  6. 监控文件事件inotify

    #include<sys/inotify.h> int inotify_init(void);//创建一个新的inotify实例,成功会返回一个文件描述符fd int inotifyk_a ...

  7. namespace关键字学习笔记

    一.namespace简介 namespace中文意思是命名空间或者叫名字空间,传统的C++只有一个全局的namespace,但是由于现在的程序的规模越来越大,程序的分工越来越细,全局作用域变得越来越 ...

  8. svelte 构建快速web 应用的工具

    svelte 和angular vue reat 类似,都是方便快速的创建用户界面,最大不同的地方是svelte 转换你的app 是在构建时,而不是运行时,所以好处就是不用花费太多的操作在,框架的 抽 ...

  9. GCC related commands

    GCC   -l  option is to link the library. It can use for static and share link.  Link -l with library ...

  10. WebSocket概念

    WebSocket 是什么? WebSocket 是一种网络通信协议.RFC6455 定义了它的通信标准. WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议 ...