C - Lining Up(思维+快速幂)

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;
        }
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值