题意:

如果你个数的二进制中1的个数要小于等于0的个数,那么这个数就符合题意。现在要你找出来区间[li,ri]这个区间内有多少这样的数

题解:

题意很明显了,是要用二进制,所以我们也把给的区间边界转化成二进制存在数组里面。然后再来枚举。要注意一点前导零可不能算在进去。比如1,它的二进制是1,那么也可以是01、001、0001等。所以前导零要排除。

dp[x][y][z]表示从开始到x位,没有前导零的前提下,已经有y个0,z个1。注意要没有前导零
在dfs过程中如果一个数前导零还存在情况下是不能直接返回dp值的,同样枚举到这个位置前导零还存在的话也不能赋值给dp数组比如二进制0001和1000他们在dp数组中所占的位置是一样的。这样就会错!

代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 #include<iostream>
5 using namespace std;
6 const int maxn=105;
7 typedef long long ll;
8 ll v[maxn],dp[maxn][maxn][maxn],ci,w[maxn];
9 ll dfs(int pos, int one, int zero, bool flag, bool limit)
10 {
11 if (pos == -1)
12 {
13 if (one <= zero) return 1;
14 return 0;
15 }
16 if (!limit && flag && dp[pos][one][zero] != -1) return dp[pos][one][zero];
17 int up = limit ? v[pos] : 1;
18 ll res = 0;
19 for (int i = 0; i <= up; i++)
20 {
21 if (i == 0)
22 {
23 if (flag) res += dfs(pos - 1, one, zero + 1, flag, limit && v[pos] == i);
24 else res += dfs(pos - 1, one, zero, flag, limit && v[pos] == i);
25 }
26 else res += dfs(pos - 1, one + 1, zero, true, limit && v[pos] == i);
27 }
28 if (!limit && flag) dp[pos][one][zero] = res;
29 return res;
30 }
31 ll solve(ll ans)
32 {
33 ll pos=0;
34 while(ans)
35 {
36 v[pos++]=ans%2;
37 ans/=2;
38 }
39 ci=pos;
40 return dfs(pos-1,0,0,false,true);
41 }
42 int main()
43 {
44
45 ll l,r;
46 memset(dp,-1,sizeof(dp));
47 scanf("%I64d%I64d",&l,&r);
48 printf("%I64d\n",solve(r)-solve(l-1));
49 return 0;
50 }

Round Numbers POJ - 3252的更多相关文章

  1. POJ 3252 Round Numbers(组合)

    题目链接:http://poj.org/problem?id=3252 题意: 一个数的二进制表示中0的个数大于等于1的个数则称作Round Numbers.求区间[L,R]内的 Round Numb ...

  2. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  3. POJ 3252 Round Numbers

     组合数学...(每做一题都是这么艰难) Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7607 A ...

  4. [ACM] POJ 3252 Round Numbers (的范围内的二元0数大于或等于1数的数目,组合)

    Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8590   Accepted: 3003 Des ...

  5. poj 3252 Round Numbers(数位dp 处理前导零)

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

  6. POJ - 3252 A - Round Numbers

    The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' ...

  7. POJ 3252 Round Numbers 数学题解

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

  8. POJ 3252 Round Numbers 组合数学

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13381   Accepted: 5208 Description The ...

  9. POJ 3252 Round Numbers(组合数学)

    Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10223   Accepted: 3726 De ...

随机推荐

  1. win10/windows 安装Pytorch

    https://pytorch.org/get-started/locally/ 去官网,选择你需要的版本. 把 pip install torch==1.5.0+cu101 torchvision= ...

  2. 【ASM】查看ASM磁盘组剩余容量和总容量

    col total_size for a10; col free_size for a20; select name,total_mb/1024 || 'G' as total_size , free ...

  3. Java调用Linux命令执行

    调用方式 Java调用linux命令执行的方式有两种,一种是直接调用linux命令,一种是将linux命令写到.sh脚本中,然后调用脚本执行. 详细说明 直接调用:使用java中lang包下面的Run ...

  4. SDUST数据结构 - chap3 栈和队列

    一.判断题: 二.选择题: 三.编程题: 7-1 一元多项式求导: 输入样例: 3 4 -5 2 6 1 -2 0 输出样例: 12 3 -10 1 6 0 代码: #include<bits/ ...

  5. K8s遇到问题解决思路

    问题排查一 从describe去查找相应的deploy/pod/rs/svc [root@k8s-master ~]# kubectl describe po/nginx-f95d765f9-8b6b ...

  6. oracle_fdw的安装和使用

    1.下载instant oracle client 下载网址:https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html ...

  7. 5V充8.4V,5V升压8.4V给电池充电的芯片电路

    5V充8.4V的锂电池,需要把USB口的5V输入,升压转换成8.4V来给两串电池充电. 5V升压8.4V给锂电池充电的专门充电IC 集成了5V升压8.4V电路和充电管理电路的PL7501C 如果不需要 ...

  8. Vue之创建组件之配置路由!

    Vue之创建组件之配置路由!== 第一步: 当然就是在我们的试图文件夹[views]新建一个文件夹比如home 在home文件夹下面新建一个文件index.vue 第二步:在router目录下做如下事 ...

  9. VMware 虚拟机逃逸漏洞

    所谓虚拟机逃逸(Escape Exploit),指的是突破虚拟机的限制,实现与宿主机操作系统交互的一个过程,攻击者可以通过虚拟机逃逸感染宿主机或者在宿主机上运行恶意软件. 针对 VMware 的虚拟机 ...

  10. (001)每日SQL学习:关于UNION的使用

    union内部必须有相同的列或者相同的数据类型,同时,每条 SELECT 语句中的列的顺序必须相同.union合并了select的结果集. union 与union all的不同: union合并了重 ...