poj3252 Round Numbers
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 7625 | Accepted: 2625 |
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
Start and
Finish.
Output
Start..
Finish
Sample Input
2 12
Sample Output
6
如果一个数二进制数中的,0比1多或相等,就是目标数,求一定范围的个数!数位dp,可以看出来,数位dp,我们用dp[i][j][k][a]表是第i位已经有j个1,总的位数是k,是否已经出现过0,这样,就可以轻易的用dp,做出来了!
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define M 40
int pri[M],dp[M][40][40][2];
int get(int i,int one,int bit){
if(one==0&&i==0)return bit-1;
return bit;
}
int dfs(int pos,int bit,int t,int flag,int one){
if(pos==0)return t<=bit/2;
if(!flag&&dp[pos][t][bit][one]!=-1)return dp[pos][t][bit][one];
int u=flag?pri[pos]:1,ans=0;
for(int i=0;i<=u;i++)
ans+=dfs(pos-1,get(i,one,bit),t+i,flag&&i==u,one||i);
return flag?ans:dp[pos][t][bit][one]=ans;
}
int solve(int x){
int cnt=0;
while(x){
pri[++cnt]=x%2;x/=2;
}
return dfs(cnt,cnt,0,1,0);
}
int main()
{
int n,m;
memset(dp,-1,sizeof(dp));
while(scanf("%d%d",&n,&m)!=EOF){
//for(m=100;m>=0;m--)
printf("%d\n",solve(m)-solve(n-1));
}
return 0;
}
poj3252 Round Numbers的更多相关文章
- [BZOJ1662][POJ3252]Round Numbers
[POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...
- POJ3252 Round Numbers —— 数位DP
题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Su ...
- poj3252 Round Numbers(数位dp)
题目传送门 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16439 Accepted: 6 ...
- poj3252 Round Numbers (数位dp)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- POJ3252 Round Numbers 【数位dp】
题目链接 POJ3252 题解 为什么每次写出数位dp都如此兴奋? 因为数位dp太苟了 因为我太弱了 设\(f[i][0|1][cnt1][cnt0]\)表示到二进制第\(i\)位,之前是否达到上界, ...
- POJ3252 Round Numbers(不重复全排列)
题目问区间有多少个数字的二进制0的个数大于等于1的个数. 用数学方法求出0到n区间的合法个数,然后用类似数位DP的统计思想. 我大概是这么求的,确定前缀的0和1,然后后面就是若干个0和若干个1的不重复 ...
- poj3252 Round Numbers[数位DP]
地址 拆成2进制位做dp记搜就行了,带一下前导0,将0和1的个数带到状态里面,每种0和1的个数讨论一下,累加即可. WA记录:line29. #include<iostream> #inc ...
- POJ3252 Round Numbers 题解 数位DP
题目大意: 求区间 \([x,y]\) 范围内有多少数的二进制表示中的'0'的个数 \(\ge\) '1'的个数. 解题思路: 使用 数位DP 解决这个问题. 我们设状态 f[pos][num0][n ...
- 题解【POJ3252】Round Numbers
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
随机推荐
- CSS3圆角(border-radius)
CSS3中的border-radius支持IE9+,chrome,firefox 2.3+,以及safari3.2+浏览器. border-radius可直接使用,无需加前缀(注意:firefox13 ...
- php开源项目学习二次开发的计划
开源项目: cms 国内 dedecms cmstop 国外 joomla, drupal 电商 国内 ecshop 国外 Magento 论坛 discuz 博客 wordpress 学习时 ...
- [CSS]position定位
CSS position 属性 通过使用 position 属性,我们可以选择 4 种不同类型的定位,这会影响元素框生成的方式. position 属性值的含义: static 元素框正常生成.块级元 ...
- sql server 数据库基础学习心得 思维导图
- dictionary ----- python
Learn of dictionary,simple example of dictionary in “Simple Python tutorial"------------------ ...
- CodeChef CBAL
题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...
- iOS 实现脉冲雷达以及动态增减元素 By Swift-感谢分享
Swift经过Xcode6 Beta4一版更新后,基本上已经可以作为生产工具了,虽然有一些地方和ObjC比起来要“落后”一些,但也无伤大雅.这里就用Xcode6 Beta4+iOS SDK 8.0开发 ...
- 运行从别处复制过来的linux可执行程序
1, 首先ldd看看缺不缺so文件,如果不缺可忽略下面的文字,直接执行 2, 先看看缺的这些库在系统上有没有,这些库可通过安装开发包,第三方软件进行安装 3, 找到可能会包含这些库的可执行程序,ldd ...
- nutch getOutLinks 外链的处理
转载自: http://blog.csdn.net/witsmakemen/article/details/8067530 通过跟踪发现,Fetcher获得网页解析链接没有问题,获得了网页中所有的链接 ...
- 转:char*, char[] ,CString, string的转换
转:char*, char[] ,CString, string的转换 (一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准 ...