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 ...
随机推荐
- js获取星期几
function getweek(time) { /*显示星期*/ /*time为date格式*/ var str; var d = time.getDay(); switch (d) { case ...
- fedora22 无法联网的情况下rpm安装gcc5.1
前天发生件很不幸的事.我在给ubuntu14.04安装NVIDIA显卡驱动的时候,想清空下一个目录,什么目录我也忘了,当时我正好切到root身份(平常我很少切root的),命令格式如下 rm -fr ...
- hierarchyviewer偶然不能使用的解决方法
在DDMS的device中可以看到设备,并显示可以debug的状态,可以看到不显示进程的信息,但是hierarchyviewer也却不显示各个Window. 在控制台的打印信息如下: - hierar ...
- 字符串copy
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...
- ruby特性
1. ruby类结构 每个类都是Class类的对象 所有类都继承自BasicObject类(Module类不能实例化) 2. 单例方法 单例方法可以不定义在类中,只为某个对象定义方法,所以称为单例方法 ...
- ios8新特性widget开发-b
os8发布已经有一段时间了,伴随着ios8同时也出现了许多新的特性,ios系统将会越来越开放,这是好事.其中一个新特性就是在下拉通知栏里加入了个性的widget,开发者可以自己定义widget的样式内 ...
- 树莓派连接GPS模块
一月份的时候觉得好玩买了树莓派,但是太懒没怎么研究,但最近当初买树莓派时的那个梦想又萦绕心头,决定抽空完成一下当年的计划~ GPS模块是其中很重要的一环,于是在某宝上搜索,找了一家相对便宜也很轻巧的G ...
- c# 把 颜色值Hex 转换为 Color
原文: http://abujj.me/archives/695 Assuming you mean the HTML type RGB codes (called Hex codes, such a ...
- bzoj 2115: [Wc2011] Xor xor高斯消元
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 797 Solved: 375[Submit][Status] ...
- 最完整的合并相交集合的Java代码(查并集)
这个是自己写的算法,如果有大牛,麻烦帮我并行化.初学者则可以学到不少东西. 产生测试用例 import java.io.*; import java.util.Random; public class ...