poj 3252 Round Numbers
来源:互联网 发布:debian ubuntu 哪个好 编辑:程序博客网 时间:2024/05/17 08:43
题目链接:点击打开链接
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
题目大意:给出两个数n,m,求这两个数之间Round Numbers的个数,Round Numbers是指将一个十进制的数转化为二进制的数,其中1的个数比0多的数.ps:在这将目标转到怎样才能判别这个数的二进制的1比0多上来.从0到n有sum个数符合Round Numbers,sum=1c1+2c2+(2c3+3c3)+(3c4+4c4)+(3c5+4c5+5c5)++sum=1c1+2c2+(2c3+3c3)+(3c4+4c4)+(3c5+4c5+5c5)++,解释一下这个式子,对于二进制的位数做组合,例如,当二进制只有一位时只能放1才满足要求,其他的以此类推,(注意第一位肯定是1的),只要位数比给出的数的二进制的位数少,可以确定这个数比给出的数要小.所以可分两种情况:1>位数小于给出的数的位数的时候的数由多少个,位数等于给出的数的位数的时候右多少个.
在计算组合数的时候可以联系杨辉三角与组合数的关系进行计算,具体的看代码的注释:
<span style="font-size:18px;">///sum=1c1+2c2+(2c3+3c3)+(3c4+4c4)+(3c5+4c5+5c5)++#include <iostream>using namespace std;int c[33][33];int a[40];int zh(int x){ int len=0,sum=0; ///二进制转化,从一开始 while(x!=0) { a[++len]=x%2; x=x/2; } for(int i=1; i<len-1; i++)///长度小于len的值计算和,因为第一位一定是1, ///不用管.长度又要小于len,所以i<len-1结束 { for(int j=i/2+1; j<=i; j++)///保证长度大于等于i/2+1 { sum+=c[i][j]; } } int z=0;///计算0的个数 ///等长度且小于x值的数 for(int i=len-1; i>=1; i--) { if(a[i])///二进制为1时统计1以后比x值小的数 { for(int j=(len+1)/2-(z+1); j<i; j++) { sum+=c[i-1][j]; } } else z++;///否则0的个数加1 } return sum;}int main(){ ///打表计算组合数的值 for(int i=0; i<=32; 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; cin>>n>>m; ///输出[n,m]之间的rn cout<<zh(m+1)-zh(n)<<endl; return 0;}</span>
- poj 3252--Round Numbers
- poj 3252 Round Numbers
- poj 3252 Round Numbers
- POJ 3252 Round Numbers
- POJ 3252 Round Numbers
- poj 3252 Round Numbers
- poj-3252-Round Numbers
- POJ 3252 Round Numbers
- POJ 3252 Round Numbers
- POJ 3252 Round Numbers
- POJ 3252 Round Numbers
- POJ 3252 Round Numbers
- POJ 3252 Round Numbers
- POJ 3252 Round Numbers
- POJ 3252 Round Numbers
- poj 3252 Round Numbers
- poj 3252 Round Numbers
- POJ 3252 Round Numbers
- jfinal后台控制跳转方式
- Car的旅行路线(最短路+寻找矩形第四个点)
- Unity3D研究院之提取游戏资源的三个工具支持Unity5(八十四)
- iterator做形参传值,而不是传引用。
- 将字符串写入磁盘
- poj 3252 Round Numbers
- iOS集成腾讯直播
- XHRHttpRequest原生js代码
- BouncyCastle使用教程
- 《编写可维护的JavaScript》读书笔记——1.基本的格式化
- springMVC session的应用@SessionAttributes和@ModelAttribute
- zz 请慎用java的File#renameTo(File)方法
- ubantu16.04菜单栏变换位置
- 良好的代码习惯(四)