bzoj1485 [HNOI2009]有趣的数列（Catalan）

xiaoxiao2021-02-28  5

Description

Input

Output

Sample Input 3 10

Sample Output 5

//这里写代码片 #include<cstdio> #include<cstring> #include<cstring> #define ll long long using namespace std; const int N=2000003; int n,p; int sshu[N],tot=0,num[N],mp[N]; bool no[N]; void makeprime(int n) { memset(sshu,0,sizeof(sshu)); for (int i=2;i<=n;i++) { if (!no[i]) sshu[++tot]=i,mp[i]=tot; for (int j=1;j<=tot&&sshu[j]*i<=n;j++) { no[sshu[j]*i]=1; if (i%sshu[j]==0) break; } } } void calc(int x,int bz) //分解质因数 { int k=x; for (int i=1;sshu[i]*sshu[i]<=k;i++) if (k%sshu[i]==0) while (k%sshu[i]==0) { k/=sshu[i]; num[i]+=bz; } if (k>1) num[mp[k]]+=bz; } ll KSM(ll a,int b) { a%=p; ll t=1; while (b) { if (b&1) t=(a%p*t%p)%p; b>>=1; a=(a%p*a%p)%p; } return t%p; } int main() { scanf("%d%d",&n,&p); makeprime(N); for (int i=n+1;i<=2*n;i++) calc(i,1); for (int i=1;i<=n;i++) calc(i,-1); calc(n+1,-1); ll ans=1; for (int i=1;i<=tot;i++) ans=ans*KSM((ll)sshu[i],num[i])%p; printf("%lld",ans); return 0; }