HNU 13101 The Triangle Division of the Convex Polygon 卡特兰数第n项%m(m可为非素数

题目链接:点击打开链接

首先要n-=2,然后就是一个卡特兰数了。

上一题用的是 h(n) = h(n-1) * (4n-2)/(n+1);

这题用的是 h(n) = (2n)! * n! / (n+1)!;

然后对阶乘分解质因数:

点击打开链接

分解完了直接快速幂。

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
#define int __int64
const int N = 100000;
int prime[N],primenum;//有primenum个素数 math.h
void PRIME(int Max_Prime){
	primenum=0;
	prime[primenum++]=2;
	for(int i=3;i<=Max_Prime;i+=2)
	for(int j=0;j<primenum;j++)
		if(i%prime[j]==0)break;
		else if(prime[j]>sqrt((double)i) || j==primenum-1)
		{
			prime[primenum++]=i;
			break;
		}
}
int n, m;
void mul(int &x, int y){
    x *= y;
    if(x >= m) x %= m;
    else if(x<0) x = x%m+m;
}
int Pow(int x, int y){
    int ans = 1;
    while(y){
        if(y&1)
            mul(ans, x);
        mul(x, x);
        y >>= 1;
    }
    return ans;
}
int D[N];
void work1(int x){
    for(int i = 0; prime[i]<=x; i++){
        D[i] = 0;
        for(int j = prime[i]; j <= x; j*=prime[i])
            D[i] += x/j;
    }
}
void work2(int x){
    for(int i = 0; prime[i]<=x; i++)
        for(int j = prime[i]; j <= x; j*=prime[i])
            D[i] -= x/j;
}
void debug(){
    for(int i = 0; i<primenum; i++)
        if(D[i])printf("[%d,%d]\n", prime[i],D[i]);puts("");
}
int work(){
	if(m==1)return 0;
	if(n==1)return 1;
	int res = 1;
    //f[n] = 2n!/n!/(n+1)!;
    work1(2*n);
    work2(n);
    work2(n+1);
    for(int i = 0; prime[i] <= 2*n; i++)
            if(D[i])
                mul(res, Pow(prime[i],D[i]));
    return res;
}
#undef int
int main() {
	PRIME(1000000);
	while(cin>>n>>m){
		n-=2;
		cout<< work()%m <<endl;
	}
	return 0;
}
时间: 2024-10-10 14:57:37

HNU 13101 The Triangle Division of the Convex Polygon 卡特兰数第n项%m(m可为非素数的相关文章

HOJ 13101 The Triangle Division of the Convex Polygon(数论求卡特兰数(模不为素数))

The Triangle Division of the Convex Polygon 题意:求 n 凸多边形可以有多少种方法分解成不相交的三角形,最后值模 m. 思路:卡特兰数的例子,只是模 m 让人头疼,因为 m 不一定是素数,所以不一定存在逆元. 解法:式子为f(n) =  ( C( 2*(n-2),  (n-2) ) / (n-1))   % m :令 p = n-2, 式子可化为:f(p) = ((2*p)! / ( p! * (p+1)! ) ) % m; 对 s!分解质因素,统计个

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

HUNAN 11562 The Triangle Division of the Convex Polygon(大卡特兰数)

http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11562&courseid=0 求n边形分解成三角形的方案数. 就是求n-2个卡特兰数,从大神那盗取了一份模板,效率极高.同时也很复杂. 1 #include <cstdio> 2 #include <cmath> 3 #include <stdlib.h> 4 #include <memory.h> 5 type

[LeetCode] Convex Polygon 凸多边形

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition). Note: There are at least 3 and at most 10,000 points. Coordinates are in the range -10,000 to 10,000. You may assume the

Leetcode: Convex Polygon

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition). Note: There are at least 3 and at most 10,000 points. Coordinates are in the range -10,000 to 10,000. You may assume the

LeetCode469 - Convex Polygon - Medium (Python)

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition). [[0,0],[0,1],[1,1],[1,0]] Answer: True [[0,0],[0,10],[10,10],[10,0],[5,5]] Answer: False 思路:这题问的是给一系列polygon的点,判断其是否是conv

Spoj-TRNGL Make Triangle

Make Triangle Chayanika loves Mathematics. She is learning a new chapter geometry. While reading the chapter a question came in her mind. Given a convex polygon of n sides. In how many ways she can break it into triangles, by cutting it with (n-3) no

HDU6219/POJ1259 [ICPC2017沈阳]Empty Convex Polygons 最大空凸包

Empty Convex Polygons Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 538    Accepted Submission(s): 138 Problem Description Given a set of distinct points S on a plane, we define a convex ho

Convex Hull | Set 1

Given a set of points in the plane. the convex hull of the set is the smallest convex polygon that contains all the points of it. https://www.geeksforgeeks.org/convex-hull-set-1-jarviss-algorithm-or-wrapping/ Lin家 Java: // Java program to find convex