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枚举所有边对,接下来就是旋转点对,那么假设多 ...
随机推荐
- Vue 入门之组件化开发
Vue 入门之组件化开发 组件其实就是一个拥有样式.动画.js 逻辑.HTML 结构的综合块.前端组件化确实让大的前端团队更高效的开发前端项目.而作为前端比较流行的框架之一,Vue 的组件和也做的非常 ...
- decode 函数及其用法
http://blog.csdn.net/oscar999/article/details/18399177
- 实践lnmpde 的安装
1.先安装apache, yum install httpd 2.安装MySQL rpm -qa | grep mysql // 这个命令就会查看该操作系统上是否已经安装了mysql数据库 ...
- Dailu Scrum (2015/10/27)
在周日晚上PM已经为大家分配了部分的代码修改工作,今天晚上PM召集了被分配代码工作的3个DEV一起讨论要求修改的代码.在共同讨论的过程中确有发现以下代码的不规范之处,PM当即要求我们先要修改规范代码的 ...
- Class 2 四则运算2的设计思路
设计思路 1.主函数中有一个大的for循环,用户可以一直随机得到相应题目.在嵌套一个循环,其可以直接确定题目数量:定义两个变量,分别作为四则运算的两个运算数,用随机数函数得到两个数值:再利用随机生成函 ...
- Linux系统(X32)安装Oracle11g完整安装图文教程另附基本操作
一.修改操作系统核心参数 在Root用户下执行以下步骤: )修改用户的SHELL的限制,修改/etc/security/limits.conf文件 输入命令:vi /etc/security/limi ...
- 安装AndroidJDK的坑
最近公司要用weex了,先开始搭一下环境,真的都是坑,写下来大家引以为鉴,我踩坑三天的后果. 首先要安装JavaJDK这个过程就不写了都是程序员网上搜索一下很多,注意找论坛上最新的帖子来看,这里有一个 ...
- Hibernate主键注解
http://www.cnblogs.com/hongten/archive/2011/07/20/2111773.html 版权声明:本文为博主原创文章,未经博主允许不得转载.
- PMS---团队展示
点我查看作业原题 [队名] PMS(一群pm) [拟做的团队项目描述] 基于监控场景的视频摘要与人车检测跟踪系统 A system, under monitor scene, for video su ...
- 浏览器播放rtmp流
我是利用flash插件实现的,需要以下几个文件: flowplayer-3.2.8.min.js flowplayer-3.2.18.swf flowplayer.rtmp-3.2.8.swf flo ...