# 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers

#include<bits/stdc++.h>
//CLOCKS_PER_SEC
#define se second
#define fi first
#define ll long long
#define Pii pair<int,int>
#define Pli pair<ll,int>
#define ull unsigned long long
#define pb push_back
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const double Pi=3.14159265;
const int N=1e4+10;
const ull base=163;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
using namespace std;
ll dp[20][200][200];int a[20];
int o=0;
ll dfs(int pos,int sta1,int sta2,bool lead,bool limit){
if(pos==-1){
return sta1==0&&sta2==0;
}
if(!limit&&!lead&&dp[pos][sta1][sta2]!=-1)return dp[pos][sta1][sta2];
int up=limit?a[pos]:9;
ll ans=0;
for(int i=0;i<=up&&sta1-i>=0;i++){
ans+=dfs(pos-1,sta1-i,(sta2*10+i)%o,lead&&i==0,limit&&i==a[pos]);
}
if(!limit&&!lead)dp[pos][sta1][sta2]=ans;
return ans;
}
ll solve(ll x){
int pos=0;
while(x){a[pos++]=x%10;x/=10;}
int up=9*pos;ll ans=0;
for(int i=1;i<=up;i++){
memset(dp,-1,sizeof(dp));
o=i;
ans+=dfs(pos-1,i,0,1,1);
}
return ans;
}
int main(){
int T;scanf("%d",&T);int tt=1;
while(T--){
ll x;scanf("%lld",&x);
printf("Case %d: %lld\n",tt++,solve(x));
}
return 0;
}

posted @ 2018-08-05 20:05  采蘑菇的小西佬  阅读(171)  评论(0编辑  收藏