Limited Permutation
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1240 Accepted Submission(s): 313
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 integers n , (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 modulo 109+7 .
Given the positive integers n , (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 modulo 109+7 .
Input
The input contains multiple test cases.
For each test case:
The first line contains one positive integer n , satisfying 1≤n≤106 .
The second line contains n positive integers l1,l2,⋯,ln , satisfying 1≤li≤i for each 1≤i≤n .
The third line contains n positive integers r1,r2,⋯,rn , satisfying i≤ri≤n for each 1≤i≤n .
It's guaranteed that the sum of n 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 n , satisfying 1≤n≤106 .
The second line contains n positive integers l1,l2,⋯,ln , satisfying 1≤li≤i for each 1≤i≤n .
The third line contains n positive integers r1,r2,⋯,rn , satisfying i≤ri≤n for each 1≤i≤n .
It's guaranteed that the sum of n 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.
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
3 1 1 3 1 3 3 5 1 2 2 4 5 5 2 5 5 5
Sample Output
Case #1: 2 Case #2: 3
Source
题意:
给出n个区间限制,第 i 个区间对应第 i 个位置上的数,满足这个数是这个区间的最小值
满足n个限制的 1 ~ n 的排列数
题解:
既然每一个数字都对应一个区间,那么数字 1 就对应整个区间,至于是第几个区间就看题目给出的数据了
于是乎,找到了这个最大的区间,然后再按照数字1分为两个区间,它的左边和它的右边
它的任意一边这个大区间又满足对应其中的一个最小的数字
就这样分下去
那么:在分左右两个区间的时候,数字怎么分配呢!!!
很简单,就是从 n - 1 个数字里面选择 当前位置减去区间的某一端点(任意端点都行,因为它是组合数)
很快发现求个组合数很麻烦还要求逆元,我试了试利用之前学过的方法
http://blog.csdn.net/summer__show_/article/details/53198863
当a,m不是互质数,计算(b/a)mod m,没办法把b/a转换成b×(a的逆元),可以用 (b/a)mod m == [ b mod (a*m) ] / a 来代替
这样打表很容易溢出,而转化为质数也非常麻烦
然后就只能看把别人的代码当做模板了,这里打表很简单,里面已经备注好了
其次,这个题目的输入简直了,之前没有用过,也只能当做模板了,最后整个题,用了模板后就差不多做完了,哈哈,当做学习吧
代码是这位大神的
http://blog.csdn.net/say_c_box/article/details/76147001
#include<map>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=1000010;
const int mod=1e9+7;
typedef long long LL;
LL inv[MAXN];
LL fac[MAXN];
int l[MAXN],r[MAXN];
typedef pair<int,int>P;
map<P,int> mp;
LL res=1;
int n;
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;
}
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;
}
void init()
{
fac[0]=1;///阶乘
for(int i=1; i<MAXN; i++)
fac[i]=fac[i-1]*i%mod;
inv[0]=inv[1]=1;///逆元
for(int i=2;i<MAXN;i++)
inv[i]=(LL)(mod-mod/i)*inv[mod%i]%mod;
for(int i=1; i<MAXN; i++)///逆元的累乘
inv[i]=inv[i-1]*inv[i]%mod;
}
LL Com(int n,int m)
{
return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
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 main()
{
init();
int cas=1;
freopen("in.txt","r",stdin);
while(read())
{
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);
}
}