HNU 13101 The Triangle Division of the Convex Polygon 组合数的因式分解求法
题意:
求第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 组合数的因式分解求法的更多相关文章
- HOJ 13101 The Triangle Division of the Convex Polygon(数论求卡特兰数(模不为素数))
The Triangle Division of the Convex Polygon 题意:求 n 凸多边形可以有多少种方法分解成不相交的三角形,最后值模 m. 思路:卡特兰数的例子,只是模 m 让 ...
- HUNAN 11562 The Triangle Division of the Convex Polygon(大卡特兰数)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11562&courseid=0 求n边形分解成三角形的 ...
- [LeetCode] Convex Polygon 凸多边形
Given a list of points that form a polygon when joined sequentially, find if this polygon is convex ...
- Leetcode: Convex Polygon
Given a list of points that form a polygon when joined sequentially, find if this polygon is convex ...
- ACM训练联盟周赛 G. Teemo's convex polygon
65536K Teemo is very interested in convex polygon. There is a convex n-sides polygon, and Teemo co ...
- 【LeetCode】469. Convex Polygon 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计算向量夹角 日期 题目地址:https://leet ...
- HDU 4195 Regular Convex Polygon
思路:三角形的圆心角可以整除(2*pi)/n #include<cstdio> #include<cstring> #include<iostream> #incl ...
- POJ 3410 Split convex polygon(凸包)
题意是逆时针方向给你两个多边形,问你这两个多边形通过旋转和平移能否拼成一个凸包. 首先可以想到的便是枚举边,肯定是有一对长度相同的边贴合,那么我们就可以n2枚举所有边对,接下来就是旋转点对,那么假设多 ...
- HDU4195 Regular Convex Polygon (正多边形、外接圆)
题意: 给你正n边形上的三个点,问n最少为多少 思路: 三个点在多边形上,所以三个点的外接圆就是这个正多边形的外接圆,余弦定理求出每个角的弧度值,即该角所对边的圆周角,该边对应的圆心角为圆心角的二倍. ...
随机推荐
- BZOJ 4012 [HNOI2015]开店 (树分治+二分)
题目大意: 给你一棵树,边有边权,点有点权,有很多次询问,求点权$\in[l,r]$的所有节点到某点$x$的距离之和,强制在线 感觉这个题应该放在动态点分之前做= = 套路方法和动态点分是一样的 每次 ...
- debian 9 配置ati驱动
可以参考debian wiki 1.识别自己显卡驱动 lspci -nn | grep VGA 2.添加源 # Debian "stretch" deb http://httpre ...
- CSS3属性之text-overflow:ellipsis,指定多行文本中任意一行显示...
对于text-overflow:ellipsis,文本超出部分显示...,但要实现这个效果,却有一些必备条件,如下: div{ overflow:hidden; white-space:nowrap; ...
- 循环语句第2种 WHILE ... LOOP END LOOP;
--------第2种-------- WHILE ... LOOP END LOOP; declare n number(3) :=1; begin WHILE n&l ...
- POJ2891 Strange Way to Express Integers (扩展欧几里德)
本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia 题目大意 求解一组同余方程 x ≡ r1 (mod a1) x ≡ r2 (mod a2) x ≡ r ...
- APP热更新方案(转)
本文转载自[http://creator.cnblogs.com/] 博客地址:Zealot Yin 为什么要做热更新 当一个App发布之后,突然发现了一个严重bug需要进行紧急修复,这时候公司各方就 ...
- 【转载】Select函数实现原理分析
Select函数实现原理分析 <原文> select需要驱动程序的支持,驱动程序实现fops内的poll函数.select通过每个设备文件对应的poll函数提供的信息判断当前是否有资源可用 ...
- 应用Spring和Hibernate(C3P0数据池)写数据库交互项目
一.xml的配置文件(application.xml) <?xml version="1.0" encoding="UTF-8"?> <bea ...
- 开源ETL工具kettle--数据迁移
背景 因为项目的需求,须要将数据从Oracle迁移到MSSQL,不是简单的数据复制,而是表结构和字段名都不一样.甚至须要处理编码规范不一致的情况,例如以下图所看到的 watermark/2/text/ ...
- gson的安装和使用
gson的安装和使用 1.安装 2.布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...