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
题意:给出一个数据范围,求里面的圆数个数有多少,圆数的定义是化为无前导零二进制里面0的个数比1的个数多的话就是圆数
思路:首先我们范围比较大,我们一个一个判断不太现实,然后它其实就是求给定的二进制位里面0的个数多余1的个数就行,然后我们这个可以进行求一个组合数来解决
分为两部分进行求解
如 101010 我会先求出5位的时候,
那说明第一位是0 第二位是1
即 1????
再从四个位置里面算出0的个数比1的个数多的组合数
再判断4位 3位 2位 1位
第二部分即
100000-101010范围内
我们可以从高位开始判断 计算0 1 的数量
然后0直接忽略
当判断为1的时候,这时候我可以选择这个位置为0,后面的位置那我就可以随意怎么选求组合数
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<cmath> #include<stack> #include<map> using namespace std; int C[50][50]; int a[50]; int suan(int n) { int len=0; int k=n; int sum=0; while(k) { a[++len]=k%2; k/=2; } for(int i=1;i<=len-2;i++) { for(int j=i/2+1;j<=i;j++) { sum+=C[i][j]; } } int zero=0; for (int i=len-1;i>=1;i--) { if (a[i]) { for (int j=(len+1)/2-(zero+1);j<=i-1;j++) { sum+=C[i - 1][j]; } } else zero++; } return sum; } int main() { int n,m; for(int i=0;i<=32;i++) { for(int j=0;j<=i;j++) { if(i==j||!j) C[i][j]=1; else C[i][j]=C[i-1][j-1]+C[i-1][j]; } } scanf("%d%d",&n,&m); printf("%d\n",suan(m+1)-suan(n)); }