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是在输入中给的,所以我们不能用求阶乘和其逆元的方法来求。因为当m不是素数的时候,可能不存在逆元。
这里,我们把阶乘做质因数分解,然后上下两边约分,即可求出解。
怎么来把这个n!因式分解呢? 我们知道 n!中某个因子x的数量可以用log(n)的方法来求。
详见:这里
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; typedef __int64 ll; const int INF = <<;
const int MAXN = 1e6+;
/*
Catalan Number : C(n, 2n)/(n+1) = (2n)!/[(n+1)!n!]
*/ bool isPrime[MAXN];
int Prime[MAXN], primeCnt; int cnt[MAXN]; ll myPow(ll x, ll y, ll mod) {
ll ret = ;
while (y>) {
if (y&) ret = (ret*x)%mod;
x = (x*x)%mod;
y >>= ;
}
return ret;
} void getPrime() {
primeCnt = ;
for (int i = ; i < MAXN; i++) if (!isPrime[i]) {
Prime[primeCnt++] = i;
for (ll j = (ll)i*i; j < MAXN; j += i)
isPrime[j] = true;
}
} inline void initFactor(int x) {
for (int i = ; Prime[i] <= x; i++)
cnt[i] = ;
} void getFactor(int x, int v) {
for (int i = ; Prime[i]<=x; i++) {
int t = x;
while (t>) {
cnt[i] += (t/Prime[i])*v;
t /= Prime[i];
}
}
} ll getAns(int x, ll mod) {
ll ret = ;
for (int i = ; Prime[i] <= x; i++)
ret = (ret*myPow(Prime[i], cnt[i], mod))%mod;
return ret;
} int main() {
#ifdef Phantom01
freopen("HNU13101.txt", "r", stdin);
#endif //Phantom01 getPrime();
int n, m;
ll ans;
while (scanf("%d%d", &n, &m)!=EOF) {
n -= ;
initFactor(*n);
getFactor(*n, );
getFactor(n+, -);
getFactor(n, -);
printf("%I64d\n", getAns(*n, m));
} return ;
}
HNU 13101 The Triangle Division of the Convex Polygon 组合数的因式分解求法的更多相关文章
- HOJ 13101 The Triangle Division of the Convex Polygon(数论求卡特兰数(模不为素数))
The Triangle Division of the Convex Polygon 题意:求 n 凸多边形可以有多少种方法分解成不相交的三角形,最后值模 m. 思路:卡特兰数的例子,只是模 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 ...
- HDU 4195 Regular Convex Polygon
思路:三角形的圆心角可以整除(2*pi)/n #include<cstdio> #include<cstring> #include<iostream> #incl ...
- POJ 3410 Split convex polygon(凸包)
题意是逆时针方向给你两个多边形,问你这两个多边形通过旋转和平移能否拼成一个凸包. 首先可以想到的便是枚举边,肯定是有一对长度相同的边贴合,那么我们就可以n2枚举所有边对,接下来就是旋转点对,那么假设多 ...
- HDU4195 Regular Convex Polygon (正多边形、外接圆)
题意: 给你正n边形上的三个点,问n最少为多少 思路: 三个点在多边形上,所以三个点的外接圆就是这个正多边形的外接圆,余弦定理求出每个角的弧度值,即该角所对边的圆周角,该边对应的圆心角为圆心角的二倍. ...
随机推荐
- JS table分页
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="paging3.aspx.c ...
- VUE使用中踩过的坑
前言 vue如今可谓是一匹黑马,github star数已居第一位!前端开发对于vue的使用已经越来越多,它的优点就不做介绍了,本篇是我对vue使用过程中以及对一些社区朋友提问我的问题中做的一些总结, ...
- [terry笔记]文件操作
如下记录一次作业: 很显然,我这个应该属于二逼青年版,会在以后更新文艺青年版的答案. 1.模仿sed,一个文件中,用新字符串替换老字符串. # file = input("file_name ...
- stl里面stack的注意事项
1. pop是不返回元素的.因为不能返回引用,只能返回实例.而这个实例是在函数里面初始化的,所以必须在外面再赋值和初始化.而如果实例复制失败,会产生丢失. 2. 而top是可以返回引用的.实际上,返回 ...
- Raspberry PI 系列 —— 裸机点亮LED灯
Raspberry PI 系列 -- 裸机点亮LED灯 背景 近期刚买了Raspberry PI B+,配置执行了官方提供的Raspbian系统,折腾了一周Linux系统,感觉没啥意思,于是就试着想了 ...
- Collection 和 Collections 的差别?
Collection 是 java.util 下的接口,它是各种集合的父接口,继承于它的 接口主要有 Set 和 List:Collections 是个 java.util 下的类.是针对集合的 帮助 ...
- bzoj2768: [JLOI2010]冠军调查(双倍经验最小割)
2768: [JLOI2010]冠军调查 题目:传送门 题解: 双倍经验(1934) 代码: #include<cstdio> #include<cstring> #inclu ...
- GIT将本地项目上传到Github(两种简单、方便的方法)
GIT将本地项目上传到Github(两种简单.方便的方法) 一.第一种方法: 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安 ...
- ES transport client底层是netty实现,netty本质上是异步方式,但是netty自身可以使用sync或者await(future超时机制)来实现类似同步调用!因此,ES transport client可以同步调用也可以异步(不过底层的socket必然是异步实现)
ES transport client底层是netty实现,netty本质上是异步方式,但是netty自身可以使用sync或者await(future超时机制)来实现类似同步调用! 因此,ES tra ...
- hdoj--1016--Prime Ring Problem(递归回溯)
Prime Ring Problem Time ...