1588:数字游戏

1588:数字游戏

/*
reference:
    
Date:
    2019.10.08
sol:
    
*/
#include
using namespace std;
#define int long long
template inline void rd(T &x){x=0;char c=getchar();int f=0;while(!isdigit(c)){f|=c=='-';c=getchar();}while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x=f?-x:x;}
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dwn(i,a,b) for(int i=(a);i>=(b);--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define ee(i,u) for(int i=head[u];i;i=e[i].next)

int l,r,len,mod;
int dp[10+5][100+10];
int bit[10+5];

inline int dfs(int pos,int sum,int limit){
    if(pos>len)return sum==0;
    if(dp[pos][sum]!=-1 && !limit)return dp[pos][sum];
    int up=limit?bit[len-pos+1]:9;
    int res=0;
    rep(i,0,up){
        res+=dfs(pos+1,(sum+i)%mod,i==up && limit);
    }
    if(!limit)dp[pos][sum]=res;
    return res;
}

inline int work(int x){
    mem(dp,-1);
    len=0;
    while(x){
        bit[++len]=x%10;
        x/=10;
    }
    return dfs(1,0,1);
} 

#undef int 
int main(){
#define int long long
    #ifdef WIN32 
    freopen("test.txt","r",stdin);
    #endif
    while(~scanf("%lld%lld%lld",&l,&r,&mod))
        printf("%lld\n",work(r)-work(l-1));
    return 0;
}

你可能感兴趣的:(1588:数字游戏)