I - Interesting Permutation Gym - 102394I(排列组合)
題意:
純數題
1≤i≤n, fi=max{a1,a2,…,ai};
1≤i≤n, gi=min{a1,a2,…,ai};
1≤i≤n, hi=fi?gi.
數列a是一個排列,問多少種排列方式滿足h數列。
題目:
DreamGrid has an interesting permutation of 1,2,…,n denoted by a1,a2,…,an. He generates three sequences f, g and h, all of length n, according to the permutation a in the way described below:
For each 1≤i≤n, fi=max{a1,a2,…,ai};
For each 1≤i≤n, gi=min{a1,a2,…,ai};
For each 1≤i≤n, hi=fi?gi.
BaoBao has just found the sequence h DreamGrid generates and decides to restore the original permutation. Given the sequence h, please help BaoBao calculate the number of different permutations that can generate the sequence h. As the answer may be quite large, print the answer modulo 109+7.
Input
The input contains multiple cases. The first line of the input contains a single integer T (1≤T≤20000), the number of cases.
For each case, the first line of the input contains a single integer n (1≤n≤105), the length of the permutation as well as the sequences. The second line contains n integers h1,h2,…,hn (1≤i≤n,0≤hi≤109).
It’s guaranteed that the sum of n over all cases does not exceed 2?106.
Output
For each case, print a single line containing a single integer, the number of different permutations that can generate the given sequence h. Don’t forget to print the answer modulo 109+7.
Example
Input
3
3
0 2 2
3
0 1 2
3
0 2 3
Output
2
4
0
Note
For the first sample case, permutations {1,3,2} and {3,1,2} can both generate the given sequence.
For the second sample case, permutations {1,2,3}, {2,1,3}, {2,3,1} and {3,2,1} can generate the given sequence.
分析:
AC代碼:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; typedef long long ll; const int M=1e5+10; const int mod=1e9+7; int t,n; ll ans,num; int a[M]; int main() {scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=1; i<=n; i++)scanf("%d",&a[i]);ans=1,num=0;if(a[1]!=0){printf("0\n");continue;}for(int i=2; i<=n; i++){if(a[i]<a[i-1]||a[i]>=n||a[i]<0){ans=0;break;}if(a[i]>a[i-1])//h[i] > h[i-1] ,那么比如0 3,說明第一個數和第二個數相差3,那么就找來一組數據x,y,放在這兩個位置上,有兩種順序;{num+=a[i]-a[i-1]-1;ans*=2;ans%=mod;}else if(a[i]==a[i-1])//h[i] == h[i-1], 這種情況說明,新加上去的數,不影響前面的絕對差值,也就是加上的數處于差值范圍內的任意一個數,這個差值是前面任意兩個數之間的差值之和;統計出每種情況的數量,乘積就是總的方案書;{ans*=num;ans%=mod;num--;}}printf("%lld\n",ans);}return 0; }總結
以上是生活随笔為你收集整理的I - Interesting Permutation Gym - 102394I(排列组合)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 出汗能减肥吗
- 下一篇: PTA天梯赛L1-006 连续因子 (2