LightOJ 1140 How Many Zeroes
题意:写出一个给定区间的每个数,求出一共写了多少个零。
解法:数位DP,定义dp[len][flag][num]:len的定义为数位的长度,flag定义为前导0和没有前导0的两种状态,num定义为写的满足条件的0的个数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
#define exp 1e-10
#define PI 3.141592654
using namespace std;
typedef long long LL;
LL digit[];
LL dp[][][];
LL dfs(LL len,bool flag,LL p,LL p2,bool fp)
{
if (!len)
{
if (!flag)
return p2-p;
else return ;
}
if (!fp && dp[len][flag][p2-p]!=-) return dp[len][flag][p2-p];
LL ret=;
LL fpmax= fp ? digit[len] : ;
for (LL i= ;i<=fpmax ;i++)
{
bool ok= i== ? true : false;
LL temp;
if (i==) temp= ; else temp= ;
ret += dfs(len-,flag&&ok,flag?p+temp:p ,p2+temp,fp&&i==fpmax);
}
if (!fp) return dp[len][flag][p2-p]=ret;
return ret;
}
LL f(LL n)
{
if (n==-) return -;
if (n==) return ;
LL len=;
while (n)
{
digit[++len]=n%;
n /= ;
}
return dfs(len,true,,,true);
}
int main()
{
int t;
int ncase=;
cin>>t;
while (t--)
{
LL a,b;
scanf("%lld%lld",&a,&b);
memset(dp,-,sizeof(dp));
printf("Case %d: %lld\n",ncase++,f(b)-f(a-));
}
return ;
}
LightOJ 1140 How Many Zeroes的更多相关文章
- lightoj 1140 - How Many Zeroes?(数位dp)
Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...
- LightOJ 1140 How Many Zeroes? (数位DP)
题意:统计在给定区间内0的数量. 析:数位DP,dp[i][j] 表示前 i 位 有 j 个0,注意前导0. 代码如下: #pragma comment(linker, "/STACK:10 ...
- How Many Zeroes? LightOJ - 1140
#include<stdio.h> #include<string.h> #include<math.h> #include<time.h> #incl ...
- 数位dp(D - How Many Zeroes? LightOJ - 1140 )
题目链接:https://cn.vjudge.net/contest/278036#problem/D 题目大意:T组测试数据,每一次输入两个数,求的是在这个区间里面,有多少个0,比如说19203包括 ...
- LightOJ 1140: How Many Zeroes? (数位DP)
当前数位DP还不理解的点: 1:出口用i==0的方式 2:如何省略状态d(就是枚举下一个数的那个状态.当然枚举还是要的,怎么把空间省了) 总结: 1:此类DP,考虑转移的时候,应当同时考虑查询时候的情 ...
- Light oj 1140 How Many Zeroes?
Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...
- light oj 1140 - How Many Zeroes? 数位DP
思路:dp[i][j]:表示第i位数,j表示是否有0. 代码如下: #include<iostream> #include<stdio.h> #include<algor ...
- LightOJ 1140 计数/数位DP 入门
题意: 给出a,b求区间a,b内写下过多少个零 题解:计数问题一般都会牵扯到数位DP,DP我写的少,这道当作入门了,DFS写法有固定的模板可套用 dp[p][count] 代表在p位 且前面出现过co ...
- LightOJ - 1140 统计0的数位 数位DP
注意以下几点: 搜索维度非约束条件的都要记录,否则大概率出错,比如_0 st参数传递和_0的互相影响要分辨清楚 num==-1就要返回0而不是1 #include<iostream> #i ...
随机推荐
- 官网下载Spring dist
新版Spring官网下载Spring的dist可真是麻烦 跟着下面的贴图走吧,有些在网页的下面,需要打开相应页面后往下拉拉. 下载完后解压lib里面就是各种jar包了 真是麻烦啊,不好找,不过Spri ...
- 封装document.ready方法
function $(fn){ if(document.addEventListener){ //W3C document.addEventListener('DOMContentLoaded',fu ...
- LevelDB:一个快速轻量级的key-value存储库(译)
作者:Jeff Dean, Sanjay Ghemawat 原文:http://leveldb.googlecode.com/svn/trunk/doc/index.html 译者:phylips@b ...
- Mysql查询(笔记二)
1.两结构相同的表数据间移植 Inset into 表一 Select 字段1,字段2,....字段n from表二 建立数据库时设置数据库编码 create database 数据库名 charse ...
- 深入理解 /etc/fstab文件
发布:thebaby 来源:net [大 中 小] /etc/fstab是用来存放文件系统的静态信息的文件.位于/etc/目录下,可以用命令less /etc/fstab 来查看,如果要修改的 ...
- mysql中存不进去json_encode格式的数据
主要是因为json_encode格式的数据,中间带有\,在存入数据库的时候,会把反斜杠删除了. 所以,想要存进去的话,需要在外层调用一下函数addslashes();这个函数会在每个反斜杠的前面添加反 ...
- 删:Centos 7安装Nginx 1.8
[CentOS 7] 安装nginx! 首先进行 nginx yum Nginx安装记录 注意:如果用源码安装,nginx配置时需要指定--with-pcer对应的压缩包路径,如果使用二进制安装不需要 ...
- html中的框架
1.总的代码 <frameset rows=20%,*> <frame src="top.html" /> <frameset cols=30%,*& ...
- Simple Shopping Cart By AngularJS
<body ng-controller='CartController'> <h1>Your Order</h1> <div ng-repeat='item ...
- 淘宝:OceanBase分布式系统负载均衡案例分享
Heroku因"随机调度+Rails单线程处理导致延迟增加的负载均衡失败"的案例之后,我们在思考:在负载均衡测试时发现问题并妥善解决的成功经验有没有?于是,挖掘出"淘宝在 ...