根据Lucas定理,等价于在$P$进制下每一位分别求组合数最后乘积模$P$。

因为答案为$0$的并不好算,所以可以考虑用$n+1$减去其它所有的答案。

那么每一位的组合数都不能是$0$,那么这就保证了$k$的每一位都不大于$n$,所以无需考虑$k\leq n$这个限制。

求出模$P$下每个数的指标,考虑数位DP,设$f[i][j]$表示考虑了最后$i$位,组合数的指标之和模$\varphi(P)$为$j$的方案数。

那么转移就是卷积的形式,FFT加速即可。

时间复杂度$O(P\log P\log n)$。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int P=51061,F=P-1,O=29,N=131100;
const double pi=acos(-1.0);
char s[P];
int flag,n=131072,len,i,j,po[P],ind[P],fac[P],inv[P],a[P],ans[P],pos[N],f[N],g[N];
struct comp{
double r,i;comp(double _r=0,double _i=0){r=_r,i=_i;}
comp operator+(const comp&x){return comp(r+x.r,i+x.i);}
comp operator-(const comp&x){return comp(r-x.r,i-x.i);}
comp operator*(const comp&x){return comp(r*x.r-i*x.i,r*x.i+i*x.r);}
comp conj(){return comp(r,-i);}
}A[N],B[N];
inline int C(int n,int m){return 1LL*fac[n]*inv[m]*inv[n-m]%P;}
void FFT(comp*a,int t){
for(int i=1;i<n;i++)if(i<pos[i])swap(a[i],a[pos[i]]);
for(int d=0;(1<<d)<n;d++){
int m=1<<d,m2=m<<1;double o=pi*2/m2*t;comp _w(cos(o),sin(o));
for(int i=0;i<n;i+=m2){
comp w(1,0);
for(int j=0;j<m;j++){
comp&A=a[i+j+m],&B=a[i+j],t=w*A;
A=B-t;B=B+t;w=w*_w;
}
}
}
if(t==-1)for(int i=0;i<n;i++)a[i].r/=n;
}
void mul(int k){
if(!flag){
flag=1;
for(i=0;i<=k;i++)f[ind[C(k,i)]]++;
for(i=0;i<F;i++)f[i]%=O;
return;
}
for(i=0;i<F;i++)g[i]=0;
for(i=0;i<=k;i++)g[ind[C(k,i)]]++;
for(i=0;i<F;i++)g[i]%=O;
for(i=0;i<F;i++)A[i]=comp(f[i],g[i]);
for(i=F;i<n;i++)A[i]=comp(0,0);
FFT(A,1);
for(i=0;i<n;i++){
j=(n-i)&(n-1);
B[i]=(A[i]*A[i]-(A[j]*A[j]).conj())*comp(0,-0.25);
}
FFT(B,-1);
for(i=0;i<F;i++)f[i]=0;
for(i=0;i<n;i++)f[i%F]=(f[i%F]+int(B[i].r+0.5))%O;
}
int main(){
for(po[0]=i=1;i<P-1;i++)po[i]=po[i-1]*2%P;
for(i=0;i<P-1;i++)ind[po[i]]=i;
for(fac[0]=fac[1]=inv[0]=inv[1]=1,i=2;i<P;i++)inv[i]=1LL*(P-inv[P%i])*(P/i)%P;
for(i=2;i<P;i++)fac[i]=1LL*fac[i-1]*i%P,inv[i]=1LL*inv[i-1]*inv[i]%P;
scanf("%s",s+1);
len=strlen(s+1);
for(i=1;i<=len;i++)a[i]=s[len-i+1]-'0',ans[0]=(ans[0]*10+s[i]-'0')%O;
ans[0]++;
ans[0]%=O;
j=__builtin_ctz(n)-1;
for(i=0;i<n;i++)pos[i]=pos[i>>1]>>1|((i&1)<<j);
while(len){
for(a[0]=0,i=len;i;i--)a[i-1]+=a[i]%P*10,a[i]/=P;
while(len&&!a[len])len--;
mul(a[0]/10);
}
for(i=0;i<F;i++)ans[po[i]]=f[i];
for(i=1;i<P;i++)ans[0]=(ans[0]+O-ans[i])%O;
for(i=0;i<P;i++)if(ans[i]<=9)putchar(ans[i]+'0');else putchar(ans[i]-10+'A');
return 0;
}

  

