题意

挺简洁的。

我们称一个长度为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数,质因数分解求组合数)的更多相关文章

  1. bzoj1485: [HNOI2009]有趣的数列(Catalan数)

    1485: [HNOI2009]有趣的数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2105  Solved: 1117[Submit][Stat ...

  2. luogu 3200 [HNOI2009]有趣的数列 卡特兰数+质因数分解

    打个表发现我们要求的就是卡特兰数的第 n 项,即 $\frac{C_{2n}^{n}}{n+1}$. 对组合数的阶乘展开,然后暴力分解质因子并开桶统计一下即可. code: #include < ...

  3. BZOJ 1485: [HNOI2009]有趣的数列( catalan数 )

    打个表找一下规律可以发现...就是卡特兰数...卡特兰数可以用组合数计算.对于这道题,ans(n) = C(n, 2n) / (n+1) , 分解质因数去算就可以了... -------------- ...

  4. BZOJ 1485: [HNOI2009]有趣的数列 [Catalan数 质因子分解]

    1485: [HNOI2009]有趣的数列 Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所 ...

  5. BZOJ1485:[HNOI2009]有趣的数列(卡特兰数)

    Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…&l ...

  6. BZOJ1485: [HNOI2009]有趣的数列(卡特兰数+快速幂)

    题目链接 传送门 题面 思路 打表可以发现前六项分别为1,2,5,12,42,132,加上\(n=0\)时的1构成了卡特兰数的前几项. 看别人的题解说把每一个数扫一遍,奇数项当成入栈,偶数项当成出栈, ...

  7. [HNOI2009]有趣的数列 卡特兰数

    题面:[HNOI2009]有趣的数列 题解: 观察到题目其实就是要求从长为2n的序列中选n个放在集合a,剩下的放在集合b,使得集合a和集合b中可以一一对应的使a中的元素小于b. 2种想法(实质上是一样 ...

  8. bzoj1485: [HNOI2009]有趣的数列(Catalan数)

    一眼卡特兰数...写完才发现不对劲,样例怎么输出$0$...原来模数不一定是质数= =... 第一次见到模数不是质数的求组合数方法$(n,m\leq 10^7)$,记录一下... 先对于$1$~$n$ ...

  9. [bzoj1485][HNOI2009]有趣的数列_卡特兰数_组合数

    有趣的数列 bzoj-1485 HNOI-2009 题目大意:求所有1~2n的排列满足奇数项递增,偶数项递增.相邻奇数项大于偶数项的序列个数%P. 注释:$1\le n\le 10^6$,$1\le ...

随机推荐

  1. BZOJ_4066_简单题_KDtree

    BZOJ_4066_简单题_KDtree Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1&l ...

  2. AtCoder Grand Contest 007 E:Shik and Travel

    题目传送门:https://agc007.contest.atcoder.jp/tasks/agc007_e 题目翻译 现在有一个二叉树,除了叶子每个结点都有两个儿子.这个二叉树一共有\(m\)个叶子 ...

  3. hibernate学习四 hibernate关联关系映射

    在Hibernate中对象之间的关联关系表现为数据库中表于表之间的关系(表之间通过外键关联). 1 单向的一对一 主键关联  外键关联 2 单向的一对多 3 单向的多对一 4 单向的多对多 5 双向的 ...

  4. css样式 -- 表格不会因为字体过长导致字体溢出的问题

    常常碰到因为表格大小就麽大了,字体过长会爆炸溢出的问题,我们后端就用这个可以了,溢出的可以省略号 ... 代替好了. /* 在表格css样式加上这三个就可以了 效果就会变成 “abc...” */ { ...

  5. storyBoard学习教程二(页面跳转)

    本篇是接着上一篇 storyBoard 学习教程一 的补充,有过storyBoard 编程经验的伙伴还是不要阅读本篇博客了,我自己认为,太基础太简单了,为了方便别人学习使用,我还是详细的做了这篇教程. ...

  6. Hue离线安装

    参考 https://blog.csdn.net/xuxudede1989/article/details/50359023 http://cloudera.github.io/hue/latest/ ...

  7. Asset Catalog Help (六)---Adding OS X Icons

    Adding OS X Icons Simplify image management by storing your OS X icons in an asset catalog. 把OS X图标存 ...

  8. g2o待总结

    http://blog.csdn.net/u010566411/article/details/53862601

  9. sql #与$的区别

    #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by “111”, 如果传入的值是i ...

  10. Codeforces Round #439 (Div. 2)C - The Intriguing Obsession(简单dp)

    传送门 题意 给出三个集合,每个集合的元素数量为a,b,c,现在需要连边,满足集合内元素不可达或最短路为3,求可行方案数 分析 设dp[i][j]为a集合元素为i个,b集合元素为j个的可行方案,易知( ...