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 ...
随机推荐
- @topcoder - SRM611D1L3@ ElephantDrinking
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定有一个 n*n 的平原,其中一些格子有些泉水.泉水每单位时间 ...
- Android ListView显示底部的分割线
有些时候,我们会提出这样的需求,希望ListView显示底部(顶部)的分割线,这样做,会使得UI效果更加精致,如下图所示: 如果搜索资料,大家会搜到一堆相关的方法,最多的莫过于设置listview的f ...
- 2、asp.net core 部署到服务器之后外网访问不了
解决问题 把自定义端口的http://localhost:5001改成http://*:5001. 什么都没有改也不行的小伙伴试试在Program的Main方法中的.UseKestrel()后面添加. ...
- 唯一索引与非唯一索引区别(UNIQUE INDEX, NON-UNIQUE INDEX)
索引是我们经常使用的一种数据库搜索优化手段.适当的业务操作场景使用适当的索引方案可以显著的提升系统整体性能和用户体验.在Oracle中,索引有包括很多类型.不同类型的索引适应不同的系统环境和访问场景. ...
- laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题
https://stackoverflow.com/questions/48568739/unable-to-open-file-for-reading-swift-ioexception-in-la ...
- 洛谷 3177 [HAOI2015] 树上染色
题目描述 有一棵点数为 N 的树,树边有边权.给你一个在 0~ N 之内的正整数 K ,你要在这棵树中选择 K个点,将其染成黑色,并将其他 的N-K个点染成白色 . 将所有点染色后,你会获得黑点两两之 ...
- jq动画和停止动画
使用jq 实现动画循环效果 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- H3C 帧中继显示与调试
- Vue 获取DOM元素ref
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python--day39--进程池的回调函数callback
运行结果: