51nod 1042 Number 0-9 (digital DP)

Source: Internet
Author: User
Title Description

Give an interval of a-B to count the number of occurrences in 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 a total of 10 rows, respectively, 0-9 occurrences of the input example

10 19
Output Example
1
1
1
1
1
1
1
1
1
Thinking of solving problems

Solution One:
Considering the feasible value of each digit from low to High, dp[i] represents the number of feasible values for the current number of digits. Take 235678 as an example, assuming that the now=5 is currently traversed, this high=23,after=678,tmp=1000:
1. When I < now, the high value range is [0,23], then dp[i]+= (high+1) *tmp;
2. When I > now, the high value range is [0,22], then dp[i]+=high*tmp;
3. When i = Now, corresponds to whether the high value is equal to two cases. When the high =high, the low value can not exceed after, that is, there are after+1 (plus a low value of 0 this case); When the high value is [0,22], the corresponding case is high* tmp. Then dp[i]+=high*tmp+after+1.
PS: When i=0, it is necessary to subtract the high value of 0, that is, dp[0]-=tmp.

Solution Two:
Implemented in recursive mode, Dp[i] represents the number of possible values for the current number of digits. Also take 235678 as an example, assuming that the current traversal to 5 this digit, because in the recursive process, 235*** this situation in the previous layer has been calculated, so the current layer x=234, corresponding Now=4,before =23=t, tmp=1000.
1. Consider that the value range of the _,# position is [0,x], the dp[0~now]+=tmp, and for the high 2, 3, corresponds to appear (now+1) *tmp times, namely dp[2,3]+= (now+1) *tmp;
2. Consider _ _ I _ _ _ (i∈[0,9]), high-level corresponding to the value [0,22], then dp[i]+=before*tmp. Also need to subtract i=0 and high value is also 0 of the case. Code Implementation

Solution One

#include <bits/stdc++.h>
using namespace std;
#define IO Ios::sync_with_stdio (false); \
cin.tie (0); \
cout.tie (0)
; typedef long long LL;
ll num[10]= {0};
void Solve (ll x,int mark)
{
    ll tmp=1,after=0,high,now;
    while (x)
    {
        now=x%10;
        HIGH=X/10;
        for (int i=0; i<=9; i++)
        {
            if (now<i)
                num[i]=num[i]+mark*high*tmp;
            else if (now>i)
                num[i]=num[i]+mark* (high+1) *tmp;
            else
                num[i]=num[i]+mark* (high*tmp+after+1);
        }
        num[0]=num[0]-mark*tmp;
        x/=10;
        after+=now*tmp;
        tmp*=10;
    }
}
int main ()
{
    IO;
    ll A, B;
    cin>>a>>b;
    Solve (b,1);
    Solve (a-1,-1);
    for (int i=0; i<10; i++)
        cout<<num[i]<<endl;
    return 0;
}

Solution Two:

 #include <bits/stdc++.h> using namespace std; #define IO Ios::sync_with_stdio (false); \
Cin.tie (0); \ cout.tie (0);
typedef long Long LL;

ll num[10]= {0};
    void Solve (ll x,ll tmp,int Mark) {ll now=x%10,before=x/10,t=before;
    for (int i=0;i<=now;i++) num[i]+=mark*tmp;
    for (int i=0;i<10;i++) num[i]+=mark*before*tmp;
    num[0]-=mark*tmp;
        while (t) {num[t%10]+=mark*tmp* (now+1);
    t/=10;
} if (before) solve (Before-1,tmp*10,mark);
    } int main () {IO;
    ll A, B;
    cin>>a>>b;
    Solve (b,1,1);
    Solve (a-1,1,-1);
    for (int i=0; i<10; i++) cout<<num[i]<<endl;
return 0; }

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.