zoukankan      html  css  js  c++  java
  • 题解 P1179 【数字统计】

    嚯嚯嚯,这道题很显然是削弱版的51nod P1042

    那么显然我们需要使用数位DP解题。


    思路大致是这样的:

    对于每一个数字,考虑三种影响关系:

    1. 它对低位的影响

    2. 它对高位的影响

    3. 高位对低位的影响


    然后在递归中实现这三种关系的计算即可。


    AC代码:

     1 #include <stdio.h>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 #define CLR(a,b) memset(a,b,sizeof(a))
     6 #define INF 0x3f3f3f3f
     7 #define LL long long
     8 void dp(LL n , LL m , LL *c)
     9 {
    10     LL x = n % 10;
    11     LL y = n / 10;
    12     for (int i = 0 ; i <= x ; i++)
    13         c[i] += m;        //当前位对低位的影响,每个都是0~9的范围 
    14     for (int i = 0 ; i <= 9 ; i++)
    15         c[i] += m * y;        //当前位对的高位影响(高位不为0,在上一个循环中算过) 
    16     c[0] -= m;        //排除前导0的情况
    17     LL t = y;
    18     while (t)        //高位对低位的影响,即都为最大限制数 
    19     {
    20         c[t % 10] += (x+1) * m;        //算上0
    21         t /= 10; 
    22     }
    23     if (y)
    24         dp(y-1 , m*10 , c);        //y值在上个while中算过,算下一个未限制数 
    25 }
    26 int main()
    27 {
    28     LL r,l;
    29     LL a[10] = {0};
    30     LL b[10] = {0};
    31     scanf ("%lld %lld",&l,&r);
    32     dp(r , 1 , a);
    33     dp(l-1 , 1 , b);
    34     printf("%lld
    ",a[2]-b[2]);
    35     return 0;
    36 }
  • 相关阅读:
    数据结构算法(3)--排序
    数据结构算法(2)--字符串匹配
    数据结构与算法(0)-四则运算
    数据结构算法(1)--递归转化
    高级软件工程实践总结
    beta冲刺随笔集合
    Beta冲刺-用户测试报告
    Beta冲刺总结
    SDN期末作业-负载均衡的实现
    SDN第六次作业
  • 原文地址:https://www.cnblogs.com/ilverene/p/9816811.html
Copyright © 2011-2022 走看看