Round Numbers POJ - 3252
题意:
如果你个数的二进制中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的更多相关文章
- POJ 3252 Round Numbers(组合)
题目链接:http://poj.org/problem?id=3252 题意: 一个数的二进制表示中0的个数大于等于1的个数则称作Round Numbers.求区间[L,R]内的 Round Numb ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- POJ 3252 Round Numbers
组合数学...(每做一题都是这么艰难) Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7607 A ...
- [ACM] POJ 3252 Round Numbers (的范围内的二元0数大于或等于1数的数目,组合)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8590 Accepted: 3003 Des ...
- poj 3252 Round Numbers(数位dp 处理前导零)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- POJ - 3252 A - Round Numbers
The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' ...
- POJ 3252 Round Numbers 数学题解
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- POJ 3252 Round Numbers 组合数学
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13381 Accepted: 5208 Description The ...
- POJ 3252 Round Numbers(组合数学)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10223 Accepted: 3726 De ...
随机推荐
- 【Spring Boot】创建一个简单的Spring Boot的 Demo
走进Spring Boot 文章目录 走进Spring Boot 环境搭建 新建Spring Boot项目 开始创建项目 配置JDK版本 和 Initializr Service URL 配置Proj ...
- 【ORA】ORA-4031错误分析和解决办法
1. ORA-4031错误的原因,一般是大量的hard parse导致了shared pool中的free list中产生大量的内存小碎片,当一个需要很大内存来进行hard parse的sql语句到来 ...
- 国人之光:大数据分析神器Apache Kylin
一.简介 Apache Kylin是一个开源的.分布式的分析型数据仓库,提供Hadoop/Spark 之上的 SQL 查询接口及多维分析(OLAP)能力以支持超大规模数据,最初由 eBay 开发并贡献 ...
- oracle动态采样导致数据库出现大量cursor pin s wait on x等待
生产库中,突然出现了大量的cursor pin s wait on x等待,第一反应是数据库出现了硬解析,查看最近的DDL语句,没有发现DDL.那么有可能这个sql是第一次进入 在OLTP高并发下产生 ...
- 技术基础 | Apache Cassandra 4.0基准测试
Apache Cassandra 4.0已经发布了Beta版,这是第一个支持JDK 11及更高JDK版本的Cassandra版本. 时延对于Apache Cassandra用户来说是个显而易见的关 ...
- 解决windows git乱码问题
在windows中打开git bash git config --global i18n.commitencoding utf-8 设置提交日志使用utf-8 git config --g ...
- 洛谷 P4999
题目链接: P4999 烦人的数学作业 题目大意 详见题目 solution 有一个显而易见的结论 发现 \(ans_{l, r} = ans_{1. r} - ans_{1, l - 1}\) 那只 ...
- loj10001种树
好久不写博客了,发现不好找做过和题!还得接着写啊! ------------------------------------------------------------------ 题目描述 某条 ...
- (二)基于shard-jdbc中间件,实现数据分库分表
基于shard-jdbc中间件,实现数据分库分表 Sharding-JDBC简介 Sharding配置示意图 1.水平分割 1.1 水平分库 1.2 水平分表 2.Shard-jdbc中间件 2.1 ...
- Docker and Kubernetes -- 监控(weave scope)
docker常用的监控工具 weave scope 简介 Weave Scope是Docker和Kubernetes的可视化监控管理软件 Weave Scope 会自动生成容器之间的关系图,方便理解容 ...