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!分解质因素,统计个数。设小于等于 s 的素数为 p1, p2, p3, ... , pk;
则各个素因子个数为 :
for i = to k
q = s
num(i) =
while q >
q = q / pi
num(i) += q
end while
end for
所以,我们就可以统计出 f(p) 的素因子及个数,分子 + , 分母 - 。最后计算时用快速幂。
代码:
#include <climits>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstdarg>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <exception>
#include <stdexcept>
#include <memory>
#include <locale>
#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
#include <string>
#include <complex>
#include <valarray> using namespace std; typedef long long ll; const int N = 1e6+; bool tag[N];
int p[N>>];
int t; void prime() {
t = ;
memset(tag, , sizeof tag);
p[t++] = , tag[] = ;
for(int i = ; i < N; i += ) {
if(!tag[i]) p[t++] = i;
for(int j = , k; j < t && (k = i * p[j]) < N; ++j) {
tag[k] = ;
if(i % p[j] == ) break;
}
}
return ;
} int n;
ll m, ans; int zp[N>>], mp[N>>];
int tz, tp; int Factor(int q[], int u) { //分解 n!
int i;
for( i = ; i < t && p[i] <= u; ++i) {
int v = u;
while(v) {
v /= p[i];
q[i] += v;
}
}
return i;
} void cat(int n) {
int nn = n + n;
tz = tp = ;
memset(zp, , sizeof zp);
memset(mp, , sizeof mp); tz = Factor(zp, nn);
tp = Factor(mp, n);
tp = Factor(mp, n+); for(int i = ; i < tp; ++i) zp[i] -= mp[i]; return ;
} ll mult_mod(int a, int b, ll m) {
ll res = 1LL, tt = (ll) a;
while(b) {
if(b&) res = (res * tt) % m;
tt = tt * tt % m;
b >>= ;
}
return res;
} void solve() {
n -= ;
cat(n);
ans = 1LL;
for(int i = ; i < tz; ++i) {
ans = (ans * mult_mod(p[i], zp[i], m)) % m;
}
printf("%I64d\n", ans);
} int main()
{
#ifdef PIT
freopen("c.in", "r", stdin);
#endif // PIT
prime();
while (~scanf("%d %I64d", &n, &m)) {
solve();
} return ;
}
HOJ 13101 The Triangle Division of the Convex Polygon(数论求卡特兰数(模不为素数))的更多相关文章
- 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是在输入中给的,所 ...
- 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 ...
- UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)
Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...
- HDU 4195 Regular Convex Polygon
思路:三角形的圆心角可以整除(2*pi)/n #include<cstdio> #include<cstring> #include<iostream> #incl ...
- POJ 3410 Split convex polygon(凸包)
题意是逆时针方向给你两个多边形,问你这两个多边形通过旋转和平移能否拼成一个凸包. 首先可以想到的便是枚举边,肯定是有一对长度相同的边贴合,那么我们就可以n2枚举所有边对,接下来就是旋转点对,那么假设多 ...
随机推荐
- 用python实现数字图片识别神经网络--启动网络的自我训练流程,展示网络数字图片识别效果
上一节,我们完成了网络训练代码的实现,还有一些问题需要做进一步的确认.网络的最终目标是,输入一张手写数字图片后,网络输出该图片对应的数字.由于网络需要从0到9一共十个数字中挑选出一个,于是我们的网络最 ...
- Python更新库
查看系统里过期的python库,可以用pip命令 [root@vnode33 sim-enb-sgi]# pip list #列出所有安装的库 Package Version ------------ ...
- idea最常使用的快捷键
撤销 反撤销 : Ctrl+Z / Ctrl+Shift+Z 删除一行 : Ctrl+Y 跳到实现类 : Ctrl+Alt+B 重命名文件: shift+F6 控制台放大缩小: ctrl+shif ...
- maven:新建的maven工程需要添加一下插件
//下面的代码是改变maven的默认使用Java版本,本机是jdk1.7所以用1.7<plugins> <plugin> <groupId>org.apache.m ...
- 2-Tenth Scrum Meeting20151210
任务分配 闫昊: 今日完成:请假.(编译) 明日任务:参加会议讨论,安排任务分工. 唐彬: 今日完成:请假.(编译) 明日任务:参加会议讨论,安排任务分工. 史烨轩: 今日完成:请假.(编译) 明日任 ...
- Java第一次试验
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1352 姓名:朱国庆 学号:20135237 成绩: ...
- Hive问题
今天一直遇到一个问题: 在查询最热10个关键词时候总是报错,下图为报错最下面 一直关注着failed的内容,头疼了一天......... 结果实验室老哥给指出问题,是yarnException报错, ...
- Chapter 3 软件项目管理
软件项目具有产品的不可见性.项目的高度不确定性.软件过程的多变化性.软件人员的高流动性的显著特征.有效的软件项目管理集中于人员.产品.过程和项目四个方面.软件项目的生命周期有项目启动.项目规划.项目实 ...
- sqlDataAdapter和SqlCommand的区别
因为DataSet是离线的,所以SqlDataAdapter这个对象是连接DataSet和数据库的桥梁,所有对DataSet的操作(填充,更新等)都要通过他 ado.net数据访问有两种方式: 1.离 ...
- C++ MOOC
相关课程列表: C++远征之起航篇 C++远征之离港篇 C++远征之封装篇 上 C++远征之封装篇 下 C++远征之继承篇 C++远征之多态篇 授课老师:james_yuan 在寒假,我主要选择 C+ ...