poj 3252 Round Numbers(数位DP)
2016-01-12 20:32
337 查看
Round Numbers
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
Source
USACO 2006 November Silver
思路:
数位DP。
组合计数的方法好麻烦,蒟蒻看不懂 =-=
代码:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11003 | Accepted: 4064 |
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
思路:
数位DP。
组合计数的方法好麻烦,蒟蒻看不懂 =-=
代码:
#include<cstdio> using namespace std; const int N = 50; int f ,b ,len; //p n0 n1 // flag 表示前面的位是否枚举完 z 标记前缀是否为0 int dp(int p,int n0,int n1,int flag,int z) { if(!p) return n0>=n1; if(!flag && f[p][n0][n1]) return f[p][n0][n1]; int ans=0; int end=flag? b[p]:1; for(int i=0;i<=end;i++) ans += dp(p-1,(z&&i==0)?0:n0+(i==0),(z&&i==0)?0:n1+(i==1),flag&&i==end,z&&(i==0)); if(!flag) f[p][n0][n1]=ans; return ans; } int Fc(int n) { len=0; while(n) b[++len]=n&1 , n>>=1; return dp(len,0,0,1,1); } int main() { int n,m; scanf("%d%d",&n,&m); printf("%d",Fc(m)-Fc(n-1)); return 0; }
相关文章推荐
- 【Ajax 4】Ajax、JavaScript和JQuery的联系和区别
- Oracle基础-JOIN查询(DML)
- 【Ajax 4】Ajax、JavaScript和JQuery的联系和区别
- 基于 Android NDK 的学习之旅----- C调用Java(附源码)
- Oracle基础-JOIN查询(DML)
- WebSocket
- BZOJ_P4326[NOIP]2015 运输计划(LCA+Tarjan+二分)
- linux /sys目录下的各个子目录说明
- 微信公众平台开发(111) 现金红包、裂变红包、企业付款
- 技术笔记:Indy控件发送邮件
- DSP28335-ePWM
- 响应式布局
- 淘宝 OAuth2.0 的登录验证与授权
- nyoj 节能 304(DP单线折返)好题
- Leetcode 笔记
- 运行时runtime初探(强制获取并修改私有变量,强制增加及修改私有方法等)
- 机器学习(八):AdaBoost算法与实现
- C++中const相关知识
- 介绍几个程序中关于"路径字符串"的函数
- Java泛型