Round Numbers(组合数学)
2015-08-21 08:42
399 查看
Round Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10484 Accepted: 3831
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
Line 1: Two space-separated integers, respectively Start and Finish.
Output
Line 1: A single integer that is the count of round numbers in the inclusive range Start..Finish
Sample Input
2 12
Sample Output
6
Source
USACO 2006 November Silver
看了好久的题解才看懂了,求小于这个数的二进制中0大于1的个数
如100100010 首先可以求出二进制位数小于这个数的(其值一定比这个数小如10010000),当位数相同时可以分成几部分求如[100000000-100100000)[100100000-1001000100)求的值就是组合数
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10484 Accepted: 3831
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
Line 1: Two space-separated integers, respectively Start and Finish.
Output
Line 1: A single integer that is the count of round numbers in the inclusive range Start..Finish
Sample Input
2 12
Sample Output
6
Source
USACO 2006 November Silver
看了好久的题解才看懂了,求小于这个数的二进制中0大于1的个数
如100100010 首先可以求出二进制位数小于这个数的(其值一定比这个数小如10010000),当位数相同时可以分成几部分求如[100000000-100100000)[100100000-1001000100)求的值就是组合数
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <map> #include <algorithm> #define INF 0x3f3f3f3f using namespace std; typedef long long LL; const int MAX = 1e5+10; int Bin[35]; int c[33][33]; void ComBinnations()//打表,组合数 { 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]+c[i-1][j-1]; } } } } void GetBite(int n)//分解二进制 { Bin[0]=0; while(n) { Bin[++Bin[0]]=n%2; n/=2; } } int cal(int n) { int sum=0; GetBite(n); for(int i=1;i<Bin[0];i++)//比这个数的位数小 { for(int j=(i+1)/2;j<i;j++) { sum+=c[i-1][j]; } } int ans=0; for(int i=Bin[0]-1;i>=1;i--)//相等的时候 { if(Bin[i]) { for(int j=(Bin[0]+1)/2-1-ans;j<i;j++) { sum+=c[i-1][j]; } } else { ans++; } } return sum; } int main() { int n,m; ComBinnations(); while(~scanf("%d %d",&n,&m)) { printf("%d",cal(m+1)-cal(n)); } return 0; }
相关文章推荐
- eclipse +maven+web应用 +tomcat jar包加载
- 【NOIP2001P/codevs1014】 装箱问题 解题报告
- jQuery实现大转盘抽奖活动仿QQ音乐代码分享
- 创建一个完整的窗口需要的步骤:4步
- OFTP说明
- 项目EasyUi和JS中遇到的问题总汇
- HDU 5414 CRB and String (2015年多校比赛第10场)
- [iOS备忘录]使用蓝牙耳机headset录音
- SVN与GIT
- HDU 5411(矩阵快速幂)
- poj1095--Trees Made to Order(卡特兰数)
- 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
- WordPress主题制作教程[壹] - 了解WP&结构&索引
- 网络语音视频技术浅议(附多个demo源码下载)
- 数据模型封装示例
- js金额转换大写(转)
- CSS:重量和级联规则,确定其优先级
- tcl/tk 的安装
- 2a03 ...
- static关键字