HDU 6044--Limited Permutation(搜索+组合数+逆元)

题目链接

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.

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.

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

题意:对于一个由1~n组成的排列称为合法的,必须满足这样的条件: 有n个(li,ri),对于每个 i 必须满足 min(pL,pL+1,?,pR)=pi if and only if li≤L≤i≤R≤ri for each 1≤L≤R≤n.

现在给了n个(li,ri)求满足的排列有多少个。

思路:对于每个(li,ri),a[li-1]<a[i]>a[ri+1] ,并且a[i]是a[li]~a[ri]的最小值,那么可以想到:对于区间(1,n)一定有一个最小值,所以一定有一个区间是(1,n)(用X表示),那么这个最小值把区间X分成两部分U和V ,所以一定存在为U和V的区间,如果不存在,那么输出0,令f(X)表示符合条件的区间X的排列数,那么f(X)=f(U)*f(V)*C(U+V+1,U)   【注:C()表示组合数,U 表示区间U的大小】,所以我们只需要从区间(1,n)进行深搜即可,其中因为数据太大取模会用到逆元和组合数。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int N=1e6+5;
const LL mod=1e9+7;
LL fac[N], Inv[N];
/*int Scan()///输入外挂
{
    int res=0,ch,flag=0;
    if((ch=getchar())==‘-‘)
        flag=1;
    else if(ch>=‘0‘&&ch<=‘9‘)
        res=ch-‘0‘;
    while((ch=getchar())>=‘0‘&&ch<=‘9‘)
        res=res*10+ch-‘0‘;
    return flag?-res:res;
}*/
namespace IO {
    const int MX = 4e7; //1e7占用内存11000kb
    char buf[MX]; int c, sz;
    void begin() {
        c = 0;
        sz = fread(buf, 1, MX, stdin);
    }
    inline bool read(int &t) {
        while(c < sz && buf[c] != ‘-‘ && (buf[c] < ‘0‘ || buf[c] > ‘9‘)) c++;
        if(c >= sz) return false;
        bool flag = 0; if(buf[c] == ‘-‘) flag = 1, c++;
        for(t = 0; c < sz && ‘0‘ <= buf[c] && buf[c] <= ‘9‘; c++) t = t * 10 + buf[c] - ‘0‘;
        if(flag) t = -t;
        return true;
    }
}

void Init(){
    fac[0] = Inv[0] = fac[1] = Inv[1] = 1;
    for(int i=2; i<N; i++) fac[i] = fac[i-1] * i % mod;
    for(int i=2; i<N; i++) Inv[i] = (mod - mod / i) * Inv[mod % i] % mod;
    for(int i=2; i<N; i++) Inv[i] = Inv[i] * Inv[i-1] % mod;
}
LL C(LL n, LL m){
    LL ans = fac[n] * Inv[m] % mod* Inv[n-m] %mod;
    return ans;
}
int ii;
struct Node{
    int l,r;
    int id;
}a[N];
bool cmp(const Node s1,const Node s2)
{
    if(s1.l==s2.l) return s1.r>s2.r;
    return s1.l<s2.l;
}
LL dfs(int L,int R)
{
    if(a[ii].l!=L || a[ii].r!=R)  return 0;
    int m=a[ii++].id;
    LL fL=1,fR=1;
    if(L<=m-1) fL=dfs(L,m-1);
    if(m+1<=R) fR=dfs(m+1,R);
    LL c=C(R-L,m-L);
    return fL*fR%mod*c%mod;
}
int main()
{
    Init();
    int n,Case=1;
    IO::begin();
    while(IO::read(n))
    {
       for(int i=1;i<=n;i++)  IO::read(a[i].l);
       for(int i=1;i<=n;i++)  IO::read(a[i].r), a[i].id = i;
       sort(a+1,a+n+1,cmp);
       ii=1;
       LL ans=dfs(1,n);
       printf("Case #%d: %lld\n",Case++,ans);
    }
    return 0;
}
时间: 2024-11-06 09:15:59

HDU 6044--Limited Permutation(搜索+组合数+逆元)的相关文章

HDU 6044 Limited Permutation 读入挂+组合数学

Limited Permutation 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 positiv

【HDU 5698】瞬间移动(组合数,逆元)

x和y分开考虑,在(1,1)到(n,m)之间可以选择走i步.就需要选i步对应的行C(n-2,i)及i步对应的列C(m-2,i).相乘起来. 假设$m\leq n$$$\sum_{i=1}^{m-2} C_{n-2}^i\cdot C_{m-2}^i=\sum_{i=1}^{m-2} C_{n-2}^i\cdot C_{m-2}^{m-2-i}=C_{n+m-4}^{m-2}$$然后标程里求i的阶乘的逆是预处理的,主要这句:$$f[i]=(M-M/i)\cdot f[M\%i]\%M$$这里f即i

hdu 4945 2048 (dp+组合数)

2048 Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 840    Accepted Submission(s): 199 Problem Description Teacher Mai is addicted to game 2048. But finally he finds it's too hard to get 2048.

HDU 4937 Lucky Number 搜索

题意: 给你一个数,求在多少种不同的进制下这个数每一位都是3.4.5.6中的一个. 思路: 搜索.枚举这个数在任意进制下的表示,判断是否合法.当数字只有3.4.5.6时,必定有无穷种. 因为数字太大,所以直接枚举必定会超时. 下面有两种剪枝的方法: 1. 先枚举最后一位的情况. 假设数字n在base进制下表示为 a[n]...a[0],即 n = a[0] + a[1]*base^1 + ... + a[n]*base^n. 则 n - a[0] = a[1]*base^1 + ... + a[

HDU 1078 记忆化搜索

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4575 Accepted Submission(s): 1829 Problem Description FatMouse has stored some cheese in a city. The city can be considered as a

hdu 4960 记忆化搜索 DP

Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 490    Accepted Submission(s): 180 Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morni

HDU - 6143 Killer Names(dp记忆化搜索+组合数)

题意:从m种字母中选取字母组成姓名,要求姓和名中不能有相同的字母,姓和名的长度都为n,问能组成几种不同的姓名. 分析: 1.从m种字母中选取i种组成姓,剩下m-i种组成名. 2.i种字母组成长度为n的姓-----可转换成用i种颜色给n个球染色,记忆化搜索 dfs(n,i)---用i种颜色给n个球染色的方案数 先给第1个小球涂色,有m种选择,假设涂色为color[1], 那么剩下n-1个小球: 如果继续使用color[1],则问题转化为用m种颜色给n-1个小球涂色: 如果不再使用color[1],

2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 802    Accepted Submission(s): 269 Problem Description hannnnah_j is a teacher in WL High school who teaches biolog

hdu NPY and girls 莫队+逆元

NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description NPY's girlfriend blew him out!His honey doesn't love him any more!However, he has so many girlfriend candidates.Because there are to