HNOI2009 有趣的数列 卡特兰数

传送门

题意:题目已经说的很清楚了吧
题解:打了个表发现是卡特兰数。。太有趣了
把奇数项看成进栈,偶数项看成出栈,则题目转化为从左到右进栈出栈,且进栈数大于出栈数。=卡特兰数
由于p不是质数,用分解质因数的方法求

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 2000001;
const int MAXP = 150001;

int p[MAXP], notp[MAX], cnt;
int Min[MAX];
int tot[MAXP];

int Pow(int a, int b, int MOD){
    int ans = 1;
    while(b){
        if(b & 1) ans = ((long long)ans * a) % MOD;
        b >>= 1;
        a = ((long long) a * a) % MOD;
    }
    return ans;
}

inline void Div(int t, int opt){
    while(t != 1){
        tot[Min[t]] += opt;
        t /= p[Min[t]];
    }
}

int main(){
    freopen("in.txt", "r", stdin);
    int n, q; scanf("%d%d", &n, &q);
    for(int i = 2; i <= n * 2; i++){
        if(!notp[i]) p[++cnt] = i, Min[i] = cnt;
        for(int j = 1; j <= cnt && p[j] * i <= n * 2; j++){
            notp[p[j] * i] = 1; Min[p[j] * i] = j;
            if(i % p[j] == 0) break;
        }
    }
    for(int i = n + 2; i <= n * 2; i++) Div(i, 1);
    for(int i = 2; i <= n; i++) Div(i, -1);
    long long ans = 1;
    for(int i = 1; i <= cnt; i++){
        if(tot[i] > 0){
            ans = (ans * Pow(p[i], tot[i], q)) % q;
        }
    }
    printf("%lld", ans);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值