日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

I - Interesting Permutation Gym - 102394I(排列组合)

發布時間:2023/12/4 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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(排列组合)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。