BZOJ2629 : binomial的更多相关文章

  1. Understanding Binomial Confidence Intervals 二项分布的置信区间

    Source: Sigma Zone, by Philip Mayfield The Binomial Distribution is commonly used in statistics in a ...

  2. 基本概率分布Basic Concept of Probability Distributions 4: Negative Binomial Distribution

    PDF version PMF Suppose there is a sequence of independent Bernoulli trials, each trial having two p ...

  3. 基本概率分布Basic Concept of Probability Distributions 1: Binomial Distribution

    PDF下载链接 PMF If the random variable $X$ follows the binomial distribution with parameters $n$ and $p$ ...

  4. Sdut 2164 Binomial Coeffcients (组合数学) (山东省ACM第二届省赛 D 题)

    Binomial Coeffcients TimeLimit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 输入 输出 示例输入 1 1 10 2 9 ...

  5. Binomial Showdown

    Binomial Showdown TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Totalsubmit: 2323   Accepted: 572 D ...

  6. Binomial Coeffcients 历届山东省省赛题

    Binomial Coeffcients Time Limit: 1000MS Memory limit: 65536K 题目描述   输入   输出   示例输入 3 1 1 10 2 954 72 ...

  7. Binomial Coeffcients 过去山东省省赛冠军

    Binomial Coeffcients Time Limit: 1000MS Memory limit: 65536K 题目描写叙述   输入   输出   演示样例输入 3 1 1 10 2 95 ...

  8. zoj 1938 Binomial Showdown 组合数裸基础

    Binomial Showdown Time Limit: 2 Seconds      Memory Limit: 65536 KB In how many ways can you choose ...

  9. The zero inflated negative binomial distribution

    The zero-inflated negative binomial – Crack distribution: some properties and parameter estimation Z ...

随机推荐

  1. JS返回上一页

    <button  onclick="javascript:history.go(-1);">返回上一页</button> <button  oncli ...

  2. ld: framework not found AGCommon 关于三方库到入 问题解决方案!!

    ld: framework  not found AGCommon clang:error:linker command failed with exit code 1 (use -v to see ...

  3. UIColor+Hex

    #import <UIKit/UIKit.h> @interface UIColor (Hex) + (UIColor *)colorWithHex:(long)hexColor;+ (U ...

  4. python字符串中插入变量

  5. JQ JSON数据类型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. OCJP(1Z0-851) 模拟题分析(六)over

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  7. dbca:Exception in thread "main" java.lang.UnsatisfiedLinkError: get

    在64位的操作系统安装oracle10g 软件安装完成后,使用dbca建库的时候报下面的错: $ dbcaUnsatisfiedLinkError exception loading native l ...

  8. shell test 數值 字符串 文件比較

    數值比較 描述 n1 –eq n2 等於 n1 –gt  n2 大於 n1 –ge n2 大於等於 n1 –lt  n2 小於 n1 –le n2 小於等於 n1 –ne n2 不等於   字符串比較 ...

  9. 如何安装sublime text2以及它的插件?

    下载Sublime Text2的安装包,安装,安装后打开的界面如图   下面我们来给他安装插件,首先安装packagecontrol,打开菜单栏中的View-->show console   在 ...

  10. 解决因为I_JOB_NEXT问题导致job执行不正常,不停生成trace文件问题

    今天同事说有个项目生产环境的目录老是满.查看了一下bdump目录,发现确实是平均1分钟生成一个8M左右的trace文件.查询了一下alert日志,发现是个job的报错引起的.具体查看了一下trace文 ...