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) , 分解质因数去算就可以了... -------------- ...
随机推荐
- B+树比B树更适合实际应用中操作系统的文件索引和数据库索引
B+树比B树更适合实际应用中操作系统的文件索引和数据库索引 为什么选择B+树作为数据库索引结构? 背景 首先,来谈谈B树.为什么要使用B树?我们需要明白以下两个事实: [事实1]不同容量的存储器, ...
- panda读取Excel
pandas读取Excel的第一种方法 方法一:默认读取第一个表单 import pandas print("\n方法一:") xls_data=pd.read_excel('ce ...
- t100 debug常用指令
1. r.d 程式代號 Find (Ctrl+F) => Find Next(F3). 設中斷點(Toggle):Double Click => Run/Continue the ...
- 【题解】Luogu P5294 [HNOI2019]序列
原题传送门 题意:给你一个长度为\(n\)的序列\(A\),每次询问修改一个元素(只对当前询问有效),然后让你找到一个不下降序列\(B\),使得这两个序列相应位置之差的平方和最小,并输出这个最小平方和 ...
- mysql 复制表结构(包括索引等)、表内容
=============================================== 2019/7/16_第1次修改 ccb_warlock == ...
- Java数据结构-ArrayList最细致的解析笔记
ArrayList是一个类,这个类有一个数组参数elementData,ArrayList集合中的元素正是保存在这个数组中,它继承了数组查询的高性能,参考第3篇.ArrayList还封装了很多方法,便 ...
- C#使用CSS选择器抓取页面内容
最近在查wpf绘图资料时,偶然看到Python使用CSS选择器抓取网页的功能.觉得很强,这里用C#也实现一下. 先介绍一下CSS选择器 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. ...
- elasticsearch原理学习笔记
https://mp.weixin.qq.com/s/dn1n2FGwG9BNQuJUMVmo7w 感谢,透彻的讲解 整理笔记 请说出 唐诗中 包含 前 的诗句 ...... 其实你都会,只是想不起 ...
- InheritedWidget and screen
self: import 'package:flutter/material.dart'; class GrantScreen { static double _width, _height; sta ...
- 深入理解JVM(三) -- 对象的内存布局和访问定位
一 对象的内存布局: 在HotSpot虚拟机中,对象在内存中存储的布局可以分为3块区域:对象头(Header),实例数据(Instance Data)和对齐填充(Padding). HotSpot的对 ...