POJ3252 Round Numbers 题解 数位DP
题目大意:
求区间 \([x,y]\) 范围内有多少数的二进制表示中的‘0’的个数 \(\ge\) ‘1’的个数。
解题思路:
使用 数位DP 解决这个问题。
我们设状态 f[pos][num0][num1][all0] 表示在:
- 当前所在数位为
pos; - 当前选择的‘0’的个数为
num0; - 当前选择的‘1’的个数为
num1; - 到当前位位置是不是前面的数都是前导零(如果都是前导0则
all0==true,否则all==false)。
下的方案数。
我们开函数 dfs(int pos, int num0, int num1, bool all0, bool limit) 来解决这个问题。
其中,
pos、num0、num1、all0所表示的含义和上述表述一致,limit表示当前是否处于限制条件。
实现代码如下:
#include <cstdio>
#include <cstring>
int f[33][33][33][2], a[33];
void init() {
memset(f, -1, sizeof(f));
}
int dfs(int pos, int num0, int num1, bool all0, bool limit) {
if (pos < 0) // 遍历完所有数位
return num0 >= num1 ? 1 : 0;
if (num0 + pos + 1 < num1) // 0的个数用于达不到1的个数
return 0;
if (!limit && f[pos][num0][num1][all0] != -1) return f[pos][num0][num1][all0];
int up = limit ? a[pos] : 1;
int tmp = 0;
for (int i = 0; i <= up; i ++) {
tmp += dfs(pos-1, num0 + (!all0 && i==0), num1 + (i==1), all0 && i==0, limit && i==up);
}
if (!limit) f[pos][num0][num1][all0] = tmp;
return tmp;
}
int get_num(int x) {
int pos = 0;
while (x) {
a[pos++] = x % 2;
x /= 2;
}
return dfs(pos-1, 0, 0, true, true);
}
int main() {
init();
int x, y;
while (~scanf("%d%d", &x, &y)) {
printf("%d\n", get_num(y) - get_num(x-1));
}
return 0;
}
POJ3252 Round Numbers 题解 数位DP的更多相关文章
- poj3252 Round Numbers(数位dp)
题目传送门 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16439 Accepted: 6 ...
- POJ3252 Round Numbers 【数位dp】
题目链接 POJ3252 题解 为什么每次写出数位dp都如此兴奋? 因为数位dp太苟了 因为我太弱了 设\(f[i][0|1][cnt1][cnt0]\)表示到二进制第\(i\)位,之前是否达到上界, ...
- POJ 3252 Round Numbers(数位dp)
题意:给定区间[l,r],l < r ,求区间中满足条件的正整数的个数:二进制表示下0的个数不少于1的个数. 分析:f(x)表示<=x时满足条件的数的个数,所求问题即为f(r)-f(l-1 ...
- [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)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)
D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...
- Codeforces Beta Round #51 D. Beautiful numbers(数位dp)
题目链接:https://codeforces.com/contest/55/problem/D 题目大意:给你一段区间[l,r],要求这段区间中可以整除自己每一位(除0意外)上的数字的整数个数,例如 ...
- poj3252 Round Numbers
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7625 Accepted: 2625 Des ...
随机推荐
- vue element 中自定义传值
一直以来都不知道如何传自定义的值,一直只会默认的,今天终于找到方法了. 比如这个上传图片的控件,想带当前的index过去,就这样写.其它的类似 :http-request="(file,fi ...
- 一个div居于另一个div底部
一个div如何与另一个div底部对齐,方法有很多,比如使用绝对定位 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...
- HDU 2191多重背包问题、
#include<cstdio> #include<cmath> #include<iostream> #include<cstring> +; int ...
- Javassist指引(二)--ClassPool
原文链接 上一章: Javassist指引(一) 2.ClassPool ClassPool是一个CtClass的容器.因为编译器随时可能访问一个CtClass类,所以一旦一个CtClass创建,它将 ...
- Spring Data Jpa一对多单向映射
/** @author StormMaybin @date 2017-01-17 */ 生命不息,奋斗不止! 一对多映射关系 在JPA中,用@OneToMany来标识一对多的关系.实现一对多的单向关联 ...
- [转]C#中 ??、 ?、 ?: 、?.、?[ ] 问号
1. 可空类型修饰符(?) 引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; 是正确的,int i=null; 编译器就会报错.为了使值类型也 ...
- golang http get请求方式
client := &http.Client{} //生成要访问的url,token是api鉴权,每个api访问方式不同,根据api调用文档拼接URLurl := fmt.Sprintf(&q ...
- P1023 活动安排
题目描述 某个人可以在n个活动中选择一些出来参加.每个活动都有起止时间.而且每个时间段只能参加一个活动.问,这个人最多能加参加几个活动. 可以在活动结束时,立即开始新的活动. 输入格式 第一行是一个整 ...
- flex布局属性说明
flex布局又称为盒子布局或弹性布局,用来为盒状模型提供最大的灵活性,任何一个容器都可以指定为 Flex 布局. 给父容器添加display: flex/inline-flex;属性,即可使容器内容采 ...
- 前端导出&配置问题
<button class="search" onclick="method5('dataTable');">导出</button> 在 ...