Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11850 | Accepted: 4461 |
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int c[35][35];
int a[35];
int b[35];
void change(int* p,int n)// 逆序
{
int i=0;
int j=n-1;
for(; i<=j; j--,i++)
{
int t;
t=*(p+i);
*(p+i)=*(p+j);
*(p+j)=t;
}
}
int searc(int n,int x,int y,int s[])
{
if(n<=0)return 0; //如果只有一位数那么只能是1,因为题目里说>=1
int sum=0;
for(int i=1; i<n; i++) //找出位数小于当前数字的情况,
{
for(int j=(i-1)/2+1; j<=i; j++) //j的初始值为比当前长度减1,的一半大的数,不管几位初始位都为1,所以在剩下的里边选0多的
{
sum+=c[i-1][j];
}
}
if(x<=y)sum++; //如果这个数本身就是一个round number则加入总数
int zero=0;
for(int i=1; i<n; i++)
{
if(s[i]) //如果当前数组的数是1,那么可以把它换位0,
{
for(int j=(n-1)/2-zero; j<=(n-1-i); j++)
{
sum+=c[n-1-i][j];
// printf("****%d\n",sum);
}
}
else zero++;
}
return sum;
}
int main()
{
c[0][0]=1;
for(int i=1; i<=33; i++)
for(int j=0; j<=i; j++)
{
if(!j||i==j)c[i][j]=1;
else c[i][j]=c[i-1][j-1]+c[i-1][j];
}
int n,m;
while(~scanf("%d%d",&n,&m))
{
int x1=0,y1=0;
int s1=0;
int sum=0;
while(n) //将n转换为二进制
{
int k=n%2;
a[s1++]=k;
n/=2;
if(k)x1++;
else y1++;
}
change(a,s1);
int x2=0,y2=0;
int s2=0;
while(m)
{
int k=m%2;
b[s2++]=k;
m/=2;
if(k)x2++;
else y2++;
}
change(b,s2);
int ki;
if(x1<=y1) ki=1; //如果n本身就是round number,那么会减去它本身,所以需要加上1;
else ki=0;
printf("%d\n",searc(s2,x2,y2,b)-searc(s1,x1,y1,a)+ki);
}
}