HDU 6044 Limited Permutation(2017多校)【计数 快速读入挂 线性逆元】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6044
题意:
对于有n个元素的全排列的合法性定义为:有n个区间,对于第i个区间[li,ri]有li<=i<=ri,对于任意1<=L<=i<=R<=n,当前仅当li<=L<=i<=R<=ri时P[i]=min(P[L],P[L+1],...,P[R])。
求排列的合法方案数;
解题思路:
大佬讲的很清楚了:https://blog.csdn.net/qq_31759205/article/details/76146845
前期技能:
①快速读入挂
②线性求阶乘逆元
为什么这样排序之后 dfs 一定是合理的呢?
因为题目给出的是 N 个区间, 每个区间对应一个值,因为每个区间的合法定义都是唯一的,
也就是说序列中的每一个值其实对应一个区间,所以dfs区间是合理的,如果区间出现不合法的情况则说明无解。
AC code:
1 #include <bits/stdc++.h> 2 #define INF 0x3f3f3f3f 3 #define LL long long 4 using namespace std; 5 const int MAXN = 1e6+10; 6 const LL mod = 1e9+7; 7 LL fac[MAXN], Inv[MAXN]; 8 9 namespace IO{ 10 const int MAX = 4e7; 11 char buf[MAX]; int c, sz; //预先缓冲到数组buf 12 void begin(){ 13 c = 0; 14 sz = fread(buf, 1, MAX, stdin); 15 } 16 inline bool read(int &t){ 17 while(c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++; 18 if(c >= sz) return false; 19 bool flag = 0; if(buf[c] == '-') flag = 1, c++; 20 for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t*10+buf[c]-'0'; 21 if(flag) t=-t; 22 return true; 23 } 24 } 25 26 void Init() //预处理排列数和逆元 27 { 28 fac[0] = Inv[0] = fac[1] = Inv[1] = 1; 29 for(int i = 2; i < MAXN; i++) fac[i] = fac[i-1]*i%mod; 30 for(int i = 2; i < MAXN; i++) Inv[i] = (mod-mod/i)*Inv[mod%i]%mod; 31 for(int i = 2; i < MAXN; i++) Inv[i] = Inv[i]*Inv[i-1]%mod; 32 } 33 34 LL C(LL n, LL m) //计算组合数 35 { 36 return fac[n]*Inv[m]%mod*Inv[n-m]%mod; 37 } 38 39 struct Node 40 { 41 int l, r; 42 int id; 43 }a[MAXN]; 44 45 bool cmp(const Node s1, const Node s2) 46 { 47 if(s1.l == s2.l) return s1.r > s2.r; 48 return s1.l < s2.l; 49 } 50 int ii; 51 LL dfs(int L, int R) 52 { 53 if(a[ii].l != L || a[ii].r != R) return 0; 54 int mid = a[ii++].id; 55 LL fL = 1LL, fR = 1LL; 56 if(L <= mid-1) fL = dfs(L, mid-1); //左区间方案数 57 if(R >= mid+1) fR = dfs(mid+1, R); //右区间方案数 58 LL cc = C(R-L, mid-L); 59 return fL*fR%mod*cc%mod; 60 } 61 62 int main() 63 { 64 Init(); 65 int N, Case = 1; 66 IO::begin(); 67 while(IO::read(N)){ 68 for(int i = 1;i <= N; i++) IO::read(a[i].l); 69 for(int i = 1;i <= N; i++) IO::read(a[i].r), a[i].id = i; 70 sort(a+1, a+1+N, cmp); ii = 1; 71 LL ans = dfs(1, N); 72 printf("Case #%d: %lld\n", Case++, ans); 73 } 74 return 0; 75 }
View Code
转载于:https://www.cnblogs.com/ymzjj/p/10804618.html
HDU 6044 Limited Permutation(2017多校)【计数 快速读入挂 线性逆元】相关推荐
- HDU 6044 Limited Permutation 读入挂+组合数学
Limited Permutation Problem Description As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplica ...
- hdu 6035:Colorful Tree (2017 多校第一场 1003) 【树形dp】
题目链接 单独考虑每一种颜色,答案就是对于每种颜色至少经过一次这种的路径条数之和.反过来思考只需要求有多少条路径没有经过这种颜色即可. 具体实现过程比较复杂,很神奇的一个树形dp,下面给出一个含较详细 ...
- hdu 6205 card card card 尺取+超神读入挂
题没啥好说的 我就是想把读入挂记录一下 update(2017-10-03) 把读入挂加上了调试 1 #include<bits/stdc++.h> 2 #define cl(a,b) m ...
- 2017 多校3 hdu 6061 RXD and functions
2017 多校3 hdu 6061 RXD and functions(FFT) 题意: 给一个函数\(f(x)=\sum_{i=0}^{n}c_i \cdot x^{i}\) 求\(g(x) = f ...
- 2017 多校2 hdu 6053 TrickGCD
2017 多校2 hdu 6053 TrickGCD 题目: You are given an array \(A\) , and Zhu wants to know there are how ma ...
- hdu6103[尺取法] 2017多校6
/*hdu6103[尺取法] 2017多校6*/ #include <bits/stdc++.h> using namespace std; int T, m; char str[2000 ...
- 2017 多校4 Wavel Sequence
2017 多校4 Wavel Sequence 题意: Formally, he defines a sequence \(a_1,a_2,...,a_n\) as ''wavel'' if and ...
- 2017 Vue.js 2快速入门指南
注意,据部分读者反映本文水多,怕湿身者勿进.后续推荐详解 Vue & Vuex 实践 2017 Vue.js 2快速入门指南翻译自Vue.js 2 Quickstart Tutorial 20 ...
- 2017多校第3场 HDU 6058 Kanade's sum 双链表,思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6058 题外话:这场多校,真心感觉自己的无力,全队都发挥的很差,结束的时候排名掉到了90多,后期没做出字 ...
最新文章
- signature=2ee20a16234208d4dd8bbd7fe87bb472,sstk-20200428
- iOS的那个漏洞并不只是影响苹果设备CVE-2017-6975 Google称影响了所有使用Broadcom Wi-Fi SoC的设备...
- 【五线谱】五线谱的常用符号 ( 花连谱号 | 高音谱号 | 低音谱号 | 休止符 | 小节线 )
- java编译器代码检查_java 命名代码检查-注解处理器
- mysql数据库没有密码_MySQL用户数据库没有密码列-在OSX上安装MySQL
- plot画分段函数_python画图函数大全
- matlab里dcgain,制系统的时域分析
- k3刷梅林5g信号不稳定_斐讯k3 5g信号不稳定 k3c路由器,现在还能入手吗?
- ftp文件服务器怎么迁移,ftp文件服务器迁移
- Java生成二维码,Spring Boot整合ZXing实现二维码生成,支持自定义二维码
- 如何将c语言程序变成应用,C语言代码转换为应用程序
- Google网站流量统计工具
- php内存设置,修改php运行内存大小的限制
- math.abs() java_Java中使用Math.abs你入坑了?
- 如何快速一键重装系统 一键重装系统图文教程
- JS数据交互:动态从数据库中获取数据填充Select
- 目标检测算法——SSD详解
- Camtasia2023最好用的电脑屏幕录制软件
- Python开发_励志小公式分享
- MATLAB之楚列斯基分解法(九)