# hunnu11562:The Triangle Division of the Convex Polygon(第n个卡特兰数取模)

 Problem 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  Problem Source 2014哈尔滨理工大学秋季训练赛

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define ls 2*i
#define rs 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 1000005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define rank rank1
const int mod = 1000000007;

int Pow(int x, int b)
{
int ret = 1;
for(int s = x; b; b >>= 1)
{
if(b & 1)
ret *= s;
s *= s;
}
return ret;
}
int PowMod(int x, int b, int p)
{
int ret = 1 % p;
for(int s = x % p; b; b >>= 1)
{
if(b & 1)
{
ret *= s;
ret %= p;
}
s *= s;
s %= p;
}
return ret;
}
pair<int, int> ExGcd(int a, int b)
{
if(a == 0) return make_pair(0, 1);
pair<int, int> s = ExGcd(b % a, a);
return make_pair(s.second - b / a * s.first, s.first);
}
int Inv(int a, int m) // ax == 1 mod m
{
// assert gcd(a, m) == 1
pair<int, int> s = ExGcd(a, m);
// s.first * a + s.second * m == 1
return s.first < 0 ? s.first + m : s.first;
}
pair<int, int> FacMod(int n, int p, int k)
{
// assert p > 1, k > 0
int pk = Pow(p, k);
int S[pk];
S[0] = 1 % pk;
for(int i = 1; i < pk; ++i)
{
S[i] = S[i - 1];
if(i % p != 0)
{
S[i] *= i;
S[i] %= pk;
}
}
int ret = 1 % pk, et = 0, ep = 0; // S[pk - 1]^et, p^ep
while(n)
{
et += n / pk;
ret *= S[n % pk];
ret %= pk;
ep += n / p;
n /= p;
}
ret *= PowMod(S[pk - 1], et, pk);
ret %= pk;
return make_pair(ret, ep);
}
int CRT(int x1, int m1, int x2, int m2) // Linear Congruence Equation
{
// assert gcd(m1, m2) == 1
// let x == x1 * k1 + x2 * k2 mod m1*m2
// k1 == 1 mod m1, k1 == 0 mod m2
// k2 == 0 mod m1, k2 == 1 mod m2
pair<int, int> s = ExGcd(m1, m2);
// s.first * m1 + s.second * m2 == 1
int k1 = s.second * m2, k2 = s.first * m1;
int t = (x1 * k1 + x2 * k2) % (m1 * m2);
return t < 0 ? t + m1 * m2 : t;
}
vector<pair<int, int> > Factorize(int n)
{
vector<pair<int, int> > ret;
for(int x = 2; x * x <= n; ++x)
{
if(n % x == 0)
{
int c = 0;
while(n % x == 0)
{
++c;
n /= x;
}
ret.push_back(make_pair(x, c) );
}
}
if(n > 1) ret.push_back(make_pair(n, 1) );
return ret;
}
int Calc(int n, int p)
{
vector<pair<int, int> > factors = Factorize(p);
int x = 0, m = 1;
for(int i = 0; i < factors.size(); ++i)
{
// C(n, k) mod p[i]^k[i]
pair<int, int> f2n = FacMod(n * 2, factors[i].first, factors[i].second);
pair<int, int> fn = FacMod(n, factors[i].first, factors[i].second);
pair<int, int> fn1 = FacMod(n + 1, factors[i].first, factors[i].second);
int pk = Pow(factors[i].first, factors[i].second);
int t = f2n.first * Inv(fn.first, pk) % pk;
t = t * Inv(fn1.first, pk) % pk;
t = t * PowMod(factors[i].first, f2n.second - fn.second - fn1.second, pk) % pk;
x = CRT(x, m, t, pk);
m *= pk;
}
return x;
}
int main()
{
int n, p;
while(scanf("%d%d", &n, &p) != -1)
printf("%d\n", Calc(n-2, p) );
return 0;
}


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

键盘上的舞者

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
08-22 1162

03-26 27
08-08 669
04-02 605
08-12 602