There are N people, conveniently numbered 1through N. 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 i is Ai.
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+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints- 1≦N≦105
- 0≦Ai≦N−1
The input is given from Standard Input in the following format:
N A1 A2 … ANOutput
Print the number of the possible orders in which they were standing, modulo 109+7.
Sample Input 15 2 4 4 0 2Sample Output 1
4
There are four possible orders, as follows:
- 2,1,4,5,3
- 2,5,4,1,3
- 3,1,4,5,2
- 3,5,4,1,2
7 6 4 0 2 4 0 2Sample Output 2
0
Any order would be inconsistent with the reports, thus the answer is 0.
Sample Input 38 7 5 1 1 7 3 5 3Sample Output 3
16
做题时极容易忽略情况,要认真考虑,而且最后输出要分情况。
AC:
#include<stdio.h>
int mod=1000000007;long long fun(int a,int b,int mod)
{
long long d=1,t=a;
while(b)
{
if(b&1)
d=(d*t)%mod;
b/=2;
t=(t*t)%mod;
}
return d;
}
int main()
{
int n,i,f;
long long sum;
int a[100005],m[100000];
scanf("%d",&n);
f=0;
for(i=0;i<n;i++)
{
m[i]=0;
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
m[a[i]]++;
if(n%2==1)
{
if(m[0]!=1)
f=1;
for(i=2;i<n;i+=2)
if(m[i]!=2)
f=1;
}
else
{
for(i=1;i<n;i+=2)
if(m[i]!=2)
f=1;
}
sum=fun(2,n/2,mod);
if(n%2)
{
if(f)
printf("0\n");
else
printf("%lld\n",sum);
}
else
{
if(f)
printf("0\n");
else
printf("%lld\n",sum);
}
return 0;
}
本文介绍了一种通过报告中给出的绝对差值来确定人们昨天站位顺序的方法,并提供了具体的编程实现,包括输入输出格式、约束条件及样例。文章讨论了如何验证报告的一致性并计算可能的排列数。

1065

被折叠的 条评论
为什么被折叠?



