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 ...
随机推荐
- css3 文本效果
CSS3 文本效果 1 CSS3 文本阴影在 CSS3 中,text-shadow 可向文本应用阴影,能够规定水平阴影.垂直阴影.模糊距离,以及阴影的颜色.text-shadow: 5px 5px ...
- 代码快捷键的设置读取App.config方法
附件下载:http://files.cnblogs.com/files/qtiger/ShortcutAchieve.zip 代码实现最重要(增加引用using System.Configuratio ...
- 【转】准确理解CSS clear:left/right的含义及实际用途
零.说点什么 好久没更新了.并不是在折腾什么大作,而是广度学习与实践中,加上婚礼等诸多大事,所以产出较少. 今天这篇也只是小作,博客是自己很好的学习工具,只要我学习不止,博客也会不断更新的. 我们平时 ...
- jQuery 遍历each()的使用方法
.each()是一个for循环的包装迭代器.each()通过回调的方式处理,并且会有2个固定的实参,索引与元素(从0开始计数).each()回调方法中的this指向当前迭代的dom元素 遍历方法: & ...
- redis的数据类型
redis有string,hash,list,sets.zsets几种数据类型 1.string数据类型 可包含任何数据,是二进制安全的,比如图片或者序列化的对象set key valueset na ...
- windows下 apache 二级域名相关配置
小编今天给大家总结下 windows 下 apache的二级域名的相关配置 利用.htaccess将域名绑定到子目录 下面就利用本地127.0.0.1进行测试 我们这里以 www.jobs.com 为 ...
- about the pageload and page init event
Page_Init The Page_Init event is the first to occur when an ASP.NET page is executed. This is where ...
- SQLite数据库管理的相关命令
1.创建数据库 启动命令行,通过输入如下命令打开Shell模式的CLP: sqlite3 test.db 虽然我们提供了数据库名称,但如果该数据库不存在,SQLite实际上就未创建该数据库,直到在数据 ...
- p ython笔记第三天
#!/usr/bin/env python# -*- coding:utf-8 -*- # v = "李杰"# for item in v:# print(item)####### ...
- python 常用函数、内置函数清单
文章内容摘自:http://www.cnblogs.com/vamei 1.type() 查询变量的类型 例: >>> a = 10>>> print a10> ...