poj3252 Round Numbers
来源:互联网 发布:访客网络开启还是关闭 编辑:程序博客网 时间:2024/05/15 17:28
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思路:先枚举2的次方,如果这个数小于目标tar,那么统计它之内的round number个数2的次方数大于tar时,将tar转成2进制,向后枚举每一位,对于每个是1的位,先取0,向后统计,再把他回复成1注意的细节:get(n-1)和get(m+1),因为get函数并没有算边界代码没有过,runtime error,不过对拍是对的,还望高手赐教代码:#include<iostream>#include<stdio.h>using namespace std;int c[333][333];int a,b,d,n,m;int comb(){ for (int i=0;i<=32;i++) for (int j=0;j<=32;j++) c[i][j]=0; c[0][0]=1; for (int i=1;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]; } } return 0;}int j2[503];int get(int aa){ if (aa==0) return 0; if (aa==1) return 0; if (aa==2) return 1; if (aa==3) return 1; int f=0; int ans=0; int w=1; ans=0; for (int i=4;i<=aa;i*=2) { w++; for (int k=w-1;k>=(w+1)/2;k--) { ans+=c[w-1][k]; } if (i==aa) f=1; } if (f==1) return ans+1; a=1; for (int i=0;i<=40;i++) j2[i]=0; int bb=aa; int biao=0; int cc=2; while(bb!=0) { j2[++biao]=bb%2; bb/=2; } int num0,num1; num0=0; num1=0; for (int i=1;i<=biao;i++) if (j2[i]==1) num1++; else num0++; if (num0>=num1) ans++; num1=0; num0=0; int sta; for (int i=40;i>=1;i--) if (j2[i]!=0) { sta=i; break; } num1=1; w++; for (int i=sta-1;i>=1;i--) { if (j2[i]==0) { num0++; continue; } num0++; for (int k=(w+1)/2-num0;k<=w-num1-num0;k++) { ans+=c[w-num1-num0][k]; } num0--; num1++; } return ans;}int main(){ comb(); scanf("%d%d",&n,&m); int aa,bb; aa=get(n-1); bb=get(m); cout<<bb-aa<<endl;}
- POJ3252 Round Numbers
- POJ3252 Round Numbers
- poj3252 Round Numbers
- poj3252 Round Numbers
- poj3252--Round Numbers
- poj3252 Round Numbers
- POJ3252 Round Numbers
- poj3252 Round Numbers
- POJ3252 Round Numbers
- POJ3252-Round Numbers
- poj3252 Round Numbers
- poj3252[Round Numbers]
- 【POJ3252】 Round Numbers
- POJ3252-Round Numbers
- POJ3252 Round Numbers
- POJ3252 Round Numbers 数位DP
- poj3252.Round Numbers,组合数
- POJ3252---Round Numbers(数位dp)
- 关于 myqsl的字符存储问题
- Linux环境中往MySQl数据库导入数据文件乱码问题
- 测试
- 最详细的Codis集群扩容方法
- ArduPilot稳定版分支编译
- poj3252 Round Numbers
- SAS Options
- C#/.NET RestSharp网络组件实现上传文件到远程服务器
- JSP 中的 Filter 过滤器
- 1002-C
- 分布式系统架构
- LoadRunner手动编写http请求示例(同样试用于TCP及其它的性能测试脚本)
- java学习笔记(2)
- java 集合 collection内容总结