4-圆数Round Numbers(数位dp)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14947 | Accepted: 6023 |
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
Output
Sample Input
2 12
Sample Output
6
Source
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int dp[33][64]; //dp[i][j]表示i位数二进制0的个数比1的个数不低于的数的个数
int digit[33]; //二进制数的每一位 int dfs(int pos, int sum, int lead, int limit){
// i, j, 是否有前导0, 取值是否受限
//sum即j表示0比1多的个数
if(pos <= 0){
return sum >= 0;
}
if(!lead && !limit && dp[pos][sum] != -1){
return dp[pos][sum];
}
int rt = 0;
int end = limit ? digit[pos] : 1;
for(int i = 0; i <= end; i++){
if(lead && i == 0){ //有前置0并且当前数位位0,则此0不计数,sum不变
rt += dfs(pos - 1, sum, lead, limit && i == end);
}
else{
rt += dfs(pos - 1, sum + (i ? -1 : 1), lead && i == 0, limit && i == end);
}
}
if(!limit && !lead){
dp[pos][sum] = rt;
}
return rt;
} int solve(int x){
int len = 0;
while(x){
digit[++len] = x & 1;
x = x >> 1;
}
return dfs(len, 0, 1, 1);
} int main(){
int n, m;
memset(dp, -1, sizeof(dp)); cin >> n >> m;
cout << solve(m) - solve(n - 1) << endl; return 0;
}
4-圆数Round Numbers(数位dp)的更多相关文章
- poj 3252 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)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- POJ3252 Round Numbers —— 数位DP
题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Su ...
- POJ 3252 Round Numbers(数位dp&记忆化搜索)
题目链接:[kuangbin带你飞]专题十五 数位DP E - Round Numbers 题意 给定区间.求转化为二进制后当中0比1多或相等的数字的个数. 思路 将数字转化为二进制进行数位dp,由于 ...
- poj 3252 Round Numbers 数位dp
题目链接 找一个范围内二进制中0的个数大于等于1的个数的数的数量.基础的数位dp #include<bits/stdc++.h> using namespace std; #define ...
- Round Numbers(数位DP)
Round Numbers http://poj.org/problem?id=3252 Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- 【poj3252】 Round Numbers (数位DP+记忆化DFS)
题目大意:给你一个区间$[l,r]$,求在该区间内有多少整数在二进制下$0$的数量$≥1$的数量.数据范围$1≤l,r≤2*10^{9}$. 第一次用记忆化dfs写数位dp,感觉神清气爽~(原谅我这个 ...
- POJ - 3252 - Round Numbers(数位DP)
链接: https://vjudge.net/problem/POJ-3252 题意: The cows, as you know, have no fingers or thumbs and thu ...
- $POJ$3252 $Round\ Numbers$ 数位$dp$
正解:数位$dp$ 解题报告: 传送门$w$ 沉迷写博客,,,不想做题,,,$QAQ$口胡一时爽一直口胡一直爽$QAQ$ 先港下题目大意嗷$QwQ$大概就说,给定区间$[l,r]$,求区间内满足二进制 ...
随机推荐
- Yii 初识
接管一个Yii的系统,因为没有文档,所以非常上火. 01 查版本 Yii::getVersion(); 02 生成webapp Yii 是支持通过命令行生成webapp的.其中, yiic.bat是W ...
- 20155313 2016-2017-2 《Java程序设计》第八周学习总结
20155313 2016-2017-2 <Java程序设计>第八周学习总结 教材内容学习 十四章 NIO与NIO2 1.认识NIO NIO使用频道(Channel)来衔接数据节点,在处理 ...
- python静态方法类方法属性方法
Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的: 1)静态方法无需传入self参数,类成员方法需传入代表本类的cls参数: 2)从第1条,静态方法是无法访问 ...
- LG5024 保卫王国
题意 题目描述 Z 国有\(n\)座城市,\(n - 1\)条双向道路,每条双向道路连接两座城市,且任意两座城市 都能通过若干条道路相互到达. Z 国的国防部长小 Z 要在城市中驻扎军队.驻扎军队需要 ...
- 扩展ScriptBundle,支持混淆加密javascript
一.需求: 在web开发中,经常会处理javascript的一些问题,其中就包括js的压缩,合并,发布版本以及混淆加密等等问题.在asp.net 开发中我们使用ScriptBundle已经可以解决ja ...
- oracle之 RAC 11G ASM下控制文件多路复用
如果数据库仅有一组control file文件,需要添加一组或者多组,保证一组文件损坏或者丢失导致数据库宕机. -- 环境说明SQL> select * from v$version;BANNE ...
- [原创]JEECMS 自定义标签调用广告版位下的所有广告(利用广告管理管理首页幻灯片)
JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法: [@cms_advertising id='3'] <img src=&quo ...
- FreeFileSync 4.2 发布,文件夹比较和同步工具
FreeFileSync 是一款开源的文件夹比较和同步工具,可用于 Win 和 Lin 平台,最近发布了 4.2 版本. FreeFileSync 采用双面板设计,使用方法很简单: 在左面版和右面版上 ...
- 【pc杂谈】win7系统通过虚拟网卡共享wifi
用管理员权限进入dos命令行 启用并设定虚拟WiFi网卡:netsh wlan set hostednetwork mode=allow ssid=paulnet key=paulwinflo(注意 ...
- 复制mysql数据库的步骤
Navicat 转存sql文件 然后命令 mysql -uroot -p123456 dbname < e:/backup/20141014.sql