hdu-5719 Arrange(组合数学)

题目链接:

Arrange

Time Limit: 8000/4000 MS (Java/Others)   

 Memory Limit: 262144/262144 K (Java/Others)


Problem Description
 
Accidentally, Cupid, god of desire has hurt himself with his own dart and fallen in love with Psyche. 

This has drawn the fury of his mother, Venus. The goddess then throws before Psyche a great mass of mixed crops.

There are n heaps of crops in total, numbered from 1 to n

Psyche needs to arrange them in a certain order, assume crops on the i-th position is Ai.

She is given some information about the final order of the crops:

1. the minimum value of A1,A2,...,Ai is Bi.

2. the maximum value of A1,A2,...,Ai is Ci.

She wants to know the number of valid permutations. As this number can be large, output it modulo 998244353.

Note that if there is no valid permutation, the answer is 0.
 

 

Input
The first line of input contains an integer T (1T15), which denotes the number of testcases.

For each test case, the first line of input contains single integer n (1n10^5).

The second line contains n integers, the i-th integer denotes Bi (1Bin).

The third line contains n integers, the i-th integer denotes Ci (1Cin).
 

 

Output
For each testcase, print the number of valid permutations modulo 998244353.
 

 

Sample Input
2
3
2 1 1
2 2 3
5
5 4 3 2 1
1 2 3 4 5
 

 

Sample Output
1
0
 
 
题意:

有多少个[1,n]的全排列,满足b[],c[];
b[i]为[1,i]的最小值,c[i]为[1,i]的最大值;
 
思路:
 
合法的b[],c[]应该是一个单调的函数;
每次可以得到一个区间[b[i]+1,c[i]-1],再减去这个区间里面的已经被用的数就是这一位能选的数目了;相乘取模就行;
 
 
AC代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}

const LL mod=998244353;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+10;
const int maxn=1e3+10;
const double eps=1e-6;

int b[N],c[N];

int main()
{
        int t;
        read(t);
        while(t--)
        {
            int n;
            read(n);
            For(i,1,n)read(b[i]);
            For(i,1,n)read(c[i]);
            LL ans=1;
            int cnt=0;
            For(i,1,n)
            {
                if(i==1)
                {
                    if(b[i]!=c[i]){ans=0;break;}
                    else cnt++;
                    continue;
                }
                if(c[i]<i||b[i]>n-i+1){ans=0;break;}
                if(b[i]>b[i-1]||c[i]<c[i-1]||b[i]>c[i]){ans=0;break;}                
                if(b[i]<b[i-1]&&c[i]==c[i-1])cnt++;
                else if(c[i]>c[i-1]&&b[i]==b[i-1])cnt++;
                else if(c[i]==c[i-1]&&b[i]==b[i-1])
                {
                    int len=c[i]-b[i]+1-cnt;
                    cnt++;
                    ans=ans*(LL)len%mod;
                }
                else {ans=0;break;}
            }
            cout<<ans<<"\n";
        }
        return 0;
}

  

 

posted @ 2016-07-18 11:29  LittlePointer  阅读(216)  评论(0编辑  收藏  举报