The Triangle Division of the Convex Polygon

题目:

Description

A convex polygon with n edges can be divided into several triangles by some non-intersect diagonals. We denote d(n) is the number of the different ways to divide the convex polygon. For example,when n is 6,there are 14 different ways.Figure 1 shows such 14 ways. Then ,we get d(6)=14.

 

Figure 1 when n=6, d(6)=14 .

Your task is that:for a given integer n, you’d tell us d(n). Because the d(n) may be a very large number, the result need to modulo m.

Input

There are multiple test cases.

Each test case contains two postive integers (n,m) separated by a space in one line.

The input will be terminated by the end of input file.

3<=n<=500,000

1<=m<=45,000

Output

For each test case ,output an integer d(n)%m in one line.

Sample Input

6 10

3 10
Sample Output

4

1

题意: 给你一个n边的多边形,求将整个图形划分为一个个三角形,问有几种划分方法。

分析:这是卡特兰数的一个很典型的应用。卡特兰数有许多的公式:递推式:h(n)=h(n-1)*(4*n-2)/(n+1);公式:h(n)=C(2n,n)/(n+1) = (2n)!/(n!)*(n+1)!

在这道题目中,我们无法使用递推式,因为此题要取模,而公式1中有除法,又因为m不保证是素数,故也无法使用乘法逆元来化简,所以我们就得使用第二个公式来计算。

第二个公式,我们同样无法使用逆元,但我们有另外一个武器:阶乘的素因子分解。

所谓的素因子分解,就是对于一个阶乘,我们可以将其表示成几个素数幂的乘法形式,比如4!中,我们可以分解出3个2以及1个3,那么2^3*3^1=24 ,这个结果就等于4! 。所以我们可以将分子分母都分解成素数乘法,然后将其中相同的素数约掉一些幂数(当然,这里的分母总是大于分子),最后将该结果用快速幂取余来求得。怎么来把这个n!因式分解呢? 我们知道 n!中某个因子x的数量可以用log(n)的方法来求。

代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<string>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cctype>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
using namespace std;
#define pt(a) cout<<a<<endl
#define debug test
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
typedef pair<int,int> PII;
const ll mod = 1e9+7;
const int N = 5e5+10;

ll m;

ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qp(ll a,ll b) {ll res=1;a%=m; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%m;a=a*a%m;}return res;}
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

ll n,vs[N],p[N],ct[N],k;

void gp() {
    for(ll i=2;i<N;i++) {
        if(!vs[i]) {
            p[++k]=i;
            for(ll j=i*i;j<N;j+=i) vs[j]=1;
        }
    }
}

void gf(int x,int sg) {
    for(ll i=1;p[i]<=x;i++) {
        ll t=x;
        while(t) ct[i]+=t/p[i]*sg,t/=p[i];
    }
}

void sv() {
    ll ans = 1;
    for(int i=1;p[i]<=2*n;i++)
        (ans *= qp(p[i],ct[i]))%=m;
    cout<<ans<<endl;
}

int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    gp();
    while(cin>>n>>m) {
        n -= 2;
        gf(2*n,1);
        gf(n,-1);
        gf(n+1,-1);
        sv();
        mst(ct,0);
    }
    return 0;
}

 

参考博客:https://blog.csdn.net/ThinkingLion/article/details/40711851

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值