Problem Statement
There are NN people, conveniently numbered 11 through NN. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person ii is AiAi.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 109+7109+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 00.
Constraints
- 1≦N≦1051≦N≦105
- 0≦Ai≦N−10≦Ai≦N−1
Input
The input is given from Standard Input in the following format:
NN A1A1 A2A2 ...... ANAN
Output
Print the number of the possible orders in which they were standing, modulo 109+7109+7.
Sample Input 1 Copy
Copy
5 2 4 4 0 2
Sample Output 1 Copy
Copy
4
There are four possible orders, as follows:
- 2,1,4,5,32,1,4,5,3
- 2,5,4,1,32,5,4,1,3
- 3,1,4,5,23,1,4,5,2
- 3,5,4,1,23,5,4,1,2
Sample Input 2 Copy
Copy
7 6 4 0 2 4 0 2
Sample Output 2 Copy
Copy
0
Any order would be inconsistent with the reports, thus the answer is 00.
Sample Input 3 Copy
Copy
8 7 5 1 1 7 3 5 3
Sample Output 3 Copy
Copy
16
题意:
有n个人编号1.....n;给出每个人的左边人数和右边人数的人数差的绝对值记为a[i]。问你有多少种排序方式,如果没有输出0;
思路:(由于对称性)
如果n为奇数:如果能排序那么一定有一个a[i]==0;剩余的一定两个2,4,6,.......n-1;否则输出0;
如果n为偶数:那么如果能排序那么一定有两个1,3,5,.......n-1;否则输出0;
能排序的话,一定有2的(n/2)次方种可能。
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll mod=1e9+7;
ll a[100009];
ll qpow(ll a,ll b)
{
ll base=a,ans=1;
while(b)
{
if(b&1)
{
ans=(ans*base)%mod;
}
base=(base*base)%mod;
b>>=1;
}
return ans%mod;
}
int main()
{
memset(a,0,sizeof(a));
int n;
cin>>n;
int x;
for(int i=1; i<=n; i++)
{
cin>>x;
a[x]++;
}
if(n%2==0)
{
for(int i=1; i<=n; i+=2)
{
if(a[i]!=2)
{
cout<<"0"<<endl;
return 0;
}
}
cout<<qpow(2,n/2)<<endl;
return 0;
}
else if(n%2==1)
{
if(a[0]!=1)
{
cout<<"0"<<endl;
return 0;
}
else
{
for(int i=2; i<=n; i+=2)
{
if(a[i]!=2)
{
cout<<"0"<<endl;
return 0;
}
}
cout<<qpow(2,n/2)<<endl;
return 0;
}
}
}