E - Lining Up AtCoder - 2271

Problem Statement

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≦AiN1
Input

The input is given from Standard Input in the following format:

N
A1 A2  AN
Output

Print the number of the possible orders in which they were standing, modulo 109+7.

Sample Input 1

5
2 4 4 0 2
Sample 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
Sample Input 2

7
6 4 0 2 4 0 2
Sample Output 2

0

Any order would be inconsistent with the reports, thus the answer is 0.

Sample Input 3

8
7 5 1 1 7 3 5 3
Sample 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;
 }
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值