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 ...
随机推荐
- JavaScript学习总结【9】、DOM Ready
1.DOM DOM(Document Object Model)即文档对象模型,是从文档中抽象出来的,DOM 操作的对象就是文档,DOM 将 HTML 文档呈现为带有元素.属性和文本的树结构,即节点树 ...
- 关于linux开机自启
/etc/rc.d/rc.local 是在系统最后启动,必须和其他rc.0 - rcN文件具有755权限
- Day17 表单验证、滚动菜单、WEB框架
一.表单验证的两种实现方式 1.DOM绑定 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- 【转】mysqldump
原文地址:http://blog.chinaunix.net/uid-16844903-id-3411118.html 导出 导出全库备份到本地的目录 mysqldump -u$USER -p$PAS ...
- Apache .htaccess Rewrite解决问号匹配的写法
如news.asp?id=123 需要把它定向到 news/123.html 这个用 RewriteRule 怎么写啊? RewriteRule ^news\.asp\?id=(\d+)$ news/ ...
- Word 2016 test
Word 2016 test
- 单元测试unit test,集成测试integration test和功能测试functional test的区别
以下内容转自 https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-function ...
- 64位Win7下安装并配置Python3的深度学习库:Theano
注:本文全原创,作者:Noah Zhang (http://www.cnblogs.com/noahzn/) 这两天在安装Python的深度学习库:Theano.尝试了好多遍,CMake.MinGW ...
- Coursera《machine learning》--(14)数据降维
本笔记为Coursera在线课程<Machine Learning>中的数据降维章节的笔记. 十四.降维 (Dimensionality Reduction) 14.1 动机一:数据压缩 ...
- vc2010配置opencv2.4.4库(图文 转)
VC 2010下安装OpenCV2.4.4 说明: 安装平台:32位XP,VS2010: OpenCV 2.4.4不支持VC 6.0: 网上有很多用CMake编译OpenCV的安装教程,这 ...