51nod 1042 number of digits 0-9 digits DP

Source: Internet
Author: User

Title Link: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1042

Title: Give an interval of a-B to count the number of occurrences within this interval of 0-9. For example, 10-19,1 appears 11 times (10,11,12,13,14,15,16,17,18,19, of which 11 includes 2 1), and the remaining numbers appear 1 times. Input

Two number A, a (1 <= a <= b <= 10^18)
Output
Output total 10 rows, respectively, 0-9 occurrences


This problem feels very troublesome, tangled for a long time.

We use recursion to consider each one. Into the already calculated low, current bit, not yet calculated high.

There are only a few cases when dealing with the current bit:

1, the current position and low-level relationship

2, the relationship between high and low

3, the current relationship with the high

Three cases can be processed. Note that 0 of special cases, such as 01 in fact, do not have 0.

#include <iostream>
#include <bits/stdc++.h>
#define LL Long long
using namespace std;

void Dfs (LL a,ll b,ll c[])
{
    ll n=a/10,m=a%10,t=n;
    for (int i=0;i<=m;i++)   c[i]+=b;//Current bit-to-low effect for
    (int i=0;i<10;i++)   c[i]+=b*n;//High-to-low effect
    c[0] -=b;//0 special treatment, the number of 0 minus while
    (t)//The current bit of the effect of the high
        (c[t%10]+=b* (m+1);//Plus 0
        t/=10;
    }
    if (n)   dfs (N-1,B*10,C);//n has been processed, so to process n-1
}

ll x[20],y[20];

int main ()
{
    ll A, B;
    cin>>a>>b;
    DFS (a-1,1,x);
    DFS (b,1,y);
    for (int i=0;i<10;i++)   cout<<y[i]-x[i]<<endl;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.