BZOJ1485: [HNOI2009]有趣的数列(卡特兰数+快速幂)
题目链接
题面

思路
打表可以发现前六项分别为1,2,5,12,42,132,加上\(n=0\)时的1构成了卡特兰数的前几项。
看别人的题解说把每一个数扫一遍,奇数项当成入栈,偶数项当成出栈,然后就是卡特兰数的公式了。
卡特兰数公式为:
&C_{2n}^{n}-C_{2n}^{n+1}&\\
=&\frac{2n!}{n!n!}-\frac{2n!}{(n+1)!(n-1)!}&
\end{aligned}
\]
因为要对P取模,但是P不一定是素数,因此使用快速幂等方法求逆元是无法实现的,因此这个时候我们就将分子的素数的指数求出来然后减去分明的素数的指数,最后通过快速幂来进行求解即可。
代码实现如下
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> piL;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("in","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 2e6 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
int n, m, P;
int p[maxn], isp[maxn], cnt[maxn];
void init() {
for(int i = 2; i <= 2 * n; ++i) p[i] = 1;
for(int i = 2; i * i <= 2 * n; ++i) {
if(p[i]) {
for(int j = i * i; j <= 2 * n; j += i) {
p[j] = 0;
}
}
}
for(int i = 2; i <= 2 * n; ++i) {
if(p[i]) {
isp[m++] = i;
}
}
}
LL qpow(LL x, int n) {
LL res = 1;
while(n) {
if(n & 1) res = res * x % P;
x = x * x % P;
n >>= 1;
}
return res;
}
void get_num(int x, int sign) {
for(int i = 0; i < m && isp[i] <= x; ++i) {
LL num = isp[i];
while(num <= x) {
cnt[i] += sign * x / num;
num *= isp[i];
}
}
}
int solve(int n, int k) {
for(int i = 0; i < m; ++i) cnt[i] = 0;
get_num(n, 1);
get_num(k, -1);
get_num(n - k, -1);
int res = 1;
for(int i = 0; i < m; ++i) {
res = (1LL * res * qpow(isp[i], cnt[i])) % P;
}
return res;
}
int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d%d", &n, &P);
init();
printf("%d\n", (solve(2 * n, n) - solve(2 * n, n + 1) + P) % P);
return 0;
}
BZOJ1485: [HNOI2009]有趣的数列(卡特兰数+快速幂)的更多相关文章
- BZOJ1485:[HNOI2009]有趣的数列(卡特兰数)
Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…&l ...
- [HNOI2009]有趣的数列 卡特兰数
题面:[HNOI2009]有趣的数列 题解: 观察到题目其实就是要求从长为2n的序列中选n个放在集合a,剩下的放在集合b,使得集合a和集合b中可以一一对应的使a中的元素小于b. 2种想法(实质上是一样 ...
- bzoj1485: [HNOI2009]有趣的数列(Catalan数)
1485: [HNOI2009]有趣的数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2105 Solved: 1117[Submit][Stat ...
- bzoj 1485 [HNOI2009]有趣的数列 卡特兰数
把排好序的序列看成一对对括号,要把他们往原数列里塞,所以就是括号序合法方案数 即为卡特兰数 f(n)=Cn2nn+1 求的时候为避免除法,可以O(n)计算每个素数出现次数,最后乘起来,打完之后发现其实 ...
- 【BZOJ 1485】[HNOI2009]有趣的数列 卡特兰数
这个题我是冲着卡特兰数来的所以就没有想到什么dp,当然也没有想到用卡特兰数的原因........... 你只要求出前几项就会发现是个卡特兰数,为什么呢:我们选择地时候要选择奇数位和偶数位,相邻(一对里 ...
- [HNOI2009] 有趣的数列——卡特兰数与杨表
[HNOI 2009] 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…&l ...
- BZOJ1485: [HNOI2009]有趣的数列(Catalan数,质因数分解求组合数)
题意 挺简洁的. 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…<a ...
- luogu 3200 [HNOI2009]有趣的数列 卡特兰数+质因数分解
打个表发现我们要求的就是卡特兰数的第 n 项,即 $\frac{C_{2n}^{n}}{n+1}$. 对组合数的阶乘展开,然后暴力分解质因子并开桶统计一下即可. code: #include < ...
- BZOJ 1485: [HNOI2009]有趣的数列( catalan数 )
打个表找一下规律可以发现...就是卡特兰数...卡特兰数可以用组合数计算.对于这道题,ans(n) = C(n, 2n) / (n+1) , 分解质因数去算就可以了... -------------- ...
随机推荐
- locale区域语言设置
查看当前配置 # 默认配置[maintain@localhost:~]$ locale LANG=zh_CN.utf8 LC_CTYPE="zh_CN.utf8" LC_NUMER ...
- Jsp编写的页面如何适应手机浏览器页面
经常遇到JSP网页需要适配手机设备的尺寸问题 解决: 在JSP加入<meta name="viewport" content="width=device-width ...
- Spring Boot启动时出现WARN:No MyBatis mapper was found in
今天发现spring-boot继承mybatis启动时老是出现WARN: org.mybatis.spring.mapper.ClassPathMapperScanner - No MyBatis m ...
- python结巴分词SEO的应用详解
结巴分词在SEO中可以应用于分析/提取文章关键词.关键词归类.标题重写.文章伪原创等等方面,用处非常多. 具体结巴分词项目:https://github.com/fxsjy/jieba ...
- [QT] - HTTP文件传输服务器#工程源码
简介: 大学时期学习弄的一个小软件,当初做的目的是在实验室的局域网内方便同学之间文件的传输,软件的几个功能截图如正文所示,文末提供工程源码文件,感谢支持! 功能截图: [ 打开软件,选择IP及需绑定的 ...
- 【转】【bat】Bat 中特殊符号
批处理.Bat 中特殊符号的实际作用,Windows 批处理中特殊符号的作用: @\\隐藏命令的回显. ~\\在for中表示使用增强的变量扩展:在set中表示使用扩展环境变量指定位置的字符串:在set ...
- mysql8.0安装时,Unable to connect to any of the specified MySQL hosts
https://blog.csdn.net/u014776759/article/details/88422967
- asp net core Remote Validation 无法验证
[注意这里,Remote Validation是需要引入Jquery插件和启用客户端验证的]
- python requests 超时与重试
一 源起: requests模块作为python爬虫方向的基础模块实际上在日常实际工作中也会涉及到,比如用requests向对方接口url发送POST请求进行推送数据,使用GET请求拉取数据. 但是这 ...
- 深度学习-InfoGAN论文理解笔记
在弄清楚InfoGAN之前,可以先理解一下变分推断目的以及在概率论中的应用与ELBO是什么,以及KL散度 https://blog.csdn.net/qy20115549/article/detail ...