hdu 4389

// 最大的和也就是90暴力枚举和数位dp
#include
#include
#include
using namespace std;
typedef long long LL;
int dp[10][91][91][91], bit[11];
LL  Hash[maxn];
int dfs(int len,LL mod,int sum,int k, int limt){

    if(len < 0 ) return (sum == k )&& !mod;
    if(!limt && -1 != dp[len][mod][sum][k]) return dp[len][mod][sum][k];
    int ret = 0;
    int up = limt?bit[len]:9;
    for(int i = 0;i <= up; ++ i){
        ret += dfs(len-1,(mod + i%k*Hash[len]%k)%k,sum + i ,k, limt & ( i == up));
    }
    if(!limt) dp[len][mod][sum][k] = ret;
    return ret;
}
int solve(int x){
    int len = 0;
    while(x){
        bit[len++] = x % 10;
        x /= 10;
    }
    int ret = 0;
    for(int i = 1; i<= 9*len; ++ i){
        ret += dfs(len-1,0,0,i,1);
    }
    return ret;
}
int main(){
    int cas,l,r, T = 1;
    memset(dp,-1,sizeof(dp));
    Hash[0] = 1;
    for(int i = 1; i<= 10; ++ i){
        Hash[i] = Hash[i-1] * 10 ;
    }
    scanf("%d",&cas);
    while(cas --){
        scanf("%d%d",&l,&r);
        int ans = solve(r) - solve(l - 1);
        printf("Case %d: %d\n",T++,ans);
    }
    return 0;
}

你可能感兴趣的:(dp)