Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7584 | Accepted: 2606 |
Description
The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets to be milked first. They can't even flip a coin because it's so hard to toss using hooves.
They have thus resorted to "round number" matching. The first cow picks an integer less than two billion. The second cow does the same. If the numbers are both "round numbers", the first cow wins,
otherwise the second cow wins.
A positive integer N is said to be a "round number" if the binary representation of N has as many or more zeroes than it has ones. For example, the integer 9, when written in binary form, is 1001. 1001 has two zeroes and two ones; thus, 9 is a round number. The integer 26 is 11010 in binary; since it has two zeroes and three ones, it is not a round number.
Obviously, it takes cows a while to convert numbers to binary, so the winner takes a while to determine. Bessie wants to cheat and thinks she can do that if she knows how many "round numbers" are in a given range.
Help her by writing a program that tells how many round numbers appear in the inclusive range given by the input (1 ≤ Start < Finish ≤ 2,000,000,000).
Input
Output
Sample Input
2 12
Sample Output
6
Source
由于 A:C(2k,0)+C(2k,1)+...+C(2k,2k)=2^(2k)
B:C(2k,0)=C(2k,2k), C(2k,1)=C(2k,2k-1) ,,C(2k,i)=C(2k,2k-i)
于是 C(2k,0)+C(2k,1)+...+C(2k,2k)
= C(2k,0)+C(2k,1)+...+C(2k,k)+C(2k,k+1)+C(2k,K+2)+...+C(2k,2k)
= 2*R(len)+C(2k,k)
=2^(2k)
所以R(len)=1/2*{2^(2k)-C(2k,k)};
2. 偶数情况 len=2*k,类似可以推到 R(len)=1/2*(2^(2k-1));
第一部分已经将长度小于8的部分求出。
现在要求长度=8的RoundNumber数目
长度为8,所以第一个1不可改变
现在到第二个1,如果Y是前缀如100*****的二进制,
这个前缀下,后面取0和1必然小于X,已经有2个0,一个1,
剩下的5个数字中至少需要2个0,
所以把第二个1改为0:可以有C(5,2)+C(5,3)+C(5,4)+C(5,5)
现在第三个1,也就是前最为101000**,同样求出,
至少需要0个0就可,所以有C(2,0)+C(2,1)+C(2,2)个RoundNumbers
。。。
将所有除了第一个1以外的1全部变为0,如上算出有多少个RoundNumbers,
结果相加(由于前缀不一样,所以后面不管怎么组合都是唯一的)
实在是非常的聪明,我们用k+n0+1>=i-k+n1,来保证我们放进去0的个数加上前面的0的个数总体是大于1的位数的,
#include<iostream>
#include<cstdio>
#include<cstring>
#define M 31
using namespace std;
int c[M][M];
int pow[M];
int array[M];
int solve(int n)
{
if(n<=1)
return 0;
int n0,n1,len;
int i,j,k,t;
int sum=0;
for(i=0;i<M;i++)
{
if(pow[i]&n)
array[i]=1;
else
array[i]=0;
}
/*for(i=0; i<M; ++i)
array[i]=((pow[i]&n)!=0)?1:0;
/* for(i=0;i<4;i++)
{
printf("ggg%d\n",array[i]);
}*/
for(i=M-1;i>=0;i--)
{
//printf("%d fdf %d\n",array[i],i);
if(array[i]==1)
{
len=i;
break;
}
}
//printf("%d len %d\n",len,n);实际长度为len+1;
for(i=len;i>=1;i--) //R(len)=1/2*{2^(2k)-C(2k,k)};
{ //1/2*(2^(2k-1));
if(i%2==1)
{
sum=sum+(pow[i-1]-c[i-1][(i-1)/2])/2;
}
else
{
sum=sum+(pow[i-1])/2;
}
}
n0=0;
n1=0;
for(i=len;i>=0;i--)
{
if(array[i])
n1++;
else
n0++;
}
if(n0>=n1)
{
sum++;
}
n0=0;
n1=1;
for(i=len-1;i>=0;i--)
{
if(array[i])
{
for(k=i;k>=0&&k+n0+1>=i-k+n1;k--)
{
sum=sum+c[i][k];
}
n1++;
}
else
n0++;
}
return sum;
}
int main()
{
int i,j,k;
int start,finsh;
for(i=0;i<M;i++)
{
c[i][0]=1;
c[i][i]=1;
pow[i]=(1<<i);
}
for(i=2;i<M;i++)
{
for(j=1;j<i;j++)
{
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
//for(i=0;i<M;i++)
//printf("%d\n",pow[i]);
scanf("%d%d",&start,&finsh);
printf("%d\n",solve(finsh)-solve(start-1));
return 0;
}