hdu 6044 Limited Permutation
来源:互联网 发布:linux init 1 编辑:程序博客网 时间:2024/05/21 09:22
题意:给你n个限制,让你求长度为n的排列列有多少种。每个限制描述为,从li 到 ri这段区间,ai为最小值。
分析:对于一个描述为[1,n] 的限制,它所对应的位置一定是1到n的最小值也就是1,那么这个位置就把区间分成了两份。假设这个位置左边有k个,然后从剩下的n-1个数中选出k个放在左边,都是符合题意的。然后把两个小区间做同样的处理。就得到了答案。
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#define mo 1000000007typedef long long ll;namespace fastIO { #define BUF_SIZE 100000 //fread -> read bool IOerror = 0; inline char nc() { static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE; if(p1 == pend) { p1 = buf; pend = buf + fread(buf, 1, BUF_SIZE, stdin); if(pend == p1) { IOerror = 1; return -1; } } return *p1++; } inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } inline void read(int &x) { char ch; while(blank(ch = nc())); if(IOerror) return; for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0'); } #undef BUF_SIZE};using namespace fastIO;using namespace std;const int N = 3e6 + 5;struct p{ int l, r, id; }a[N];ll mul[N];ll ans, tot;int pos;ll quickM(ll a, ll b){ ll ans = 1; while(b){ if(b & 1)ans = ans * a % mo; b >>= 1; a = a * a % mo; } return ans;}int cmp(p a, p b){ return a.l < b.l || a.l == b.l && a.r > b.r;}ll C(ll n, ll m){ return mul[n] * quickM(mul[m], mo - 2) % mo * quickM(mul[n - m], mo - 2) % mo; }void dfs(int l, int r){ int pos = tot; if(r < l) return; if(a[tot].l != l || a[tot].r != r) ans = 0; if(ans == 0) return; if(l == r) { tot++; return; } ans = ans * C(r - l, a[tot].id - l) % mo; tot++; dfs(l, a[pos].id - 1); dfs(a[pos].id + 1, r);}int main(){ int n, kase = 0; mul[0] = 1; for(ll i = 1; i <= 1e6; i++) mul[i] = mul[i - 1] * i % mo; while(read(n), !fastIO::IOerror){ ans = tot = 1; for(int i = 1; i <= n; i++){ read(a[i].l); a[i].id = i; } for(int i = 1; i <= n; i++){ read(a[i].r); } sort(a + 1, a + n + 1, cmp); dfs(1, n); printf("Case #%d: %lld\n", ++kase, ans); } return 0;}
阅读全文
0 0
- hdu 6044 Limited Permutation
- HDU 6044 Limited Permutation 想法
- HDU-6044 Limited Permutation(计数)
- HDU 6044 Limited Permutation dfs 统计
- HDU 6044 Limited Permutation(递归)
- Limited Permutation HDU
- HUD 6044 Limited Permutation
- HDU 6044 Limited Permutation dfs + 组合数(读入挂)
- 2017 多校训练第一场 HDU 6044 Limited Permutation
- HDU 6044 Limited Permutation (组合数+逆元)
- 2017多校第一场 HDU 6044 Limited Permutation 思维,计数,DFS
- 2017多校联合第一场 1012题 hdu 6044 Limited Permutation 笛卡尔树 递归
- 2017多校训练赛第一场 HDU 6044 Limited Permutation(虚建笛卡尔树+超级读入挂)
- HDU6044 Limited Permutation[快速输入][分治]
- hdu6044 Limited Permutation【读入优化+dfs】
- HDU6044 Limited Permutation (递归,预处理阶乘逆元)
- HDU6044-Limited Permutation(fread挂&&阶乘求逆元&&组合数)
- Limited
- 一个简单的循环rsync是否同步完成脚本
- tensorflow语法学习
- log4j2的介绍与使用(配置文件,路径等)
- postfix 实现自动发邮件
- delphi代码编写加载指示器
- hdu 6044 Limited Permutation
- 关于kafka中的timestamp与offset的对应关系
- vue学习总结之路由
- HDU 2516 取石子游戏
- 如何搭建自己的Maven远程私仓
- mysql表名大小写敏感开启关闭
- C++ 模板遇到的问题(1)
- HDU 6060 RXD and dividing
- 创建型设计模式