题意

挺简洁的。

我们称一个长度为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. 检测SSL证书很好用的三个网站

    https://cryptoreport.websecurity.symantec.com/checker/views/certCheck.jsp https://cipherli.st/ https ...

  2. 「LuoguP3796」 【模板】AC自动机(加强版)

    题目描述 有N个由小写字母组成的模式串以及一个文本串T.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串T中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据. 每组数据的第一 ...

  3. Tutte矩阵求一般图最大匹配

    [集训队2017论文集] 一张无向图的Tutte矩阵为 其中xi,j为一个random的值. Tutte矩阵的秩(一定为偶数)/2 就是这张图的最大匹配. 原理大概就是: 一个图有完美匹配,则det( ...

  4. Exception in thread “main” java.sql.SQLException: The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents more than one time zone.

    Exception in thread “main” java.sql.SQLException: The server time zone value ‘�й���׼ʱ��’ is unrecogn ...

  5. Tensorflow基础知识

    基本知识 使用 TensorFlow, 你必须明白 TensorFlow: 使用图 (graph) 来表示计算任务. 在被称之为 会话 (Session) 的上下文 (context) 中执行图. 使 ...

  6. linux命令:rsync

    Rsync的命令格式可以为以下六种: rsync [OPTION]... SRC DEST rsync [OPTION]... SRC [USER@]HOST:DEST rsync [OPTION]. ...

  7. matlab 2017版本修改帮助文档为离线

    安装matlab2017a后发现,帮助文档为在线版本,必须关联账号. 经过查询资料,帮助文档默认是使用web, on mathworks.com 可以将帮助文档改为离线版本. 具体修改方法: Pref ...

  8. 【网络爬虫】【python】网络爬虫(二):网易微博爬虫软件开发实例(附软件源码)

    对于urllib2的学习,这里先推荐一个教程<IronPython In Action>,上面有很多简明例子,并且也有很详尽的原理解释:http://www.voidspace.org.u ...

  9. C#实现简易ajax调用后台方法

    在当前WEB当中,有些人都会抛弃asp.net的服务器控件,转而使用ajax来进行数据的交互和存储. 当我们大量使用ajax的时候,对于新手而言,肯定会创建很多的ashx或aspx页面,通过拼接参数, ...

  10. PorterDuffXfermode 图像混合技术在漫画APP中的应用

    此文已由作者游葳授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 写在开头 随着应用开发的深入,视觉同学在完成了页面的基本设计后,再也按耐不住心中的寂寞,开始对各种细节不满意, ...