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) , 分解质因数去算就可以了... -------------- ...
随机推荐
- commitlint那些事儿
这里主要介绍提交信息用到的 cz 工具集. 一.生成器 commitizen,cz`生成提交说明`,格式化 git commit message. # 全局安装cz npm install -g co ...
- 【Spring Boot学习之三】Spring Boot整合数据源
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.Spring Boot整合Spring JDBC 1.pom.xml <project xmlns=&quo ...
- Swarm 集群并用 Portainer 管理
https://blog.csdn.net/zhrq95/article/details/79430284 使用docker-proxy代理服务(所有节点): docker pull docker.i ...
- 【ARM-Linux开发】Makefile 使用总结
Makefile 使用总结 1. Makefile 简介 Makefile 是和 make 命令一起配合使用的. 很多大型项目的编译都是通过 Makefile 来组织的, 如果没有 Makefile, ...
- ajax处理csrf的三种方式
方式一: $.post({ url: '/get_result/', data: { value0: $('#v1').val(), value1: $('#v2').val(), csrfmiddl ...
- Ubuntu下重启mysql
启动mysql: 方式一:sudo /etc/init.d/mysql start 方式二:sudo service mysql start 停止mysql: 方式一:sudo /etc/init.d ...
- Debian系Linux源码安装Redis5.0.6
一,先在官网下载源码包:https://redis.io/download 二,解压源码包,并cd到解压后的目录: 三,执行make MALLOC=libc: 接着cd src[解压的目录里有这个子目 ...
- [转帖]50 亿美元!微软签下毕马威!JEDI 100 亿美元订单之后又一大单!
50 亿美元!微软签下毕马威!JEDI 100 亿美元订单之后又一大单! https://mp.weixin.qq.com/s/K0SrFNSVK5aOu6TIzhN92Q 前段时间,微软击败亚马逊, ...
- 【转帖】HBase读写的几种方式(二)spark篇
HBase读写的几种方式(二)spark篇 https://www.cnblogs.com/swordfall/p/10517177.html 分类: HBase undefined 1. HBase ...
- 长乐国庆集训Day5-2
T1 彩虹 题目 [题目描述] Mr.Raju和他的一个大家庭外出度假,他们想要乘着彩虹欣赏周围的景色,但是这样最会有一些问题. 在他们家族中,如果一个人想要骑上彩虹,那么他喜欢的所有人和喜欢他的所有 ...