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 ...
随机推荐
- 创建对象的两种方法: new 和 面向对象(对象字面量)及对象属性访问方法
创建对象的两种方法: new 和 面向对象(对象字面量)用 new 时:var o = new Object();o.name = "lin3615";alert(o.name); ...
- CSS鼠标点击式变化图片透明度
今天分享前端代码主题:jequery控制css图片透明度 很多时候在网站图片处理上需要实现一些辅助效果,比如鼠标在图片上滑动时或点击时改变图片颜色(变灰或者其他),其实一个简单的办法就是改变图片css ...
- PYTHON开发--面向对象基础入门
面向对象 一:面向对象初级 1.思考:首先在python中,以前我们以前用到的几乎都是函数式编程,但是有时候函数式编程其中代码重复利用率太高,我们往往会把这些重复代码写进一个函数日后去调用,所以呢,今 ...
- python【第十五篇】JavaScript
大纲 1 简介 2 存在形式 3 放置位置 4 变量 5 注释 6 数据类型 7 时间处理 8 语句和异常 9 函数及其作用域 1.JS简介 JavaScript是世界上最流行的脚本语言,因为你在电脑 ...
- golang中设置Host Header的小Tips
前言 笔者最近时间一直在学习和写Ruby和Go,尤其是Go,作为云计算时代的标准语言,写起来还是相当有感觉的,难过其会越来越火. 不过写的过程中,也遇到了一些小问题,本文就是分享关于go语言设置 HT ...
- CSS3随笔系列之transform(一)—— transform-origin
transform-origin属性平时似乎用得很少,它决定了变换时依赖的原点.基本的属性特性可以参考CSS手册. 如果在H5动画项目中,用到旋转的话,它还是不能小觑的. 假如我们做一个秋千效果 其实 ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- PYTHON调用JENKINS的API来进行CI
我查到的相关API有两套,我主要用的是python-jenkins. https://pypi.python.org/pypi/python-jenkins/ 按语法调用即可... import je ...
- SQL表建立,临时表,表变量示例
CODE: USE Sales; GO /* CREATE TABLE Orders ( OrderID int IDENTITY(1,1) PRIMARY KEY, OrderGUI uniquei ...
- win7 下与mac虚拟机的共享文件的建立
1. 确保针对Mac虚拟机的VMware Tools的安装 加载进入系统后,在mac里可看到安装和卸载vmware tools的两个图标(点开vmware tools磁盘),点安装的就可以了. 2. ...