HDU 6044 Limited Permutation dfs 统计
来源:互联网 发布:云计算的优缺点 编辑:程序博客网 时间:2024/05/21 06:30
地址
http://acm.hdu.edu.cn/showproblem.php?pid=6044
题意
给出
思路
对一个区间
当递归到只有一个数时,这个区间返回方案数1(长度为1的排列当然只有1个方案),一个区间的方案数就是其两个子区间的方案数相乘再乘上
标程用的笛卡尔树的做法可以用sort()
进行lower_bound()
带来的
不难发现,不可能有两个
代码
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int IOMX = 4096;char buf[IOMX], IOt[50];int bi = IOMX, bn = IOMX;int read(char *s) { while (bn) { for (; bi < bn && buf[bi] <= ' '; ++bi); if (bi < bn) break; bn = fread(buf, 1, IOMX, stdin); bi = 0; } int sn = 0; while (bn) { for (; bi < bn && buf[bi] > ' '; ++bi) s[sn++] = buf[bi]; if (bi < bn) break; bn = fread(buf, 1, IOMX, stdin); bi = 0; } s[sn] = 0; return sn;}bool read(int &x) { if (!read(IOt)) return false; x = atoi(IOt); return true;}#define lowbit(x) (x & (-x))typedef long long LL;const int MAXN = 1e6 + 5;const int MOD = 1e9 + 7;struct Node { int l, r, pos; Node(int l = 0, int r = 0, int pos = 0): l(l), r(r), pos(pos) {} bool operator<(const Node &node) { return l < node.l || (l == node.l && r < node.r); } bool operator==(const Node &node) { return l == node.l && r == node.r; }};int n;int tree_array[MAXN];LL ans;LL factorail_num[MAXN], inv_factorail[MAXN];bool no_solution;Node nodes[MAXN];inline LL Pow(LL a, LL b) { LL ret = 1; for (; b; b >>= 1) { if (b & 1) (ret *= a) %= MOD; (a *= a) %= MOD; } return ret;}inline LL C(int n, int m) { return factorail_num[n] * inv_factorail[m] % MOD * inv_factorail[n - m] % MOD;}LL dfs(int l, int r) { if (l > r) return 1; int idx = lower_bound(nodes, nodes + n, Node(l, r)) - nodes; if (idx == n || nodes[idx].l != l || nodes[idx].r != r) no_solution = true; if (no_solution) return 0; if (l == r) return 1; LL ret1 = dfs(l, nodes[idx].pos - 1); LL ret2 = dfs(nodes[idx].pos + 1, r); return C(r - l, nodes[idx].pos - l) * ret1 % MOD * ret2 % MOD;}int main() { factorail_num[0] = inv_factorail[0] = 1; for (int i = 1; i < MAXN; ++i) { factorail_num[i] = (factorail_num[i - 1] * i) % MOD; inv_factorail[i] = (inv_factorail[i - 1] * Pow(i, MOD - 2)) % MOD; }// cout << C(5, 1) << endl;// cout << C(6, 3) << endl; int kase = 0;// while (~scanf("%d", &n)) {// for (int i = 0; i < n; ++i) scanf("%d", &nodes[i].l);// for (int i = 0; i < n; ++i) scanf("%d", &nodes[i].r); while (read(n)) { for (int i = 0; i < n; ++i) read(nodes[i].l); for (int i = 0; i < n; ++i) read(nodes[i].r); for (int i = 0; i < n; ++i) nodes[i].pos = i + 1; no_solution = false; sort(nodes, nodes + n); for (int i = 1; i < n; ++i) if (nodes[i - 1] == nodes[i]) no_solution = true; ans = dfs(1, n); printf("Case #%d: %I64d\n", ++kase, ans); }}
阅读全文
0 0
- HDU 6044 Limited Permutation dfs 统计
- hdu 6044 Limited Permutation
- HDU 6044 Limited Permutation dfs + 组合数(读入挂)
- HDU 6044 Limited Permutation 想法
- 2017多校第一场 HDU 6044 Limited Permutation 思维,计数,DFS
- HDU-6044 Limited Permutation(计数)
- HDU 6044 Limited Permutation(递归)
- Limited Permutation HDU
- HUD 6044 Limited Permutation
- 2017 多校训练第一场 HDU 6044 Limited Permutation
- HDU 6044 Limited Permutation (组合数+逆元)
- hdu6044 Limited Permutation【读入优化+dfs】
- 2017多校联合第一场 1012题 hdu 6044 Limited Permutation 笛卡尔树 递归
- 2017多校训练赛第一场 HDU 6044 Limited Permutation(虚建笛卡尔树+超级读入挂)
- HDU6044 Limited Permutation[快速输入][分治]
- HDU 2563 统计问题 (DFS + 打表)
- HDU 2563 统计问题 (DFS)
- HDU6044 Limited Permutation (递归,预处理阶乘逆元)
- 关于 MAC 下解压文件乱码的问题
- cookie 使用
- Android adb无线调试2
- 代码混淆
- I/O流
- HDU 6044 Limited Permutation dfs 统计
- jsp探针
- 八款Android 开发者必备的小工具
- cocos2d-x游戏引擎基础知识介绍(一)
- kruskal(最小生成树)
- Gerrit上分支操作记录(创建分支、删除分支)
- 微信小程序(初学篇)——仿美团外卖
- Apache Commons工具包
- spring 整合 redis,以及spring的RedisTemplate如何使用