http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11562&courseid=0

求n边形分解成三角形的方案数。

就是求n-2个卡特兰数,从大神那盗取了一份模板,效率极高.同时也很复杂.

 #include <cstdio>
#include <cmath>
#include <stdlib.h>
#include <memory.h>
typedef int typec;
typec GCD(typec a, typec b)
{
return b? GCD(b, a % b) : a;
}
typec extendGCD(typec a, typec b, typec& x, typec& y)
{
if(!b) return x = , y = , a;
typec res = extendGCD(b, a % b, x, y), tmp = x;
x = y, y = tmp -(a/b)*y;
return res;
}
typec power(typec x, typec k)
{
typec res = ;
while(k)
{
if(k&) res *= x;
x *= x, k >>= ;
}
return res;
}
typec powerMod(typec x, typec k, typec m)
{
typec res = ;
while(x %= m, k)
{
if(k&) res *= x, res %= m;
x *= x, k >>= ;
}
return res;
}
typec inverse(typec a, typec p, typec t = )
{
typec pt = power(p, t);
typec x, y;
y = extendGCD(a, pt, x, y);
return x < ? x += pt : x;
}
typec linearCongruence(typec a, typec b, typec p, typec q)
{
typec x, y;
y = extendGCD(p, q, x, y);
x *= b - a, x = p * x + a, x %= p * q;
if(x < ) x += p * q;
return x;
}
const int PRIMEMAX = ;
int prime[PRIMEMAX + ];
int getPrime()
{
memset(prime, , sizeof(int) * (PRIMEMAX + ));
for(int i = ; i <= PRIMEMAX; i++)
{
if(!prime[i]) prime[++prime[]] = i;
for(int j = ; j <= prime[] && prime[j] <= PRIMEMAX/i; j++)
{
prime[prime[j]*i] = ;
if(i % prime[j] == ) break;
}
}
return prime[];
}
int factor[][], facCnt;
int getFactors(int x)
{
facCnt = ;
int tmp = x;
for(int i = ; prime[i] <= tmp / prime[i]; i++)
{
factor[facCnt][] = , factor[facCnt][] = ;
if(tmp % prime[i] == )
factor[facCnt][] = prime[i];
while(tmp % prime[i] == )
factor[facCnt][]++, factor[facCnt][] *= prime[i], tmp /= prime[i];
if(factor[facCnt][]) facCnt++;
}
if(tmp != ) factor[facCnt][] = tmp, factor[facCnt][] = tmp, factor[facCnt++][] = ;
return facCnt;
}
typec combinationModPt(typec n, typec k, typec p, typec t = )
{
if(k > n) return ;
if(n - k < k) k = n - k;
typec pt = power(p, t);
typec a = , b = k + , x, y;
int pcnt = ;
while(b % p == ) pcnt--, b /= p;
b %= pt;
for(int i = ; i <= k; i++)
{
x = n - i + , y = i;
while(x % p == ) pcnt++, x /= p;
while(y % p == ) pcnt--, y /= p;
x %= pt, y %= pt, a *= x, b *= y;
a %= pt, b %= pt;
}
if(pcnt >= t) return ;
extendGCD(b, pt, x, y);
if(x < ) x += pt;
a *= x, a %= pt;
return a * power(p, pcnt) % pt;
}
const typec PTMAX = ;
typec facmod[PTMAX];
void initFacMod(typec p, typec t = )
{
typec pt = power(p, t);
facmod[] = % pt;
for(int i = ; i < pt; i++)
{
if(i % p) facmod[i] = facmod[i - ] * i % pt;
else facmod[i] = facmod[i - ];
}
}
typec factorialMod(typec n, typec &pcnt, typec p, typec t = )
{
typec pt = power(p, t), res = ;
typec stepCnt = ;
while(n)
{
res *= facmod[n % pt], res %= pt;
stepCnt += n /pt, n /= p, pcnt += n;
}
res *= powerMod(facmod[pt - ], stepCnt, pt);
return res %= pt;
}
typec combinationModPtFac(typec n, typec k, typec p, typec t = )
{
if(k > n || p == ) return ;
if(n - k < k) k = n - k;
typec pt = power(p, t), pcnt = , pmcnt = ;
if(k < pt) return combinationModPt(n, k, p, t);
initFacMod(p, t);
typec a = factorialMod(n, pcnt, p, t);
typec b = factorialMod(k, pmcnt, p, t);
b *= b, pmcnt <<= , b %= pt;
typec tmp = k + ;
while(tmp % p == ) tmp /= p, pmcnt++;
b *= tmp % pt, b %= pt;
pcnt -= pmcnt;
if(pcnt >= t) return ;
a *= inverse(b, p, t), a %= pt;
return a * power(p, pcnt) % pt;
}
typec combinationModFac(typec n, typec k, typec m)
{
getFactors(m);
typec a, b, p, q;
for(int i = ; i < facCnt; i++)
{
if(!i) a = combinationModPtFac(n, k, factor[i][], factor[i][]), p = factor[i][];
else b = combinationModPtFac(n, k, factor[i][], factor[i][]), q = factor[i][];
if(!i) continue;
a = linearCongruence(a, b, p, q), p *= q;
}
return a;
}
int main()
{
getPrime();
typec n, k;
while(scanf("%d %d", &n, &k) != EOF)
printf("%d\n", combinationModFac( * (n-), n-, k));
return ;
}

