**题意:** 给出a,b求区间a,b内写下过多少个零
**题解:**计数问题一般都会牵扯到数位DP,DP我写的少,这道当作入门了,DFS写法有固定的模板可套用

dp[p][count] 代表在p位 且前面出现过count个零的方案数

/** @Date    : 2016-10-27-17.26

* @Author : Lweleth (SoungEarlf@gmail.com)

* @Link :

* @Version : $

*/

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <algorithm>

#include <utility>

#include <vector>

#include <map>

#include <set>

#include <string>

#include <stack>

#include <queue>

#define LL long long

#define MMF(x) memset((x),0,sizeof(x))

#define MMI(x) memset((x), INF, sizeof(x))

using namespace std;



const int INF = 0x3f3f3f3f;

const int N = 1e5+2000;
int dp[25][25];

LL bit[25];
//位置 前面零的数量 是否是数的最后一位 是否是零

LL dfs(int p, int cnt, int ima, int ipz)

{

//if(p == -1)

//system("pause");

//cout << p << endl;

if(p == 0)

return cnt;

if(!ima && dp[p][cnt] != -1 && !ipz)

return dp[p][cnt];
int len = ima?bit[p]:9;

LL ans = 0;

for(int i = 0; i <= len; i++)

{

int t = cnt;

if(i == 0 && !ipz)//注意当前是零时 且前面没有前导零

t++;

ans+=dfs(p-1, t, ima && i == len, ipz && i == 0);



}

if(!ima && !ipz)

dp[p][cnt] = ans;

//cout << ans << endl;

return ans;

}



LL sol(LL x)

{

if(x == -1)//考虑a是0的情况

return -1;

int len = 0;

while(x)

{

bit[++len] = x % 10;

x /= 10;

}

return dfs(len, 0, 1, 1);

}



int main()

{

int T;

cin >> T;

int cnt = 0;

while(T--)

{

LL a, b;

scanf("%lld%lld", &a, &b);

memset(dp, -1, sizeof(dp));

printf("Case %d: %lld\n", ++cnt, sol(b) - sol(a - 1));

}

return 0;

}

LightOJ 1140 计数/数位DP 入门的更多相关文章

  1. 数位dp入门 hdu2089 不要62

    数位dp入门 hdu2089 不要62 题意: 给定一个区间[n,m] (0< n ≤ m<1000000),找出不含4和'62'的数的个数 (ps:开始以为直接暴力可以..貌似可以,但是 ...

  2. HDU 2089 不要62【数位DP入门题】

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. xbz分组题B 吉利数字 数位dp入门

    B吉利数字时限:1s [题目描述]算卦大湿biboyouyun最近得出一个神奇的结论,如果一个数字,它的各个数位相加能够被10整除,则称它为吉利数.现在叫你计算某个区间内有多少个吉利数字. [输入]第 ...

  4. hdu3555 Bomb 数位DP入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 简单的数位DP入门题目 思路和hdu2089基本一样 直接贴代码了,代码里有详细的注释 代码: ...

  5. 数位DP入门

    HDU 2089 不要62 DESC: 问l, r范围内的没有4和相邻62的数有多少个. #include <stdio.h> #include <string.h> #inc ...

  6. 【BZOJ-1833】count数字计数 数位DP

    1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 2494  Solved: 1101[Submit][ ...

  7. HDU 2089 不要62(数位dp入门)

    题意:统计区间 [a,b] 中不含 4 和 62 的数字有多少个. 题解:这是数位DP的入门题了,首先要理解数DP的原理,DP[i][j]:代表第i位的第j值,举个栗子:如4715   数位数是从右向 ...

  8. HDU 2089 - 不要62 - [数位DP][入门题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...

  9. LightOJ 1068 Investigation (数位dp)

    problem=1068">http://www.lightoj.com/volume_showproblem.php?problem=1068 求出区间[A,B]内能被K整除且各位数 ...

随机推荐

  1. codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)

    You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the ...

  2. ACM hust 2.1

    来自咸鱼王的呻吟 http://www.xiami.com/song/3599639?spm=a1z1s.3521865.23309997.1.PbLu7E 配合咸鱼食用效果更佳(右键新窗口打开) 题 ...

  3. ACM 第十五天

    计算几何基础 练习题 C - Wasted Time Mr. Scrooge, a very busy man, decided to count the time he wastes on all ...

  4. MVC4 DropDownList (一) — 使用方法

    1.下面代码包含了三种绑定DropDownList的方法 using System; using System.Collections.Generic; using System.Linq; usin ...

  5. Linux环境PHP5.6升级7.1.8

    PHP7和HHVM比较PHP7的在真实场景的性能确实已经和HHVM相当, 在一些场景甚至超过了HHVM.HHVM的运维复杂, 是多线程模型, 这就代表着如果一个线程导致crash了, 那么整个服务就挂 ...

  6. New API

    New API Producer >增加发送回调 >重构Partition 统一High Level API与Low Level API >从kafka.consumer和kafka ...

  7. 第23天:js-数据类型转换

    一.padding1.内边距会影响盒子大小2.行内元素,尽量不用上下的padding和margin3.块元素嵌套块元素.子级会继承父级的宽度,高度由内容决定.如果给子级再设置padding,不会影响盒 ...

  8. Go语言【第十二篇】:Go数据结构之:切片(Slice)、范围(Range)、集合(Map)

    Go语言切片(Slice) Go语言切片是对数组的抽象,Go数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片("动态数组"),与数 ...

  9. CMD命令提示符

    mspaint  画图板 notepad  打开记事本 write  写字板 calc.exe  计算器 control.exe  控制面板 osk  打开屏幕键盘 rononce -p ----15 ...

  10. 【以前的空间】Poj 3071 Cut the Sequence

    dp+单调性+平衡树 在看某篇论文中看到这道题,但是那篇论文不如这个http://www.cnblogs.com/staginner/archive/2012/04/02/2429850.html 大 ...