C - Lining Up
Time Limit: 2 sec / Memory Limit: 256 MB
Score : 300300 points
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 ...... ANANOutput
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 2Sample Output 1 Copy
Copy
4There 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
7 6 4 0 2 4 0 2
Sample Output 2 Copy
0
Any order would be inconsistent with the reports, thus the answer is 00.
Sample Input 3 Copy
8 7 5 1 1 7 3 5 3
Sample Output 3 Copy
16
题目大意:
有n个人,只记得他前面的人数减去后面的人数的绝对值,问有多少中排列方式
思路:
因为前后对应位置绝对值人数相同是,对于每对人有两种(A在前或B在前)共有n/2对,所以有pow(2,n/2)种联系方式
注意:要判断是否合法
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x3f3f3f3f //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
const LL mod=1e9+7;
LL quick_mod(LL a,LL n)
{
LL ret=1;
while(n)
{
if(n&1)
ret=ret*a%mod;
a=a*a%mod;
n>>=1;
}
return ret;
}
int num[100005];
int main()
{
//freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);
//freopen("D:\\chnegxubianji\\inORout\\out.txt", "w", stdout);
LL n;
scanf("%lld",&n);
for(int i=1; i<=n; i++)
{
LL tem;
scanf("%d",&tem);
num[tem]++;
}
if(n%2==1)
{
int flag=1;
for(int i=n-1;i>0;i-=2)
{
if(num[i]!=2)
{
flag=0;
break;
}
}
if(num[0]!=1)
flag=0;
if(!flag)
printf("0\n");
else
printf("%lld\n",quick_mod(2,n/2));
}
else
{
int flag=1;
for(int i=n-1;i>0;i-=2)
{
if(num[i]!=2)
{
flag=0;
break;
}
}
if(!flag)
printf("0\n");
else
printf("%lld\n",quick_mod(2,n/2));
}
return 0;
}
D - Xor Sum
Time Limit: 2 sec / Memory Limit: 256 MB
Score : 600600 points
Problem Statement
You are given a positive integer NN. Find the number of the pairs of integers uu and vv (0≦u,v≦N)(0≦u,v≦N) such that there exist two non-negative integers aa and bb satisfying aa xorxor b=ub=u and a+b=va+b=v. Here, xorxor denotes the bitwise exclusive OR. Since it can be extremely large, compute the answer modulo 109+7109+7.
Constraints
- 1≦N≦10181≦N≦1018
Input
The input is given from Standard Input in the following format:
NNOutput
Print the number of the possible pairs of integers uu and vv, modulo 109+7109+7.
Sample Input 1 Copy
Copy
3Sample Output 1 Copy
Copy
5The five possible pairs of uu and vv are:
u=0,v=0u=0,v=0 (Let a=0,b=0a=0,b=0, then 00 xorxor 0=00=0, 0+0=00+0=0.)
u=0,v=2u=0,v=2 (Let a=1,b=1a=1,b=1, then 11 xorxor 1=01=0, 1+1=21+1=2.)
u=1,v=1u=1,v=1 (Let a=1,b=0a=1,b=0, then 11 xorxor 0=10=1, 1+0=11+0=1.)
u=2,v=2u=2,v=2 (Let a=2,b=0a=2,b=0, then 22 xorxor 0=20=2, 2+0=22+0=2.)
u=3,v=3u=3,v=3 (Let a=3,b=0a=3,b=0, then 33 xorxor 0=30=3, 3+0=33+0=3.)
Sample Input 2 Copy
Copy
1422Sample Output 2 Copy
Copy
52277
Sample Input 3 Copy
Copy
1000000000000000000Sample Output 3 Copy
Copy
787014179
题目大意,
对于u,v 在n的范围内,存在a+b=v && a xor b=u , 问有多少对<u,v> 满足条件,结果对1000000007取模。
思路:
我们知道 a xor b <= a + b (a xor b == a + b 时当且仅当 a & b = 0),所以我们只要保证a+b<=n即可,则题意就转化为找出多少对a,b,满足a+b<=n。
递推式:dp[n]=dp[n/2]+dp[(n-1)/2]+dp[(n-2)/2]
//注意剪枝
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x3f3f3f3f //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
const LL mod=1e9+7;
map<LL,LL> dp;
LL f(LL x)
{
if(dp[x]!=0)
return dp[x];
return dp[x]=(f(x/2)%mod+f((x-1)/2)%mod+f((x-2)/2)%mod)%mod;
}
int main()
{
//freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);
//freopen("D:\\chnegxubianji\\inORout\\out.txt", "w", stdout);
LL n;
scanf("%lld",&n);
dp[0]=1;
dp[1]=2;
printf("%lld",f(n));
return 0;
}