HDOJ 4389——デジタルDP

4673 ワード

X mod f(x)
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1432    Accepted Submission(s): 613
Problem Description
Here is a function f(x):
   int f ( int x ) {
       if ( x == 0 ) return 0;
       return f ( x / 10 ) + x % 10;
   }

   Now, you want to know, in a given interval [A, B] (1 <= A <= B <= 10
9), how many integer x that mod f(x) equal to 0.
 
Input
   The first line has an integer T (1 <= T <= 50), indicate the number of test cases.
   Each test case has two integers A, B.
 
Output
   For each test case, output only one line containing the case number and an integer indicated the number of x.
 
Sample Input

   
   
   
   
2 1 10 11 20

 
Sample Output

   
   
   
   
Case 1: 10 Case 2: 3

 
Author
WHU
 
Source
2012 Multi-University Training Contest 9
 
Recommend
zhuyuanchen520
 
デジタルDPはやはり書きにくいので、考え方の参考にしてください.http://blog.csdn.net/tclh123/article/details/7905994
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
#define bug(s) cout<<"#s = "<< s << endl;
const int MAXN = 2000 + 50;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
//#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")
int tens[11] , dp[11][85][85][85];///d[  ][   ][  ][  ]
int bit[15];

void DP()
{
    clr(dp , 0);
    FORR(sum , 0 , 9)FORR(mod , 1 , 81)
    {
        dp[1][sum][mod][sum % mod]++;
    }
    FORR(len , 1 , 8)FORR(sum , 0 , 81)FORR(mod , 1 , 81)FORR(res ,0  ,80)FORR(x , 0 , 9)
    {
        if(sum + x > 81)break;
        dp[len + 1][sum + x][mod][(res * 10 + x) % mod] += dp[len][sum][mod][res];
    }
}
void init()
{
    tens[0] = 1;
    FORR(i , 1,  9)
    {
        tens[i] = tens[i - 1] * 10;
    }
    DP();
}
int cal(int n)
{
    if(n == 0)return 0;
    int S = 0;
    int num = 0;
    for(int t = n ; t ;  t /= 10)
    {
        bit[++num] = t % 10;
        S += bit[num];
    }
    int cnt = 0;
    FORR(mod , 1 , 9 * num)
    {
        if(mod > 81)break;
        if(mod > n)break;
        int pre = 0;
        int sum = 0;
        REPP(i , num , 2)
        {
            int len = i - 1;
            FORR(j , 0 , bit[i] - 1)
            {
                if(mod - sum - j < 0)break;
                FORR(res , 0 , mod - 1)
                {
                    if((pre + j * tens[len] + res) % mod == 0)
                    {
                        cnt += dp[len][mod - sum - j][mod][res];
                    }
                }
            }
            sum += bit[i];
            if(sum > mod)break;
            pre += bit[i] * tens[len];
        }

    }
    for(int t = n ; ;t-- , S--)
    {
        if(t % S == 0)cnt++;
        if(t % 10 == 0)break;
    }
    return cnt;
}

int main()
{
    //ios::sync_with_stdio(false);
    #ifdef Online_Judge
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif // Online_Judge
    int t;
    cin >> t;
    init();
    FORR(kase , 1 , t)
    {
        int a , b;
        scanf("%d%d" , &a ,&b);
        printf("Case %d: %d
" , kase , cal(b) - cal(a - 1)); } return 0; }