• [数位DP]寻找范围内没有62的有多少个数


    数位DP就是把数拆成一个一个的,然后,看 上一位如果是6的话,这一位就不能是2,这个数加上往下递减 所得到的总数就是结果。

    其中有一点就是limit,数据的范围很可能不是整数,就用一个limit,看上一位是否达到了最大值,如果达到了最大值,这一位的范围就是(0-最大值),再往下传递。

    总之三个参数,len,if6,limit。

    其中用到了一个记忆化搜索,要注意是没到极限的时候,传递结果,如果到了就不能传递了。

    起始时 limit 设为true,到达极限 。

    不要62

    Description

    杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。 
    杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。 
    不吉利的数字为所有含有4或62的号码。例如: 
    62315 73418 88914 
    都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。 
    你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。

    Input

    输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。 

    output

    对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。 

    Examples

    Input

    1 100
    0 0

    Output

    80

     

    正确解法:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string>
     4 #include<cstring>
     5 #include<map>
     6 #include<set>
     7 #include<vector>
     8 #include<queue>
     9 #include<algorithm>
    10 #include<cmath>
    11 using namespace std;
    12 typedef long long ll;
    13 const int N = 1010;
    14 int n, m;
    15 int digit[20];
    16 ll dp[20][2];
    17 ll dfs(int len, bool if6, bool limit)
    18 {
    19     if (len == 0)    return 1ll;
    20     if (!limit &&dp[len][if6])
    21         return dp[len][if6];
    22     ll cnt = 0,up=(limit ? digit[len]:9);
    23     for (int i = 0; i <= up; i++)
    24     {
    25         if (if6&&i == 2)
    26             continue;
    27         if (i == 4)    continue;
    28         cnt += dfs(len - 1, i == 6, limit&&i == up);
    29     }
    30     if (!limit)    dp[len][if6] = cnt;
    31     return cnt;
    32 }
    33 ll solve(ll num)
    34 {
    35     int k = 0;
    36     while (num)
    37     {
    38         digit[++k] = num % 10;
    39         num /= 10;
    40     }
    41     return dfs(k,false,true);
    42 }
    43 int main()
    44 {
    45     while (scanf("%d %d", &n, &m))
    46     {
    47         if (m+n == 0)    break;
    48         printf("%d
    ",solve(m)-solve(n-1));
    49     }
    50     return 0;
    51 }
    View Code
    No matter how you feel, get up , dress up , show up ,and never give up.
  • 相关阅读:
    python3--shelve 模块
    python3--常用模块
    python3 时间复杂度
    Python3 正则表达式
    python--冒泡排序
    python3--正则表达式
    python3--算法基础:二维数组转90度
    python3--算法基础:二分查找/折半查找
    python3--递归
    dedecms单独调用指定文章
  • 原文地址:https://www.cnblogs.com/Kaike/p/10422582.html
Copyright © 2020-2023  润新知