题意:统计在给定区间内0的数量。

析:数位DP,dp[i][j] 表示前 i 位 有 j 个0,注意前导0.

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL dp[15][15];
int a[15]; LL dfs(int pos, int num, bool is, bool ok){
if(!pos) return is ? 1 : num;
LL &ans = dp[pos][num];
if(!ok && ans >= 0 && !is) return ans; LL res = 0;
int n = ok ? a[pos] : 9;
for(int i = 0; i <= n; ++i){
if(is) res += dfs(pos-1, num, !i, ok && i == n);
else if(!i) res += dfs(pos-1, num+1, false, ok && i == n);
else res += dfs(pos-1, num, false, ok && i == n);
}
if(!ok && !is) ans = res;
return res;
} LL solve(LL n){
if(n == -1) return 0;
int len = 0;
while(n > 0){
a[++len] = n % 10;
n /= 10;
}
return dfs(len, 0, true, true);
} int main(){
memset(dp, -1, sizeof dp);
LL x, y;
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%lld %lld", &x, &y);
printf("Case %d: %lld\n", kase, solve(y)-solve(x-1));
}
return 0;
}

LightOJ 1140 How Many Zeroes? (数位DP)的更多相关文章

  1. light oj 1140 - How Many Zeroes? 数位DP

    思路:dp[i][j]:表示第i位数,j表示是否有0. 代码如下: #include<iostream> #include<stdio.h> #include<algor ...

  2. UVALive - 6575 Odd and Even Zeroes 数位dp+找规律

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48419 Odd and Even Zeroes Time Limit: 3000MS 问题描述 In mat ...

  3. LightOJ 1032 - Fast Bit Calculations 数位DP

    http://www.lightoj.com/volume_showproblem.php?problem=1032 题意:问1~N二进制下连续两个1的个数 思路:数位DP,dp[i][j][k]代表 ...

  4. lightoj 1021 - Painful Bases(数位dp+状压)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1021 题解:简单的数位dp由于总共就只有16个存储一下状态就行了.求各种进制能 ...

  5. LightOJ 1140 How Many Zeroes

    题意:写出一个给定区间的每个数,求出一共写了多少个零. 解法:数位DP,定义dp[len][flag][num]:len的定义为数位的长度,flag定义为前导0和没有前导0的两种状态,num定义为写的 ...

  6. lightoj 1140 - How Many Zeroes?(数位dp)

    Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...

  7. LightOJ - 1140 统计0的数位 数位DP

    注意以下几点: 搜索维度非约束条件的都要记录,否则大概率出错,比如_0 st参数传递和_0的互相影响要分辨清楚 num==-1就要返回0而不是1 #include<iostream> #i ...

  8. 数位dp(D - How Many Zeroes? LightOJ - 1140 )

    题目链接:https://cn.vjudge.net/contest/278036#problem/D 题目大意:T组测试数据,每一次输入两个数,求的是在这个区间里面,有多少个0,比如说19203包括 ...

  9. LightOJ 1140: How Many Zeroes? (数位DP)

    当前数位DP还不理解的点: 1:出口用i==0的方式 2:如何省略状态d(就是枚举下一个数的那个状态.当然枚举还是要的,怎么把空间省了) 总结: 1:此类DP,考虑转移的时候,应当同时考虑查询时候的情 ...

随机推荐

  1. Linux驱动基础开发

    Linux 内核配置机制(make menuconfig.Kconfig.makefile)讲解 前面我们介绍模块编程的时候介绍了驱动进入内核有两种方式:模块和直接编译进内核,并介绍了模块的一种编译方 ...

  2. gulp 安装时一直提示缺少模块( Cannot find module 'gulp-load-plugins')

    我们要考虑两种情况? 1. 本地安装和全局安装gulp npm i -g gulp && npm i --save-dev gulp 2.新建package.json,然后手动填写缺少 ...

  3. wamp配置虚拟域名

    1.打开apache下httpd.conf 我的目录是在F:\wamp\bin\apache\apache2.2.22\conf\httpd.conf 2.去掉这两行前面的#注释 LoadModule ...

  4. redis中关于过期键的删除策略

    我们已经了解到了Redis是一种内存数据库,Redis中数据都是以key-value的形式存储在内存中.由Redisserver来维护和管理这部分内存,内存是何足珍贵,不须要的数据或者是已经使用过的无 ...

  5. 标准C头文件

    ISO C标准定义的头文件: POSIX标准定义的必须的头文件: POSIX标准定义的XSI可选头文件: POSIX标准定义的可选头文件:

  6. VS2008转VS2013时遇到的问题

    最近我们要把DPM进行行人检测嵌入到我们的项目里,需要一个高级版本的VS,于是我们要把2008转换成2013,至于为什么没有换成最高级的版本,可能担心会遇到有更多的麻烦吧,毕竟我们的DPM源码是在20 ...

  7. openwrt: sysupgrade

    sysupgrade 用法: sysupgrade [...] sysupgrade [-q] [-i] 升级选项: -d 重启前等待 delay 秒 -f 从 .tar.gz (文件或链接) 中恢复 ...

  8. 如何在外部采用AES-128对HLS的TS流进行加密

    http://www.wowza.cn/hls/howToSecureHlsByAes128External

  9. ajax返回页面停留跳转

    ajax返回数据后,页面停留跳转. 原理:利用匿名函数自动运行的特性和定时器来完成. (function(){ ; // 设置停留时间单位秒 var href =data.url; //设置跳转的ur ...

  10. BMP文件格式具体解释

    BMP文件格式具体解释(BMP file format) BMP文件格式,又称为Bitmap(位图)或是DIB(Device-Independent Device,设备无关位图),是Windows系统 ...