poj3252——Round Numbers(数位dp)
来源:互联网 发布:java api安卓版 编辑:程序博客网 时间:2024/06/05 10:31
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
2 12
Sample Output
6
注意前导0
#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <map>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 10000005#define Mod 10001using namespace std;int dight[40];long long dp[40][40][40];long long dfs(int pos,bool limit,int zero,int one,int first){ if(pos==0) { if(first) return 1; return zero>=one; } if(!limit&&dp[pos][zero][one]!=-1&&!first) return dp[pos][zero][one]; int end; long long ret=0; if(limit) end=dight[pos]; else end=1; for(int d=0; d<=end; ++d) { if(first) { if(d==1) ret+=dfs(pos-1,limit&&d==end,0,1,0); else ret+=dfs(pos-1,limit&&d==end,0,0,1); } else { if(d==0) ret+=dfs(pos-1,limit&&d==end,zero+1,one,0); else ret+=dfs(pos-1,limit&&d==end,zero,one+1,0); } } if(!limit&&!first) dp[pos][zero][one]=ret; return ret;}long long solve(long long a){ memset(dight,0,sizeof(dight)); int cnt=1; while(a!=0) { dight[cnt++]=a%2; a/=2; } return dfs(cnt-1,1,0,0,1);}int main(){ memset(dp,-1,sizeof(dp)); long long x,y; while(~scanf("%lld%lld",&x,&y)) { printf("%lld\n",solve(y)-solve(x-1)); } return 0;}
- poj3252——Round Numbers(数位DP)
- poj3252——Round Numbers(数位dp)
- poj3252 - Round Numbers(数位dp)
- poj3252 Round Numbers (数位dp)
- poj3252 Round Numbers(数位dp)
- POJ3252:Round Numbers(数位dp)
- POJ3252 Round Numbers 数位DP
- POJ3252---Round Numbers(数位dp)
- poj3252 Round Numbers (数位dp)
- 【POJ3252】Round Numbers(数位DP)
- POJ3252:Round Numbers(数位DP)
- POJ3252 数位DP Round Numbers
- POJ3252 Round Numbers 组合数学||数位DP
- POJ3252:Round Numbers(数位DP+记忆化DFS)
- POJ3252——Round Numbers
- POJ3252——Round Numbers(组合数)
- Round #51 Beautiful numbers —— 数位dp
- POJ3253 Round Numbers + HDU2089 不要62 —— 数位DP
- 博客第一文
- 自己写的一个图形验证码页面(Asp.Net2.0通过)
- Cocos2d-x之精灵帧SpriteFrame
- 用正则表达式过滤脚本的研究(ASP.net)
- hdu 2133 What day is it
- poj3252——Round Numbers(数位dp)
- C语言中变量存储和作用域
- ASP.NET2.0中配置文件的加密与解密
- 记一个SwipeMenuListView侧滑删除错乱的Bug
- mp4v2录制音视频同步方法
- Oracle、SQL Server、MySQL分页方法
- 弹出网页窗口全攻略(html/hta)
- 不使用composer安装yii高级版本的方法
- spark源码阅读笔记Dataset(三)structField、structType、schame