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 ...
随机推荐
- 数据库系统——B+树索引
原文来自于:http://dblab.cs.toronto.edu/courses/443/2013/05.btree-index.html 1. B+树索引概述 在上一篇文章中,我们讨论了关于ind ...
- 浅析mysql 共享表空间与独享表空间以及他们之间的转化
innodb这种引擎,与MYISAM引擎的区别很大.特别是它的数据存储格式等.对于innodb的数据结构,首先要解决两个概念性的问题: 共享表空间以及独占表空间.什么是共享表空间和独占表空间共 ...
- Ubuntu上glibc CVE-2015-7547漏洞的POC验证和修复
Ubuntu上查看Glibc版本 $ldd --version ldd (Ubuntu GLIBC 2.21-0ubuntu4) 2.21 Ubuntu上查看使用Glibc的相关程序 sudo lso ...
- ViewPagerindicator 源码解析
ViewPagerindicator 源码解析 1. 功能介绍 1.1 ViewPagerIndicator ViewPagerIndicator用于各种基于AndroidSupportL ...
- 转载:C#实现接口回调
通常情况下,我们创建一个对象,并马上直接去使用它的方法.然而,在有些情况下,希望能在某个场景出现后或条件满足时才调用此对象的方法.回调就可以解决这个“延迟调用对象方法”的问题.这个被调用方法的对象称为 ...
- (转)SQL Server中使用convert进行日期转换
原文链接:http://www.cnblogs.com/weiqt/articles/1826847.html SQL Server中使用convert进行日期转换 一般存入数据库中的时间格式为yyy ...
- C# 汉字的字符串截取指定字节的长度
int index = 0; int setCharCount = 74; string str1 = "三星 SCH-I829 电信3G手机(优 ...
- Razor html标签
1.Label Html语法: <label for=“UserName”>用户名</label> Razor语法: @Html.LabelFor(m=>m.UserNa ...
- Oracle11G 数据库 expdp、impdp使用示例
expdp 备份数据库.expdp/impdp简单测试 操作系统层面创建目录 [root@Oracle11g ~]# mkdir -p /home/oracle/db_back/ 修改目录的所属用户. ...
- 认识html文件基本结构
html文件的结构:一个HTML文件是有自己固定的结构的. <html> <head>...</head> <body>...</body> ...