POJ 3252 Round Numbers
来源:互联网 发布:淘宝网麦克风 编辑:程序博客网 时间:2024/05/20 00:15
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 ofN 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
#include <stdio.h>#include <string.h>#include <math.h>int base[50],top[50];int main(){ long long int f(int m,int n); int i,j,l1,l2,sum,weishu,t; long long int n,m,s; scanf("%lld %lld",&n,&m); l1=l2=0; while(n!=0) { base[l1++]=n%2; n=n/2; } while(m!=0) { top[l2++]=m%2; m=m/2; } s=0; for(i=l1;i<=l2-1;i++) { if(i%2==0) { j=i/2; }else if(i%2!=0) { j=i/2+1; } for(;j<=i-1;j++) { s+=f(j,i-1); } } for(i=l2-2,sum=0;i>=1;i--) { if(top[i]==1) { if(l2%2==0) { j=l2/2; }else { j=l2/2+1; } for(j=j-1-sum;j<=i; j++) { if(j>=0) { s+=f(j,i); } } }else { sum++; } } if(l2%2==0) { weishu=l2/2; }else { weishu=l2/2+1; } if(top[0]==1) { if(sum>=weishu) { s+=2; }else { if((weishu-sum)==1) { s++; } } }else { if((sum+1)>=weishu) { s++; } } base[0]-=1; i=0; while(base[i]<0) { base[i]+=2; base[i+1]-=1; i++; } t=l1; while(base[l1-1]==0) { l1-=1; } if(l1==t) { for(i=l1-2,sum=0;i>=1;i--) { if(base[i]==1) { if(l1%2==0) { j=l1/2; }else { j=l1/2+1; } for(j=j-1-sum;j<=i; j++) { if(j>=0) { s-=f(j,i); } } }else { sum++; } } if(l1%2==0) { weishu=l1/2; }else { weishu=l1/2+1; } if(base[0]==1) { if(sum>=weishu) { s-=2; }else { if((weishu-sum)==1) { s--; } } }else { if((sum+1)>=weishu) { s--; } } } printf("%lld\n",s); return 0;}long long int f(int m,int n){ int i,j; long long int res; double s; if(m<0||m>n) { return 0; } if(m==0) { return 1; } s=1.0; for(i=n,j=m;i>=n-m+1;i--,j--) { s=s*(double)i/(double)j; } res=(long long int)(s+0.01); return res;}
- 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
- 搭建自己的版本维护系统[版本管理器]
- [数据库连接字符串] SQL SERVER 2008 连接字符串
- ubuntu 修改系统配置文件
- 数字三角形III
- Qt4.8.3移植总结_作者:wuyuan
- POJ 3252 Round Numbers
- 【学习笔记】读项亮的《推荐系统实践》_第二章利用用户行为数据_上
- tomcat6上做session复制
- Robotics Toolbox for matlab如何安装
- Longest Palindromic substring
- HOOPS 3D可视化入门教程三:基本概念和数据结构
- 自己动手编写JEECMS自定义栏目统计标签
- CakePHP 2.x CookBook 中文版 第三章 入门 之 CakePHP 的结构
- 通过一个简单控制台的实现来漫谈软件工程基本概念