【UVA11361】Investigating Div-Sum Property——數位DP

來源:互聯網
上載者:User

An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is divisible
by 3 and 12(3+7+0+2) is also divisible by 3. This property also holds for the integer 9.
In this problem, we will investigate this property for other integers.

Input

The first line of input is an integer T (T < 100) 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 is divisible by K and the sum
of its digits is also divisible by K.

Sample Input

3
1 20 1
1 20 2
1 1000 4

Sample Output

20
5
64

題意:計算區間 [A,B]之間有多少數滿足是k的倍數,並且各位元之和也是k的倍數 [A,B]之間有多少數滿足是k的倍數,並且各位元之和也是k的倍數

分析:數位Dp的入門題,一般數位Dp[x]表示在不超過x的數中滿足條件的輸的個數,那麼本題的答案就是Dp[B] -Dp[A-1],現在的任務就是如何求出Dp[x],假設x = 3234,那麼我們可以將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]。由於各區間之間是相互獨立的,滿足加法定理,所以我們Dp[i][j][s],表示i位時,對k取餘為j,各位元之和對k取餘為s的數量,所以可以枚舉第i+1上的數x則Dp[i+1][(j*10+x)%k][(s+x)%k]+=Dp[i][j][s],對於區間我們從高位到低位枚舉,處理區間的限制。

#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));        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.