POJ 3252 Round Numbers(数位DP)
2015-07-12 21:03
190 查看
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
Sample Output
题意:问你[a,b]区间中满足其二进制表达式中0的个数大于1的个数的数字个数
分析:数位DP直接搞,需要注意的就是由于前导0的存在,要记录前导0是否确定
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
题意:问你[a,b]区间中满足其二进制表达式中0的个数大于1的个数的数字个数
分析:数位DP直接搞,需要注意的就是由于前导0的存在,要记录前导0是否确定
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int INF=0x3f3f3f3f; typedef long long LL; LL a,b; LL dp[60][60][60][2]; int num[35]; LL dfs(int pos,int sum1,int sum2,int first,int flag)//sum1:0 sum2:1 { if(!pos&&!first) return 0; if(!pos&&first) return sum1>=sum2; if(!flag&&dp[pos][sum1][sum2][first]!=-1) return dp[pos][sum1][sum2][first]; int ed=flag?num[pos]:1; LL res=0; for(int i=0;i<=ed;i++) { if(!first&&i) res+=dfs(pos-1,sum1,sum2+1,1,flag&&i==ed); else if(!first&&!i) res+=dfs(pos-1,sum1,sum2,0,flag&&i==ed); else if(!i) res+=dfs(pos-1,sum1+1,sum2,first,flag&&i==ed); else res+=dfs(pos-1,sum1,sum2+1,first,flag&&i==ed); } if(!flag) dp[pos][sum1][sum2][first]=res; return res; } LL solve(LL x) { int pos=0; while(x) { num[++pos]=x%2; x/=2; } return dfs(pos,0,0,0,1)+1;//dfs(pos,sum1,sum2,flag) } int main() { CLEAR(dp,-1); while(~scanf("%lld%lld",&a,&b)) { LL ans=solve(b)-solve(a-1); printf("%lld\n",ans); } return 0; }
相关文章推荐
- jersey 学习资料(初学时记的笔记)
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds解
- VMware 10下简易安装Ubuntu 14
- 10 Examples of HotSpot JVM Options in Java Read more: http://javarevisited.blogspot.com/2011/11/hot
- shell学习四十四天----寻找文件
- 一些linux命令
- Nmap系列A------基础知识
- IDEA设置Eclipse编译(用于忽略有编译错误的Class)
- poj 1985 Cow Marathon 【树的直径】
- 互联网+
- 寻找子串位置
- Linux 文件系统
- 习题6-4 骑士的移动 UVa 439
- android 自定义edittext
- JavaScript学习(一)
- linux 管道
- [转载] Hibernate与 MyBatis的比较
- Sql sever 分组排序
- 结构体字节对齐
- LeetCode Search in Rotated Sorted Array II