[HNOI2009] 有趣的数列

题目描述:

qwq…

题目分析:

DP打表发现答案即为 Catalan(n)

Catalan(n)=Cn2nn+1 C a t a l a n ( n ) = C 2 n n n + 1

注意P不保证是质数,通过质因数分解算组合数…

题目链接:

BZOJ 1485
Luogu 3200

Ac 代码:

#include <cstdio> 
#include <iostream>
#include <algorithm>
#include <cstring>
const int maxm=2000010;
int prime[maxm],vis[maxm],num[maxm],down[maxm],cnt;
int n,p;
inline int fastpow(int x,int y)
{
    int ans=1;
    for(;y;y>>=1,x=(1ll*x*x)%p) if(y&1) ans=(1ll*ans*x)%p;
    return ans;
}
inline void pre(int N)
{
    vis[1]=1;
    for(int i=2;i<=N;i++)
    {
        if(!vis[i]) prime[++cnt]=i,down[i]=cnt;
        for(int j=1;j<=cnt&&prime[j]*i<=N;j++)
        {
            vis[i*prime[j]]=1;
            down[i*prime[j]]=j;
            if(!(i%prime[j])) break;
        }
    }
}
inline void tax(int x,int v)
{
    while(x!=1)
    {
        num[down[x]]+=v;
        x/=prime[down[x]];
    }
}
int main()
{
    long long ans=1;
    scanf("%d%d",&n,&p);
    pre(2*n);
    for(int i=n+2;i<=n*2;i++) tax(i,1);  
    for(int i=1;i<=n;i++) tax(i,-1);  
    for(int i=1;i<=cnt;i++) ans=(ans*fastpow(prime[i],num[i]))%p;
    printf("%lld\n",ans);
    return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值