题目大意:

求区间 \([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) 来解决这个问题。

其中,

  • posnum0num1all0 所表示的含义和上述表述一致,
  • 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的更多相关文章

  1. poj3252 Round Numbers(数位dp)

    题目传送门 Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16439   Accepted: 6 ...

  2. POJ3252 Round Numbers 【数位dp】

    题目链接 POJ3252 题解 为什么每次写出数位dp都如此兴奋? 因为数位dp太苟了 因为我太弱了 设\(f[i][0|1][cnt1][cnt0]\)表示到二进制第\(i\)位,之前是否达到上界, ...

  3. POJ 3252 Round Numbers(数位dp)

    题意:给定区间[l,r],l < r ,求区间中满足条件的正整数的个数:二进制表示下0的个数不少于1的个数. 分析:f(x)表示<=x时满足条件的数的个数,所求问题即为f(r)-f(l-1 ...

  4. [BZOJ1662][POJ3252]Round Numbers

    [POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...

  5. POJ3252 Round Numbers —— 数位DP

    题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Su ...

  6. poj3252 Round Numbers (数位dp)

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  7. 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 ...

  8. Codeforces Beta Round #51 D. Beautiful numbers(数位dp)

    题目链接:https://codeforces.com/contest/55/problem/D 题目大意:给你一段区间[l,r],要求这段区间中可以整除自己每一位(除0意外)上的数字的整数个数,例如 ...

  9. poj3252 Round Numbers

    Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7625   Accepted: 2625 Des ...

随机推荐

  1. oracle函数 NLS_LOWER(x[,y])

    [功能]返回字符串并将字符串的变为小写; [参数]x字符型表达式 [参数]Nls_param可选,指定排序的方式(nls_sort=) . SCHINESE_RADICAL_M(部首.笔画) SCHI ...

  2. getopt、getopt_long和getopt_long_only解析命令行参数

    一:posix约定: 下面是POSIX标准中关于程序名.参数的约定: 程序名不宜少于2个字符且不多于9个字符: 程序名应只包含小写字母和阿拉伯数字: 选项名应该是单字符或单数字,且以短横 '-' 为前 ...

  3. pytorch BiLSTM+CRF代码详解 重点

    一. BILSTM + CRF介绍 https://www.jianshu.com/p/97cb3b6db573 1.介绍 基于神经网络的方法,在命名实体识别任务中非常流行和普遍. 如果你不知道Bi- ...

  4. java操作数组的工具类-Arrays

    static int binarySearch(type[] a, type key) 使用二分搜索法来搜索key元素在数组中的索引:若a数组不包括key,返回负数.(该方法必须已按升序排列后调用). ...

  5. maven环境隔离

    pom <build>节点下增加节点 <resources> <resource> <directory> src/main/resources.${d ...

  6. 【codeforces 761D】Dasha and Very Difficult Problem

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. python基础十四之匿名函数

    匿名函数 处理简单问题的简化函数,关键字lambda. # 格式:函数名 = lambda 参数:返回值 anonymity = lambda s: s ** 0.5 print(anonymity( ...

  8. 2019-10-10-优雅调试-REST-API-的工具

    title author date CreateTime categories 优雅调试 REST API 的工具 lindexi 2019-10-10 20:9:33 +0800 2019-10-1 ...

  9. 【git】Git回退代码到指定版本

    1. 查看所有的历史版本,获取你git的某个历史版本的id, git log2. 回退本地代码库:git reset --hard ID3. 推送到远程服务器:git push -f -u origi ...

  10. P1065 汪老师的烟

    题目描述 汪老师有n根烟,他每吸完一根烟就把烟蒂保存起来,\(k(k>1)\) 个烟蒂可以换一个新的烟,那么 汪老师 最终能吸到多少根烟呢? 输入格式 每组测试数据一行包括两个整数 \(n,k( ...