HUNAN 11562 The Triangle Division of the Convex Polygon(大卡特兰数)的更多相关文章

  1. HOJ 13101 The Triangle Division of the Convex Polygon(数论求卡特兰数(模不为素数))

    The Triangle Division of the Convex Polygon 题意:求 n 凸多边形可以有多少种方法分解成不相交的三角形,最后值模 m. 思路:卡特兰数的例子,只是模 m 让 ...

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

  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. HDU 5914 Triangle(打表——斐波那契数的应用)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whos ...

  8. HDU 4195 Regular Convex Polygon

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

  9. POJ 3410 Split convex polygon(凸包)

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

随机推荐

  1. 关于highchts X时间轴比设置时间相差好几个小时的解决

    经过一番查询和研究发现,在曲线图里,x轴的UNIX时间戳是要乘以1000的(通过在线的UNIX转换,结果与原来没有乘以1000的时间戳相差甚远),不然显示的时间会有很大的误差,真是百思不得其解. 另外 ...

  2. Java简答题附答案

    1. Java有没有goto? 有, Goto语句在java中作为保留字, 并没有实现它. 带标号的break, continue局限于循环体中跳转 带标号的goto可以在一个函数(c语言)中任意跳转 ...

  3. C# 使用Epplus导出Excel [1]:导出固定列数据

    C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...

  4. 【搜索 技巧】Letter gaps

    需要一定技巧的搜索题 题目描述 记得做过这样一道题 Ponder This Challenge: In the string CABACB, each letter appears exactly t ...

  5. java.sql.date 插入数据库没有时分秒

    java.sql.date 插入数据库没有时分秒 把java中实体类的sql.date类型改成java.sql.Timestamp类型即可 util.date 转 Timestamp: java.sq ...

  6. linux中test的意义 又可以表示为[]

    测试标志 代表意义 文件名.文件类型 -e 该文件名是否存在 -f 该文件名是否存在且为file -d 该文件名是否存在且为目录 -b 该文件名是否存在且为一个block -c 该文件名是否存在且为一 ...

  7. Verilog学习笔记基本语法篇(三)·········赋值语句(待补充)

    在Verilog HDL语言中,信号有两种赋值方式. A)非阻塞赋值(Non-Blocking)方式(如:b<=a;) (1)在语句块中,上面语句所赋值的变量不能立即为下面的语句所用: (2)块 ...

  8. scheduleWithFixedDelay和scheduleAtFixedRate源码分析

    先放张图,有兴趣的话我再加细节说明. scheduleWithFixedDelay和scheduleAtFixedRate的执行流程都是一样的,如下 ScheduledThreadPoolExecut ...

  9. android sdk 下载

    不知道是因为最近kaihui还是怎么的,打开android sdk官方网站特别的慢,想下载最新版本的platform几乎变成不可能完成的任务,不知道为什么Google不像Apache那样在各国设立镜像 ...

  10. 咕咕咕,skkyk的博客生活从此开始了

    2019-5-9 咕咕咕,skkyk的博客生活从此开始了 我是划水时长两个半小时的个人OIer skkyk,我喜欢打表,暴零,骗分,瞎搞(律师函警告); 2019-6-14 中考结束的第二天 我们苦逼 ...