Light OJ 1140

数位dp,需要记录前导0。

数位dp中需要注意统计0,00,000……这些数字。

数位dp的写法可以分为两类。由于我们通常采用记忆化搜索的方式进行dp,所以我们有一个记忆化数组。

一种是记忆化数组的意义是不通用的,对于不同case,该数组的值不同。另一种是通用的,不同case,数组的值不变。

对于第一种情况的实现比较简单,只需要將递归过程的全部参数记录在数组的维度中。

由于要记录全部的参数,数组维度高,所以空间效率低。

由于不同case要重新计算记忆化数组,所以对于多case的评判时间效率低。

模板如下:

long long dfs(int digit, bool less, bool leading_zero, ...)
{
    if (digit < 0)
    {
        return ...;
    }
    if (memoize[digit][less][leading_zero][...] != -1)
    {
        return memoize[digit][less][leading_zero][...];
    }
    int limit = less ? 9 : f[digit];
    long long ret = 0;
    for (int i = 0; i <= limit; i++)
    {
        ret += dfs(digit - 1, less || i < f[digit], leading_zero && i==0, ...);
    }
    return memoize[digit][less][leading_zero][...] = ret;
}

对于第二种情况,则需要对某些参数进行条件判断,记忆化数组memoize[digit]中记录的是,最低的digit位可以任意取值的情况下,我们所需要的答案。

因而,这种记忆化数组自然不会受到上界的限制。

但是实现起来复杂,如果需要条件判断的变量(在递归参数中,却不在记忆化数组中的变量)过多,则会尤为复杂。

尤其是对于那种多个数字,每个数字都有上界,同时进行dp的情况,不应该使用这种方法,而应选用第一种方法。

模板如下:

long long dfs(int digit, bool less, bool leading_zero, ...)
{
    if (digit < 0)
    {
        return ...;
    }
    if (less && !leading_zero && memoize[digit][...] != -1)
    {
        return memoize[digit][...];
    }
    int limit = less ? 9 : f[digit];
    long long ret = 0;
    for (int i = 0; i <= limit; i++)
    {
        ret += dfs(digit - 1, less || i < f[digit], leading_zero && i == 0, ...);
    }
    if (less && !leading_zero)
    {
        memoize[digit][...] = ret;
    }
    return ret;
}

本题答案如下:

#include <cstdio>
#include <cstring>
using namespace std;

const int MAX_DIGIT = 20;

long long n;
int f[MAX_DIGIT];
long long memoize[MAX_DIGIT][20*20*9];
int pivot;

int to_digits(long long a)
{
    int ret = 0;
    while (a > 0)
    {
        f[ret++] = a % 10;
        a /= 10;
    }
    return ret;
}

long long dfs(int digit, bool less, bool leading_zero, int zero_num)
{
    if (digit < 0)
    {
        return zero_num;
    }
    if (less && !leading_zero && memoize[digit][zero_num] != -1)
    {
        return memoize[digit][zero_num];
    }
    int limit = less ? 9 : f[digit];
    long long ret = 0;
    for (int i = 0; i <= limit; i++)
    {
        int delta = !leading_zero && i == 0 ? 1 : 0;
        ret += dfs(digit - 1, less || i < f[digit], leading_zero && i == 0, zero_num + delta);
    }
    if (less && !leading_zero)
    {
        memoize[digit][zero_num] = ret;
    }
    return ret;
}

long long work(long long n)
{
    if (n < 0)
    {
        return 0;
    }
    if (n == 0)
    {
        return 1;
    }
    int len = to_digits(n);
    return dfs(len - 1, false, true, 0) + 1;
}

int main()
{
    int t;
    scanf("%d", &t);
    memset(memoize, -1, sizeof(memoize));
    for (int i = 1; i <= t; i++)
    {
        long long a, b;
        scanf("%lld%lld", &a, &b);
        printf("Case %d: %lld\n", i, work(b) - work(a - 1));
    }
    return 0;
}

时间: 2024-08-09 22:02:42

Light OJ 1140的相关文章

Light oj 1140 How Many Zeroes?

Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down? Input Input starts with an integer T (≤ 11000), denoting the number of test cases. Each case contains two

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i

[2016-04-21][light]OJ[1234][Harmonic Number]

时间:2016-04-21 22:18:26 星期四 题目编号:[2016-04-21][light]OJ[1234][Harmonic Number] 题目大意:求∑nk=11kn∈(1,108),精确到10?8求∑k=1n1kn∈(1,108),精确到10?8 分析: 想法是打表,然后输出,但是直接打表会爆内存 解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值 对应的整百就是n

Light OJ 1411 Rip Van Winkle`s Code 线段树成段更新

题目来源:Light OJ 1411 Rip Van Winkle`s Code 题意:3中操作 1种查询 求区间和 其中每次可以把一段区间从左到右加上1,2,3,...或者从右到左加上...3,2,1 或者把某个区间的数都置为v 思路:我是加了6个域 add是这段区间每个数都要加上add  add是这么来的 对与123456...这个等差数列 可能要分为2个区间 那么我就分成123和123 两个右边的等差数列每个数还应该加上3 所以右区间add加3 v是这个区间都要置为v 他的优先级最高 b是

Light OJ 1168 Wishing Snake 强连通缩点+哈密顿通路

题目来源:Light OJ 1168 Wishing Snake 题意:有点难看懂题意 看了一个小时再加别人的代码才懂意思 从0开始 输入的那些每一对u v 都要经过 就是从0到到达那些点 思路:首先缩点 每一个强连通分量里面的点都是可达的 缩点后的图是有向无环图 如果从0这个强连通分量可以出去到2个强连通分量 那么这两个强连通分量是不可能相互可达 所以可行的方案就是所有的强连通分量连成一线 一个一个串起来 除了第一个 出度是1入度是0和最后一个出度是0入度是1 其他都是入度等于出度是1 特判只

Jan&#39;s light oj 01--二分搜索篇

碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计数问题,利用二分直接枚举可能的结果,再检查是否符合题目要求. 4.区间求解,即利用两次二分分别查找有序序列左右上下限,再求差算出总个数. 题型知识补充: 1. 三分的一般写法: 1 double thfind(double left,double right) 2 { 3 double midmid

light oj 1422 - Halloween Costumes (区间dp)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it's Ha

Light OJ 1341 Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi

Light OJ 1114 Easily Readable 字典树

题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 只要满足每个单词的首尾字符一样 中间顺序可以变化 思路:每个单词除了首尾 中间的字符排序 然后插入字典树 记录每个单词的数量 输入一个句子 每个单词也排序之后查找 根据乘法原理 答案就是每个单词的数量之积 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm>