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 ...
随机推荐
- phpstorm2017破解版 2017.3.4 官网中文版
phpstorm2017破解版是一款强大的PHP编程工具,新云软件园提供phpstorm激活下载,最新版PhpStorm 2017正式版改进了PHP 7支持,改进代码完成功能,PhpStorm 是最好 ...
- JavaWeb项目结构和classpath:
以tomcat为例 项目结构 开发时的项目结构 蓝框 : 存放java文件 绿框 : 存放配置文件 红框 : 存放前台代码 这个项目结构大家都很熟悉,那么当项目被部署到tomcat中时,项目的结构会发 ...
- 全文检索 java Lucene
索引文件:[D:\luceneDemo\data\TXT小说\陛下是妻迷.txt] 大小:[1185.0 KB] 索引文件:[D:\luceneDemo\data\TXT小说\随身空间重生在七十年代. ...
- H3C 快速以太网和千兆以太网
- 9-6 UVa 11400
参考 http://www.cnblogs.com/Kiraa/p/5510757.html http://www.cnblogs.com/zhaopAC/p/5159950.html 根据题目说明中 ...
- EL表达式中的empty和null
EL表达式中的empty和null 先说一下EL表达式中的null和empty区别,然后再说说最近在项目中出现的一个有趣的问题. EL中的null和empty都可用来判断值是否为空,但两者存在略微的区 ...
- Educational Codeforces Round 11、A B题
A. Co-prime Array 题意:给你一个数列,要求构造两两相邻之间的数互质的数列,可以插入的数的小于10的9次方 思路:其实可以选择靠近10的9次方的最大的三个素数.然后按我下面的方法做就可 ...
- 如何检查linux是否安装了php
方法一.在终端通过php -v命令来查看一下当前php的版本.如果没有安装php,一般会提示没有php这个命令的. 2 方法二.在终端查询安装的包中是否有php,以redhat为例,则可以执行如下命令 ...
- 用diiv实现多个方块居中嵌套--margin
文章地址 https://www.cnblogs.com/sandraryan/ 案例:用diiv嵌套多个正方形,配合盒模型相关知识,使每个div在他的父元素上居中.(每个div中心点对齐) 涉及到m ...
- gyp verb check python checking for Python executable "python2" in the PATH
缺少python2.7支持 可快速使用以下语句完成安装 npm install --global --production windows-build-tools 到时候会自动下载python的 如果 ...