题意:

  求第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. 在使用easyui datagrid在tab中遇到的问题

    当切换tab时,数据加载了,但是table的宽和高不能不能够初始化. 郁闷了好久解决了这个问题: 在页面加载时和切换tab时,获取当前tab的名字,进行内容的初始化 $('a[name="m ...

  2. vscode代码格式化 空格的配置

    一个项目中同事使用webstorm,看我代码的时候说我传上去的会多出空格, VSCode 编辑 Setting.json文件,列出一些可以配置的项目   "javascript.format ...

  3. Nusoap复杂对象的的webService制作

    推荐网址:http://www.scottnichol.com/nusoapprogwsdl.htm摘抄部分如下:服务器端程序 <?php // Pull in the NuSOAP code ...

  4. BZOJ 3413 匹配 (后缀自动机+线段树合并)

    题目大意: 懒得概括了 神题,搞了2个半晚上,还认为自己的是对的...一直调不过,最后终于在jdr神犇的帮助下过了这道题 线段树合并该是这道题最好理解且最好写的做法了,貌似主席树也行?但线段树合并这个 ...

  5. NOIP2018提高组省一冲奖班模测训练(四)

    NOIP2018提高组省一冲奖班模测训练(四) 这次比赛只AC了第一题,而且花了40多分钟,貌似是A掉第一题里面最晚的 而且还有一个半小时我就放弃了…… 下次即使想不出也要坚持到最后 第二题没思路 第 ...

  6. Manacher(最大回文字串)

    很好的讲解 注意两端的字符要不同,同时数组要开大一些 [Manacher]最长回文子串 #include<bits/stdc++.h> #define REP(i, a, b) for(r ...

  7. 使用InstelliJ IDEA创建Spring MVC应用程序

    环境版本 Windows 8.1 IDE:InstelliJ IDEA 13    Spring:Spring 4.1.1 & Spring MVC 4.1.1    WebLogic 10. ...

  8. linux内核(四)内存管理单元MMU

    1,基本概念 一个程序运行时没必要全部都同时装入内存,只需要把当前需要运行的部分装入内存即可,这样就使得一个大程序可以在较小的内存中运行,也使得内存中可以同时装入更多的程序并发执行,从用户角度看,该系 ...

  9. UVALive 5545 Glass Beads

    Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origin ...

  10. ASP.NET-post、get的区别

    post.get的区别 1.get通过把参数加在浏览器的地址栏中提交(最大2K),用post可以进行文件的提交: 2.使用post提交的页面在点击[刷新]按钮的时候浏览器一般会提示"是否重新 ...