码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-08 23:00:19      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:hunnu

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


题意:

一个正多边形分割成三角形有多少种不同的分法


思路:

不难看出是卡特兰数,实在是数学乏力,超时到身心疲惫,用分解质因子的方法需要980+ms,太不理想了,在别人那里看到了一个不错的代码,只要31ms,于是引用来做模板

地址:http://blog.csdn.net/kongming_acm/article/details/6361176


#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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:hunnu

原文地址:http://blog.csdn.net/libin56842/article/details/47362077

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!