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边的多边形,求将整个图形划分为一个个三角形,有几种划分方法?

这是卡特兰数的一个很典型的应用。

卡特兰数的相关应用:http://blog.csdn.net/duanruibupt/article/details/6869431

卡特兰数有许多的公式:

递推式: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! 。。所以我们可以将分子分母都分解成素数乘法,然后将其中相同的素数约掉一些幂数(当然,这里的分母总是大于分子),最后将该结果用快速幂取余来求得。

更具体的原理请参照:http://wenku.baidu.com/link?url=Yt_aIRP1Y_BGJnSfzp5EcacPWbVEa0Amh2u3IajKz2qBh2BjvvCy1uuBdqTJnQ93So3sSt4HpMeEdMCm-q_kduVHGFkf3EQMePyLLfOH1sK


/*
 * ThinkingLion.cpp
 *
 *  Created on: 2014年11月1日
 *      Author: dell
 */
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<set>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
int n,m,k;
#define N 1000005
#define ll __int64
ll prime[N],p[N],f[N]; //prime用来存储素数,p[i]代表某个整数含第i个质数的幂的次数,f[i]代表i这个数含有的最小的质因数
int tot,len;
ll quick_mod(ll a,ll b, int mod){			//快速幂取余
	ll res = 1%mod;
	while(b){
		if(b&1) res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res;
}
void getPrime(){						//素数打表
	memset(f,0,sizeof f);
	tot=0;
	for(int i=2;i<N;i++){
		if(!f[i])
		prime[tot++]=f[i]=i;
		for (int j = 0; j < tot && prime[j] <= f[i] && i * prime[j] < N; j++)
			f[i * prime[j]] = prime[j];
	}
}
void Func1(ll t,int flag){		//对某个数(t代表阶乘的一部分)的阶乘分解质因数,flag为1表示该数(t)在阶乘的分子上,为0表示在分母上
	ll tmp;
	int i;
	for(i=0;prime[i]<=t;i++){	//分解完后,t就可以表示为一些质数幂的乘积,比如4!,我们可以分成3个2和1个3,(2^3 * 3^1 = 24)  == (4!)
		tmp=t;
		while(tmp){	//p数组在使用前得清空
			if(flag == 1) p[i]+=(tmp/prime[i]);		//p[i]代表tmp含第i个质数的幂次,在分子上就+
			else p[i]-=(tmp/prime[i]);			//在分母中就 - ,(因为分子/分母,即分子的幂次-分母的幂次)
			tmp/=prime[i];
		}
	}
	len = Max(len,i);		//存储最长能分解到的素数位置
}
ll Func2(int mod){				//计算分解后的阶乘
	ll ans=1;
	for(int i=0;i<len;i++){
		ll tmp = quick_mod(prime[i],p[i],m);
		ans=ans*tmp%mod;
	}
	return ans;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
#endif
	getPrime();
	while(scanf("%d%d",&n,&m)!=EOF){
		if(n<3){
			printf("0\n");
			continue;
		}
		memset(p,0,sizeof p);
		n=n-2;
		len=-1;
		Func1(2*n,1);
		Func1(n,0);
		Func1(n+1,0);
		ll ans=Func2(m);
		printf("%I64d\n",ans);
	}
return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值