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. Nginx 配置优化

    一.开启Gzip 1.参数 gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_comp_level 2;gzip_types text/plain ...

  2. Spring自定义标签解析与实现

           在Spring Bean注册解析(一)和Spring Bean注册解析(二)中我们讲到,Spring在解析xml文件中的标签的时候会区分当前的标签是四种基本标签(import.alias ...

  3. tcp/ip客户端与服务器

    单击“发送数据”把数据发送到指定IP地址的指定端口号 using System; using System.Collections.Generic; using System.ComponentMod ...

  4. 第一次spring冲刺第6天

    鉴于昨天的调查,今天做了个谈论,主要针对以下几个问题 1.我们的客户类型? 2.如何实现他们的需求? 3.他们真正想要什么? 4.如何保证他们的满足度? 5.怎么使得工程不陷入死循环? 6.还存在什么 ...

  5. java实现中值滤波均值滤波拉普拉斯滤波

    目录 来对下面的图像滤波,其实就是对各个像素点进行数学运算的过程 均值滤波 中值滤波 拉普拉斯滤波 Sobel滤波 注意 来对下面的图像滤波,其实就是对各个像素点进行数学运算的过程 均值滤波 均值滤波 ...

  6. NTP同步网络时间

    为什么要同步网络时间呢,这是由于树莓派没有RTC和后备电池,不能像PC机那样关机之后仍可以走时. NTP对时步骤: 1 安装ntpdate sudo apt-get install ntpdate s ...

  7. Sprint 3.0

    3.0----------------------------------------------------- SCRUM 流程的步骤2: Spring 计划 1. 确保product backlo ...

  8. 如何在Sql Server 2000中用一条语句实现树的查询

    以公司的网站栏目表为例,表结构(表名:Subject)如下:   字段名称 字段类型 字段长度 id int 4 NAME varchar 200 PARENTID int 4 CODE varcha ...

  9. selenium之数据驱动框架应用WPS个人中心自动签到

    wps在注册后,有个每日签到的功能,签到后有几率送wps的专属金币[稻米],为了免费获得,又不想每天都是人工去执行签到动作,所以用selenium写了个小脚本,准备用数据驱动框架来完成这个事情,数据驱 ...

  10. 关于.net服务启动注册到zookeeper,但是注册节点20分钟自动消失解决办法

        ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,作用简单描述就是相当于一个中介,服务提供者将服务注册到zk,服务调用者直接从zk获取,zk的作用就是协调     最近碰到公 ...