BZOJ1485: [HNOI2009]有趣的数列(Catalan数,质因数分解求组合数)
题意
挺简洁的。
我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件:
(1)它是从1到2n共2n个整数的一个排列{ai};
(2)所有的奇数项满足a1<a3<…<a2n-1,所有的偶数项满足a2<a4<…<a2n;
(3)任意相邻的两项a2i-1与a2i(1≤i≤n)满足奇数项小于偶数项,即:a2i-1<a2i。
现在的任务是:对于给定的n,请求出有多少个不同的长度为2n的有趣的数列。因为最后的答案可能很大,所以只要求输出答案 mod P的值。
Sol
打表后发现时Catalan数。
通项公式:$\frac{C_{2n}^n}{n + 1}$
/* */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define rg register
#define sc(x) scanf("%d", &x);
#define pt(x) printf("%d ", x);
#define db(x) double x
#define rep(x) for(int i = 1; i <= x; i++)
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
char obuf[<<], *O = obuf;
#define OS *O++ = ' ';
using namespace std;
using namespace __gnu_pbds;
const int MAXN = 1e5 + , INF = 1e9 + , mod = 1e9 + ;
const double eps = 1e-;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N;
int a[MAXN], js[MAXN];
bool check() {
for(int i = ; i <= N - ; i += )
if(a[i] >= a[i + ]) return ; for(int i = ; i <= N - ; i += )
if(a[i] >= a[i + ]) return ; for(int i = ; i <= N - ; i += )
if(a[i] >= a[i + ]) return ;
return ;
}
main() {
while() {
N = read() << ;
js[] = ;
for(int i = ; i <= N; i++) js[i] = i * js[i - ];
for(int i = ; i <= N; i++) a[i] = i;
int ans = ;
for(int i = ; i <= js[N]; i++) {
if(check())
ans++;
next_permutation(a + , a + N + );
}
printf("%d\n", ans);
}
return ;
}
/*
1 2 5 14 42 132
*/
打表程序
注意这里的模数不是质数,因此我们没法用逆元来求。
这里有一种最差$O(nlogn)$的算法
首先将每个数质因数分解,统计出每个质数的出现次数(除的话就是减去)
最后一起算即可
考虑到每个数的最小的质因数$ \geqslant 2$,因此极限复杂度为$O(n log n)$
/*
*/
#include<cstdio>
//#define int long long
#define LL long long
const int MAXN = * 1e6 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, mod, prime[MAXN], vis[MAXN], tot, mn[MAXN], num[MAXN];
void GetPhi(int N) {
vis[] = ;
for(int i = ; i <= N; i++) {
if(!vis[i]) prime[++tot] = i, mn[i] = tot;
for(int j = ; j <= tot && (i * prime[j] <= N); j++) {
vis[i * prime[j]] = ; mn[i * prime[j]] = j;
if(!(i % prime[j])) break;
}
}
}
void insert(int x, int opt) {
while(x != ) num[mn[x]] += opt, x = x / prime[mn[x]];
}
int fastpow(int a, int p) {
int base = ;
while(p) {
if(p & ) base = (1ll * base * a) % mod;
a = (1ll * a * a) % mod; p >>= ;
}
return base;
}
main() {
N = read(); mod = read();
GetPhi( * N);
for(int i = N + ; i <= * N; i++) insert(i, );
for(int i = ; i <= N; i++) insert(i, -);
insert(N + , -);
LL ans = ;
for(int i = ; i <= tot; i++)
if(num[i])
ans = (1ll * ans * fastpow(prime[i], num[i])) % mod;
printf("%lld", ans); return ;
}
/*
6 100
1 2 5 14 42 132
*/
BZOJ1485: [HNOI2009]有趣的数列(Catalan数,质因数分解求组合数)的更多相关文章
- bzoj1485: [HNOI2009]有趣的数列(Catalan数)
1485: [HNOI2009]有趣的数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2105 Solved: 1117[Submit][Stat ...
- luogu 3200 [HNOI2009]有趣的数列 卡特兰数+质因数分解
打个表发现我们要求的就是卡特兰数的第 n 项,即 $\frac{C_{2n}^{n}}{n+1}$. 对组合数的阶乘展开,然后暴力分解质因子并开桶统计一下即可. code: #include < ...
- BZOJ 1485: [HNOI2009]有趣的数列( catalan数 )
打个表找一下规律可以发现...就是卡特兰数...卡特兰数可以用组合数计算.对于这道题,ans(n) = C(n, 2n) / (n+1) , 分解质因数去算就可以了... -------------- ...
- BZOJ 1485: [HNOI2009]有趣的数列 [Catalan数 质因子分解]
1485: [HNOI2009]有趣的数列 Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所 ...
- BZOJ1485:[HNOI2009]有趣的数列(卡特兰数)
Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…&l ...
- BZOJ1485: [HNOI2009]有趣的数列(卡特兰数+快速幂)
题目链接 传送门 题面 思路 打表可以发现前六项分别为1,2,5,12,42,132,加上\(n=0\)时的1构成了卡特兰数的前几项. 看别人的题解说把每一个数扫一遍,奇数项当成入栈,偶数项当成出栈, ...
- [HNOI2009]有趣的数列 卡特兰数
题面:[HNOI2009]有趣的数列 题解: 观察到题目其实就是要求从长为2n的序列中选n个放在集合a,剩下的放在集合b,使得集合a和集合b中可以一一对应的使a中的元素小于b. 2种想法(实质上是一样 ...
- bzoj1485: [HNOI2009]有趣的数列(Catalan数)
一眼卡特兰数...写完才发现不对劲,样例怎么输出$0$...原来模数不一定是质数= =... 第一次见到模数不是质数的求组合数方法$(n,m\leq 10^7)$,记录一下... 先对于$1$~$n$ ...
- [bzoj1485][HNOI2009]有趣的数列_卡特兰数_组合数
有趣的数列 bzoj-1485 HNOI-2009 题目大意:求所有1~2n的排列满足奇数项递增,偶数项递增.相邻奇数项大于偶数项的序列个数%P. 注释:$1\le n\le 10^6$,$1\le ...
随机推荐
- liunx下解压压缩命令详细介绍
Linux下的压缩解压缩命令详解及实例 实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip == ...
- js中this 的四种用法
this 在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this ...
- android 通过包名过滤logcat
#!/bin/bash ]]; then cat <<EOF Usage: `` <packagename> EOF exit package_name=$ ip= pid_l ...
- 网页中控制ActiveX插件高度
说明:IE窗口中承载了一个ActiveX插件,试图使该插件充填窗口(自适应窗口的高度.宽度),且不出滚动条. 承载插件的代码如下: <body> <form id=" ...
- eclipse 怎么关闭 show children
转自:http://blog.51cto.com/swordbean/1736994 eclipse 关闭 show children 前段时间使用eclipse时,误按了 shift+alt+B结果 ...
- <正则吃饺子>:关于使用powerDesign连接oracle数据库,导出数据表结构(ER图吧)
最近做的项目中,没有完整的数据库表结构图(ER图),自己就根据服务器上oracle数据库和powerdesign整理一份,但是存在两个问题:1.没有把数据库表的相关备注弄下来:2.数据库表中的主外键关 ...
- OutputDebugString()输出调试的使用
- 51nod 1102 【单调栈】
思路: 对于这个高度往左能延伸最远x,往右能延伸最远y,(x+1+y)*w; 利用单调栈就行了: #include <cstdio> #include <stack> #inc ...
- Codeforces643A【一种暴力】
mdzz,今天好烦啊,连特么暴力都不会写了. 题意是:给你n个数(<=n),然后让你求对于每个数输出含有他最多数量的区间数,还有如果存在相等的话,这个区间算小的那个 思路: 暴力起点,然后从小区 ...
- UVALive 7327【模拟】
题意: 每次方案一个或多个子序列: 每个子序列要整除m 认为分割不同,子序列边界的不同就是不同: 1246有4个 1246 12 46 124 6 12 4 6 思路: 先从整体考虑,因为取膜适用于加 ...