poj 3252(详解)
来源:互联网 发布:剑网3菊花插件数据 编辑:程序博客网 时间:2024/06/08 06:44
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
Source
USACO 2006 November Silver
看详细的思路,代码自己慢慢敲吧。http://poj.org/showmessage?message_id=158333
#include<cmath>#include<iostream>#include<stdio.h>using namespace std;int C[32][32];void init(){ for(int i=0;i<=32;i++) for(int j=0;j<=i;j++){ if(j==0 || i==j){ C[i][j]=1; } else{ C[i][j]=C[i-1][j]+C[i-1][j-1]; } }}int cal(int x){ if(x==0) return 0; int sum=0; int tmp=x; int k=0; int tt=0; while(tmp){ if(!(tmp&1)) tt++; tmp=tmp>>1; k++; } if(2*tt>=k) //判断本身是否为RN sum++; int t_k=k-1; while(t_k>=1){ //计算长度为k-1到1的情况 if(t_k&1) sum+=((1<<(t_k-1))-C[t_k-1][(t_k-1)/2])/2; else sum+=(1<<(t_k-1))/2; t_k--; } tmp=x; for(int i=1;i<k;i++){ //计算长度等于k的情况 if(tmp&1){ int t; int num_z=1; tmp=tmp>>1; t=tmp; while(t){ if((t&1)==0) num_z++; t=t>>1; } for(int j=i-1;j>=0;j--){ if(num_z+j>=int(k/2.0+0.5)){ sum+=C[i-1][j]; } else break; } } else tmp=tmp>>1; } return sum;}int main(){ init(); int a,b; while(scanf("%d%d",&a,&b)!=EOF){ printf("%d\n",cal(b)-cal(a-1)); } return 0;}
- poj 3252(详解)
- poj 1611(详解)
- poj 1942(详解)
- poj 1222 高斯消元详解
- poj 2230详解
- poj 3581 后缀数组 详解
- POJ 1195 树状数组详解
- POJ 3461 Oulipo(KMP详解)
- POJ-2240 Arbitrage(floyd详解)
- poj 1312(详解)棋盘问题
- poj 2096 概率dp详解
- POJ 2709 Painter 贪心详解
- POJ 1741:点分治详解
- poj 2104 主席树 详解
- 单调栈详解 + poj 2796 poj 2559 CF 547B
- poj 3252
- poj-3252
- POJ 3252
- HDU 1060 Leftmost Digit【对数运算】
- UNIX环境高级编程复习——文件和目录(1)
- Oracle 11g 学习四:多表查询、左右连接、SQL1999语法、统计函数及分组查询
- leetcode Best Time to Buy and Sell Stock(I~III)(*)
- iOS页面跳转及数据传递
- poj 3252(详解)
- 没有窗口怎么解决SetTimer问题
- 拓扑排序
- HDU 4920 Matrix multiplication 解题报告(暴力)
- (4.4.1)使用storyboard实现页面跳转,简单的数据传递
- 在linux下设置共享库路径
- Node.js使用Sequelize ORM框架
- 微处理器之Trace Cache浅析
- 计算工资