题意:

  求第n-2个Catalan数 模上 m。

思路:

  Catalan数公式: Catalan[n] = C(n, 2n)/(n+1) = (2n)!/[(n+1)!n!]

  因为m是在输入中给的,所以我们不能用求阶乘和其逆元的方法来求。因为当m不是素数的时候,可能不存在逆元。

  这里,我们把阶乘做质因数分解,然后上下两边约分,即可求出解。

  怎么来把这个n!因式分解呢? 我们知道 n!中某个因子x的数量可以用log(n)的方法来求。

详见:这里

代码:

  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; typedef __int64 ll; const int INF = <<;
const int MAXN = 1e6+;
/*
Catalan Number : C(n, 2n)/(n+1) = (2n)!/[(n+1)!n!]
*/ bool isPrime[MAXN];
int Prime[MAXN], primeCnt; int cnt[MAXN]; ll myPow(ll x, ll y, ll mod) {
ll ret = ;
while (y>) {
if (y&) ret = (ret*x)%mod;
x = (x*x)%mod;
y >>= ;
}
return ret;
} void getPrime() {
primeCnt = ;
for (int i = ; i < MAXN; i++) if (!isPrime[i]) {
Prime[primeCnt++] = i;
for (ll j = (ll)i*i; j < MAXN; j += i)
isPrime[j] = true;
}
} inline void initFactor(int x) {
for (int i = ; Prime[i] <= x; i++)
cnt[i] = ;
} void getFactor(int x, int v) {
for (int i = ; Prime[i]<=x; i++) {
int t = x;
while (t>) {
cnt[i] += (t/Prime[i])*v;
t /= Prime[i];
}
}
} ll getAns(int x, ll mod) {
ll ret = ;
for (int i = ; Prime[i] <= x; i++)
ret = (ret*myPow(Prime[i], cnt[i], mod))%mod;
return ret;
} int main() {
#ifdef Phantom01
freopen("HNU13101.txt", "r", stdin);
#endif //Phantom01 getPrime();
int n, m;
ll ans;
while (scanf("%d%d", &n, &m)!=EOF) {
n -= ;
initFactor(*n);
getFactor(*n, );
getFactor(n+, -);
getFactor(n, -);
printf("%I64d\n", getAns(*n, m));
} return ;
}

HNU 13101 The Triangle Division of the Convex Polygon 组合数的因式分解求法的更多相关文章

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

    The Triangle Division of the Convex Polygon 题意:求 n 凸多边形可以有多少种方法分解成不相交的三角形,最后值模 m. 思路:卡特兰数的例子,只是模 m 让 ...

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

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11562&courseid=0 求n边形分解成三角形的 ...

  3. [LeetCode] Convex Polygon 凸多边形

    Given a list of points that form a polygon when joined sequentially, find if this polygon is convex ...

  4. Leetcode: Convex Polygon

    Given a list of points that form a polygon when joined sequentially, find if this polygon is convex ...

  5. ACM训练联盟周赛 G. Teemo's convex polygon

    65536K   Teemo is very interested in convex polygon. There is a convex n-sides polygon, and Teemo co ...

  6. 【LeetCode】469. Convex Polygon 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计算向量夹角 日期 题目地址:https://leet ...

  7. HDU 4195 Regular Convex Polygon

    思路:三角形的圆心角可以整除(2*pi)/n #include<cstdio> #include<cstring> #include<iostream> #incl ...

  8. POJ 3410 Split convex polygon(凸包)

    题意是逆时针方向给你两个多边形,问你这两个多边形通过旋转和平移能否拼成一个凸包. 首先可以想到的便是枚举边,肯定是有一对长度相同的边贴合,那么我们就可以n2枚举所有边对,接下来就是旋转点对,那么假设多 ...

  9. HDU4195 Regular Convex Polygon (正多边形、外接圆)

    题意: 给你正n边形上的三个点,问n最少为多少 思路: 三个点在多边形上,所以三个点的外接圆就是这个正多边形的外接圆,余弦定理求出每个角的弧度值,即该角所对边的圆周角,该边对应的圆心角为圆心角的二倍. ...

随机推荐

  1. jQuery Validate验证(项目中使用的)

    大致结构是: <script type="text/javascript" src="<%=path %>/js/jquery-1.9.1.min.js ...

  2. [AtCoder Regular Contest 083] Bichrome Tree

    树形DP. 每个点有两个属性:黑色点的权值和,白色点权值和,一个知道另一个也一定知道. 因为只要子树的和它相等的点得权值和不超过x[u],u点的权值总能将其补齐. 设计状态f[u]表示以u为根的子树, ...

  3. [转载] C 陷阱与缺陷( C traps and Pitfalls )

    本文转自 https://www.xuebuyuan.com/1951579.html 自己找工作过程中复习过的书包括<C traps and Pitfalls>,<编程珠玑> ...

  4. java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap

    最近在使用java PiO导入导出Excle在windos本机上运行没有问题: 但是!!问题来了!放到Linux服务器上部署后出现异常 java.lang.NoClassDefFoundError: ...

  5. [剑指offer] 1. 二维数组中的查找 (数组)

    注意是有序数组!! 思路: 1.利用二维数组由上到下,由左到右递增的规律,选取右上角或者左下角的元素a[m][n]与target进行比较, 当target小于元素a[m][n]时,那么target必定 ...

  6. Golang-import-introduce

    本文主要讲解golang中import关键字的用法 import( "fmt" ) //然后在代码里面可以通过如下的方式调用 fmt.Println("hello wor ...

  7. C++ STL 源代码学习(之deque篇)

    stl_deque.h /** Class invariants: * For any nonsingular iterator i: * i.node is the address of an el ...

  8. 数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)

    树形选择排序 (tree selection sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 算法逻辑: 依据节点的大小, ...

  9. Gradle 编译多个project(包括多Library库project依赖)指导

    Gradle Android最新自己主动化编译脚本教程(提供demo源代码) 这篇文章我简单写了基于Gradle2.1 进行的android project和android library的编译实例, ...

  10. matplotlib 可视化 —— matplotlib.patches

    官方帮助文档 patches - Matplotlib 1.5.1 documentation patches 下主要包含的常用图形类有: Eclipse Circle Wedge 1. plt.gc ...