POJ Round Numbers(数位DP)
题目大意:
Round Number: 将一个整数转化为二进制数字后,(不含前导0) 要是0的个数 大于等于1的个数 则是 Round Number
问从L-R之中有多少个Round Number
题目分析:
要转化为2进制数字,我们用10进制保存明显不好判断0和1的个数,所以选择用8进制来存储,这样的话每一次进位会多出 ”000“, 然后再加上8进制的尾数, 最后我们可以得出增加了几个 1 和 增加了 几个 0
需要注意的一点就是, 当前面的数字小于 8 的时候 我们要对 前导 0 进行特殊判断
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef __int64 LL;
LL dp[][][];//dp[位数][0的个数][1的个数]
int bit[];
int binary[][] = { {,}, {,},{,},{,},{,},{,},{,},{,} };
LL dfs(int pos,int preCou0,int preCou1,int flag,int len)
{
if(pos == -)
{
return (preCou1 || preCou0) && preCou0 >= preCou1;//这里要对 0 进行特殊判断, 将0去掉
} if( !flag && dp[pos][preCou0][preCou1] != -)
return dp[pos][preCou0][preCou1]; LL ans = ;
int end = flag?bit[pos]:; for(int i=; i<= end; i++)
{
int nowCou0 = preCou0 + binary[i][];
int nowCou1 = preCou1 + binary[i][];
if(preCou0 == && preCou1 == )//判断是否是第一位,若是第一位则需要进行特殊处理
{
if(i == || i == )nowCou0 = ;
if(i == )nowCou0 = ;
if(i == )nowCou0 = ;
} ans += dfs(pos-, nowCou0, nowCou1, flag && i == end, len);
}
if(!flag)
dp[pos][preCou0][preCou1] = ans; return ans;
} LL solve(LL n)
{
int len = ; while(n)
{
bit[len++] = n%;
n /= ;
} return dfs(len-, , , , len-);
} int main()
{
LL a, b; memset(dp, - ,sizeof(dp));
while(scanf("%I64d%I64d", &a, &b) != EOF)
{
// printf("%I64d\n", solve(a));
// printf("%I64d\n", solve(b));
printf("%I64d\n", solve(b) - solve(a-) );
}
return ;
} /* 0的个数 大于等于 1的个数
1 0001
2 0010 1
3 0011
4 0100 1
5 0101
6 0110
7 0111
8 1000 1
9 1001 1
10 1010 1
11 1011
12 1100 1
13 1101
14 1110
15 1111
16 10000 1 */
POJ Round Numbers(数位DP)的更多相关文章
- 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 Round Numbers(数位dp&记忆化搜索)
题目链接:[kuangbin带你飞]专题十五 数位DP E - Round Numbers 题意 给定区间.求转化为二进制后当中0比1多或相等的数字的个数. 思路 将数字转化为二进制进行数位dp,由于 ...
- POJ - 3252 - Round Numbers(数位DP)
链接: https://vjudge.net/problem/POJ-3252 题意: The cows, as you know, have no fingers or thumbs and thu ...
- poj 3252 Round Numbers 数位dp
题目链接 找一个范围内二进制中0的个数大于等于1的个数的数的数量.基础的数位dp #include<bits/stdc++.h> using namespace std; #define ...
- $POJ$3252 $Round\ Numbers$ 数位$dp$
正解:数位$dp$ 解题报告: 传送门$w$ 沉迷写博客,,,不想做题,,,$QAQ$口胡一时爽一直口胡一直爽$QAQ$ 先港下题目大意嗷$QwQ$大概就说,给定区间$[l,r]$,求区间内满足二进制 ...
- POJ3252 Round Numbers —— 数位DP
题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Su ...
- Round Numbers(数位DP)
Round Numbers http://poj.org/problem?id=3252 Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- 4-圆数Round Numbers(数位dp)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14947 Accepted: 6023 De ...
- poj3252 Round Numbers (数位dp)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
随机推荐
- [Angular 2] The form export from NgFormControl
In last post, we need to create an instanse variable: sku: AbstructControl; We can get rid of this b ...
- python 参议院文本预处理的一维数组的间隔空间
#!/usr/bin/python import re def pre_process_msg ( msgIn ): if msgIn=="": retur ...
- leetcode第一刷_Construct Binary Tree from Preorder and Inorder Traversal
构造方式跟中序与后序全然一样,并且一般都习惯正着来,所以更简单. 代码是之前写的,没实用库函数,不应该. TreeNode *buildIt(vector<int> &preord ...
- iOS网络HTTP、TCP、UDP、Socket 知识总结
OSI 七层模型 我们一般使用的网络数据传输由下而上共有七层,分别为物理层.数据链路层.网络层.传输层.会话层.表示层.应用层,也被依次称为 OSI 第一层.第二层.⋯⋯. 第七层. 如下图: 各层功 ...
- codevs3304水果姐逛水果街
/* 线段树开到*4 *4 *4 *4 ! 维护 4个值 区间最大值 区间最小值 从左往右跑最大收益 从右往左跑最大收益 */ #include<iostream> #include< ...
- Linq101-Quantifiers
using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Quantif ...
- Asp.net中的页面跳转及post数据
/// <summary> /// This method prepares an Html form which holds all data /// in hidden field i ...
- c#将Excel数据导入到数据库的实现代码(OleDb)
sing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web ...
- C# Double类型 不四舍五入
测试中发现Double类型需要#0.00 小数点精度为后2位,并且多余部分不需要四舍五入,直接截断 用字符串处理也可以,但是比较麻烦 这里给出一种思路: double a = 9999.999; a ...
- 01-android快速入门
adb Android debug bridge 安卓调试桥 创建模拟器,屏幕尽量小些,启动速度运行速度快 Android项目的目录结构 Activity:应用被打开时显示的界面 src:项目代码 R ...