题意:

  求第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. freeswitch 注册错误408 - Request Timeout

    1.网络不通(可能虚拟机没打开网络) 2.防火墙没有过滤端口号(关闭防火墙) 3.IP地址错误

  2. 三、Git 分支

    使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线.有人把 Git 的分支模型称为它的`‘必杀技特性’',也正因为这一特性,使得 Git 从众多版本控制系统中脱颖而出. 1.分支简介 ...

  3. echarts图表属性说明

    参考博客: https://blog.csdn.net/luanpeng825485697/article/details/76691965

  4. Windows桌面美化

    [工具链接]链接: https://pan.baidu.com/s/12aUGsu91F8WfaW5HU5ps3g 提取码: dnan [样例] [美化步骤] 1.解压下载文件,安装两个软件: Sta ...

  5. crm 系统项目(三) 自动分页

    crm 系统项目(三) 自动分页 需求: 1. 做一个自动分页, 每15条数据1页 2. 让当前页数在中间显示 3. 上一页, 下一页 注意情况: 1.总页数 小于 规定显示的页数 2. 左右两边极值 ...

  6. zoj 3471 Most Powerful(状压dp+Tsp问题+连续性问题)

    上来直接一波敲键盘,直接套Tsp问题的代码 然后WA 发现貌似这道题没有连续性. Tsp问题是一条路径,一个点到另一个点,多了一个限制,所以就需要加多一维 而这道题没有限制,也就是说那一维不需要加,我 ...

  7. Oracle安装和使用说明

    很久没有安装Oracle了,今天试了一下重新安装Oracle,然后做了一个总结: 一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要 ...

  8. [环境搭建] VS-Visual Studio-IIS Express 支持局域网訪问

    原创作品,转载请注明出处:http://blog.csdn.net/qiujuer/article/details/40350385 使用Visual Studio开发Web网页的时候有这种情况:想要 ...

  9. Swift基础--定位

    // // ViewController.swift // JieCoreLocation // // Created by jiezhang on 14-10-4. // Copyright (c) ...

  10. poj 2488 A Knight&#39;s Journey(dfs+字典序路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...