ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

The Third Problem

2022-07-05 10:35:16  阅读:192  来源: 互联网

标签:nn Third int permutation test Problem include MEX


You are given a permutation a1,a2,…,ana1,a2,…,an of integers from 00 to n−1n−1. Your task is to find how many permutations b1,b2,…,bnb1,b2,…,bn are similar to permutation aa.

Two permutations aa and bb of size nn are considered similar if for all intervals [l,r][l,r] (1≤l≤r≤n1≤l≤r≤n), the following condition is satisfied:

MEX([al,al+1,…,ar])=MEX([bl,bl+1,…,br]),MEX⁡([al,al+1,…,ar])=MEX⁡([bl,bl+1,…,br]),

where the MEXMEX of a collection of integers c1,c2,…,ckc1,c2,…,ck is defined as the smallest non-negative integer xx which does not occur in collection cc. For example, MEX([1,2,3,4,5])=0MEX⁡([1,2,3,4,5])=0, and MEX([0,1,2,4,5])=3MEX⁡([0,1,2,4,5])=3.

Since the total number of such permutations can be very large, you will have to print its remainder modulo 109+7109+7.

In this problem, a permutation of size nn is an array consisting of nn distinct integers from 00 to n−1n−1 in arbitrary order. For example, [1,0,2,4,3][1,0,2,4,3] is a permutation, while [0,1,1][0,1,1] is not, since 11 appears twice in the array. [0,1,3][0,1,3] is also not a permutation, since n=3n=3 and there is a 33 in the array.

Input

Each test contains multiple test cases. The first line of input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. The following lines contain the descriptions of the test cases.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the size of permutation aa.

The second line of each test case contains nn distinct integers a1,a2,…,ana1,a2,…,an (0≤ai<n0≤ai<n) — the elements of permutation aa.

It is guaranteed that the sum of nn across all test cases does not exceed 105105.

Output

For each test case, print a single integer, the number of permutations similar to permutation aa, taken modulo 109+7109+7.

题目大意:给出一个0到n-1的排列,问有多少个0到n-1的全排列满足与原序列对应的任意字串的mex值相等,mex值为区间内未出现的最小非负整数。

思路:容易观察发现,0和1的位置是固定的,假设1在0右边,则有三种情况:1. 2在1右边,2.2在0左边,3.2在1与2中间。可以发现,因为0到1的mex是固定的,所以情况一与情况二的2便固定,考虑3的时候便要3在考虑0到2或者2到1(指的是原序列的地址)的情况,而情况三2便可以放在0到1之间任意空位。依次更新即可。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;

#define int long long
const int maxn=1e5+5,mod=1e9+7;
int t,n,a[maxn];

void solve() {
	cin>>n;
	for(int i=1;i<=n;i++) {
		int x;cin>>x;
		a[x]=i;
	}
	int l=a[0],r=a[0];
	int ans=1;
	for(int i=1;i<n;i++) {
		if(a[i]>r) r=a[i];
		else if(a[i]<l) l=a[i];
		else ans= (ans*(r-l-i+1))%mod;
	}
	cout<<ans<<'\n';
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--) {
		solve();
	}
}

 

标签:nn,Third,int,permutation,test,Problem,include,MEX
来源: https://www.cnblogs.com/cbmango/p/16445463.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有