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(数论求卡特兰数(模不为素数))的更多相关文章

  1. 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是在输入中给的,所 ...

  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. UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)

    Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...

  8. HDU 4195 Regular Convex Polygon

    思路:三角形的圆心角可以整除(2*pi)/n #include<cstdio> #include<cstring> #include<iostream> #incl ...

  9. POJ 3410 Split convex polygon(凸包)

    题意是逆时针方向给你两个多边形,问你这两个多边形通过旋转和平移能否拼成一个凸包. 首先可以想到的便是枚举边,肯定是有一对长度相同的边贴合,那么我们就可以n2枚举所有边对,接下来就是旋转点对,那么假设多 ...

随机推荐

  1. shutdown命令详解

    基础命令学习目录 原文链接:http://www.cnblogs.com/qlqwjy/p/7746364.html 我 们在操作Linux v/服务器的时候肯定会有需要重启系统,或者关闭系统等操作. ...

  2. Scrum Meeting 10.31

    成员 今日任务 明日任务 今日工作时长 徐越 整理开发文档,学习ip相关知识,学习servlet相关知识 代码迁移,学习数据库相关知识 5h 赵庶宏 学习学长的servlet代码 进行数据库的连接 4 ...

  3. Leetcode题库——34.在排序数组中国查找元素的第一个和最后一个位置

    @author: ZZQ @software: PyCharm @file: searchRange.py @time: 2018/11/12 19:19 要求:给定一个按照升序排列的整数数组 num ...

  4. 团队GIT实战总结

    项目要求 组长博客 遇到的困难及解决办法 组员1(组长):王彬 遇到的困难  在团队任务分工的时候没有充分照顾到所有人,导致队员们的工作量不均. 现场编程时间不够 解决办法 在此对组员们表示抱歉,由于 ...

  5. JAVA自学日记——Part Ⅰ.

    和C++比较相似,Java同样是面向对象的设计语言,在基础的语句上有一些不大的差别,经过两天的学习,大概的了解了在eclipse中如何进行简单的编程,解决一些简单的问题,诸如在学习C时做过的“字符串倒 ...

  6. PAT 1074 宇宙无敌加法器

    https://pintia.cn/problem-sets/994805260223102976/problems/994805263297527808 地球人习惯使用十进制数,并且默认一个数字的每 ...

  7. Idea(三)常用插件以及快捷键总结

    idea常用插件以及快捷键 现在开发中和日常自己开发都统一换成了idea进行开发了.现在针对自己常用到的插件和快捷键进行总结记录下. 插件 Alibaba Java Coding Guidelines ...

  8. 第212天:15种CSS居中的方式,最全了

    CSS居中是前端工程师经常要面对的问题,也是基本技能之一.今天有时间把CSS居中的方案整理了一下,目前包括水平居中,垂直居中及水平垂直居中方案共15种.如有漏掉的,还会陆续的补充进来,算做是一个备忘录 ...

  9. 重新认识javascript的settimeout和异步

    1.简单的settimeout setTimeout(function () { while (true) { } }, 1000); setTimeout(function () { alert(' ...

  10. HDU4641_K-string

    若它的一个子串出现的次数不少于K次,那么这个子串就是一个K-string. 现给出原串,每次可以向该串后面添加一个字符或者询问当前有多少个不同的K-string. 在线添加查询,解法直指SAM. 其实 ...