"UVA11361" investigating div-sum property--digital DP

Source: Internet
Author: User

An integer was divisible by 3 if the sum of it digits is also divisible by 3. For example, 3702 is divisible
by 3 and (3+7+0+2) are also divisible by 3. The also holds for the integer 9.
In this problem, we'll investigate this property for other integers.

Input

The first line of input was an integer t (T < a) that indicates the number of test cases. Each case is
A line containing 3 positive integers a, B and K. 1≤a≤b < 231 2^{31} and 0 < K < 10000.

Output

For each case, output the number of integers in the range [A, B] which was divisible by K and the sum
of of its digits are also divisible by K.

Sample Input

3
1 20 1
1 20 2
1 1000 4

Sample Output

20
5
64

Test instructions: The calculation interval [a, b] between the number of satisfied is a multiple of k, and the sum of the number of K is also a multiple of [a, a] between the number of K is satisfied is a multiple, and the sum of the numbers is also a multiple of K

Analysis: Digital Dp of the entry, the General digital Dp[x] for the number of not more than X to meet the conditions of the number of losses, then the answer to the question is dp[b]-dp[a-1], now the task is how to find out dp[x], assuming x = 3234, then we can decompose X [0,999] , [1000,1999],[2000,2999],[3000,3099],[3100,3199],[3200,3209],[3210,3219],[3220,3229],[3230,3231],[3231,3232],[ 3232,3233],[3233,3234]. Since each interval is independent of each other, satisfies the addition theorem, so we dp[i][j][s], said I bit, the K to take the remainder of J, the sum of the number of K and the remainder of the number of S, so you can enumerate the number on the i+1 x is dp[i+1][(j*10+x)%k][(s+x)%k]+= Dp[i][j][s], for the interval we enumerate from high to low, processing interval limits.

#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <stack>

#include <string> #include <algorithm> #include <iostream> using namespace std;

typedef long Long LL;

LL dp[11][110][110];

int po[11];

    void Break (LL N) {po[0] = 0;

    stack<int>st;

        while (n) {st.push (n%10);
    n/= 10; } while (!

        St.empty ()) {po[++po[0]] = St.top ();
    St.pop ();

    }} LL Opera (ll N, ll m) {memset (dp,0,sizeof (Dp));

    Break (n);

    LL ans =0, ant = 0;
            for (int i = 1;i<=po[0];i++) {for (int j = 0;j<m;j++) {for (int k = 0;k<m;k++) {for (int s = 0;s<=9;s++) {dp[i][(j+s)%m][(k*10+s)%M]+=DP [I-1] [j]
                [K];
        }}} for (int j = 0;j<po[i];j++) {dp[i][(ans+j)%m][(ant*10+j)%m]++;

   }     Ans = (ans+po[i])%m;
    Ant = (ant*10+po[i])%m;
    } if (ans==0&&ant==0) {dp[po[0]][0][0]++;

} return dp[po[0]][0][0];

    } int main () {LL n,m,k;

    int T;

    scanf ("%d", &t);

        while (t--) {scanf ("%lld%lld%lld", &n,&m,&k);
        if (k>=100) {printf ("0\n");
        } else {printf ("%lld\n", Opera (m,k)-opera (n-1,k));

 }
    }

}

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.