HDU6044 Limited Permutation (递归,预处理阶乘逆元)
来源:互联网 发布:csol大刀优化参数 编辑:程序博客网 时间:2024/06/07 00:29
Limited Permutation
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 680 Accepted Submission(s): 130
Problem Description
As to a permutation p1,p2,⋯,pn from 1 to n , it is uncomplicated for each 1≤i≤n to calculate (li,ri) meeting the condition that min(pL,pL+1,⋯,pR)=pi if and only if li≤L≤i≤R≤ri for each 1≤L≤R≤n .
Given the positive integersn , (li,ri) (1≤i≤n) , you are asked to calculate the number of possible permutations p1,p2,⋯,pn from 1 to n , meeting the above condition.
The answer may be very large, so you only need to give the value of answer modulo109+7 .
Given the positive integers
The answer may be very large, so you only need to give the value of answer modulo
Input
The input contains multiple test cases.
For each test case:
The first line contains one positive integern , satisfying 1≤n≤106 .
The second line containsn positive integers l1,l2,⋯,ln , satisfying 1≤li≤i for each 1≤i≤n .
The third line containsn positive integers r1,r2,⋯,rn , satisfying i≤ri≤n for each 1≤i≤n .
It's guaranteed that the sum ofn in all test cases is not larger than 3⋅106 .
Warm Tips for C/C++: input data is so large (about 38 MiB) that we recommend to use fread() for buffering friendly.
For each test case:
The first line contains one positive integer
The second line contains
The third line contains
It's guaranteed that the sum of
Warm Tips for C/C++: input data is so large (about 38 MiB) that we recommend to use fread() for buffering friendly.
size_t fread(void *buffer, size_t size, size_t count, FILE *stream); // reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by buffer; the total number of elements successfully read is returned.
Output
For each test case, output "Case #x : y " in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.
Sample Input
31 1 31 3 351 2 2 4 55 2 5 5 5
Sample Output
Case #1: 2Case #2: 3
Source
2017 Multi-University Training Contest - Team 1
Recommend
liuyiding | We have carefully selected several similar problems for you: 6044 6043 6042 6041 6040
对于一个1-n的排列,对每个位置给出一个l[i]和一个r[i],该位置的数要满足在这段区间内是最小值.若范围超多这段区间,则不是最小值.
考虑L,R这一段区间,先找到覆盖整个区间的那对l[i]和r[i],它的位置一定是这段区间内的值的最小值.
递归地考虑左区间和右区间,整个区间的解的个数为左区间解的个数*右区间解的个数*乘以C(区间长度,左区间长度)
预处理组合数的时候一定不能用递归的方法!!!!!!!!
T到怀疑人生
#include <cstring>#include <iostream>#include <stdio.h>#include <algorithm>#include <cmath>#include <map>#include<time.h>using namespace std;const int MAXN=1000010;const int mod=1e9+7;typedef long long LL;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline bool rea(int & x){ char c=nc();x=0; if(c==EOF) return false; for(;c>'9'||c<'0';c=nc()); for(;c>='0'&&c<='9';x=x*10+c-'0',c=nc()); return true;}inline bool rea(LL & x){ char c=nc();x=0; if(c==EOF) return false; for(;c>'9'||c<'0';c=nc()); for(;c>='0'&&c<='9';x=x*10+c-'0',c=nc()); return true;}LL inv[MAXN];LL fac[MAXN];LL Com(int n,int m){ return fac[n]*inv[m]%mod*inv[n-m]%mod;}void init(){ inv[0]=fac[0]=1; inv[1]=1; for(int i=1;i<MAXN;i++){ fac[i]=fac[i-1]*i%mod; } inv[1]=1; for(int i=2;i<MAXN;i++){ inv[i]=(LL)(mod-mod/i)*inv[mod%i]%mod; } inv[0]=1; for(int i=1;i<MAXN;i++){ inv[i]=inv[i-1]*inv[i]%mod; }}typedef pair<int,int> P;map<P,int> mp;int l[MAXN],r[MAXN];LL res=1;void dfs(int L,int R){ if(res==0) return; if(R<L) return; int x=mp[P(L,R)]; if(x==0){ res=0; return ; } if(L==R) return; int len=R-L; int tt=x-L; res=res*Com(len,tt)%mod; dfs(L,x-1); dfs(x+1,R);}int n;bool read(){ bool res=rea(n); if(res==false){ return false; } for(int i=1;i<=n;i++) rea(l[i]); for(int i=1;i<=n;i++) rea(r[i]); return true;}int main(){ init(); int cas=1; while(read()){ int ok=1; res=1; mp.clear(); for(int i=1;i<=n;i++){ mp[P(l[i],r[i])]=i; } dfs(1,n); printf("Case #%d: %lld\n",cas++,res); }}
阅读全文
0 2
- HDU6044 Limited Permutation (递归,预处理阶乘逆元)
- HDU6044-Limited Permutation(fread挂&&阶乘求逆元&&组合数)
- HDU6044 Limited Permutation[快速输入][分治]
- hdu6044 Limited Permutation【读入优化+dfs】
- 预处理阶乘逆元
- HDU 6044 Limited Permutation (组合数+逆元)
- HDU 6044 Limited Permutation(递归)
- 组合数取模,预处理阶乘逆元模板
- HDU4675 GCD of Sequence(预处理阶乘逆元+推公式)
- Limited Permutation HDU
- hdu 6044 Limited Permutation
- HUD 6044 Limited Permutation
- 阶乘和阶乘逆元
- 2017多校联合第一场 1012题 hdu 6044 Limited Permutation 笛卡尔树 递归
- HDU 6044 Limited Permutation 想法
- 阶乘逆元
- 阶乘逆元 记一下
- 【Lightoj 1067】+ 逆元 + 预处理
- 安全态势,交互发现 —— 基于阿里云轻松搭建安全大屏
- poj Sunscreen
- 剑指offer题解C++【4】重建二叉树
- 对于Android中接口回调机制的理解
- liferay-ui:tabs
- HDU6044 Limited Permutation (递归,预处理阶乘逆元)
- 手机网页图片自适应大小 background-size css 图片全屏 背景尺寸设置
- vue.js框架入门学习I
- _tmain和main的区别
- ora-14551 cannot perform a DML operation inside a query
- android TextView加边框&drawable开发
- CSS 样式书写规范
- lua中的协同程序
- 博主招募令 | 技术大舞台 有才你就来!【微信小程序联盟】