模运算比较慢
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int bit[20];
LL dp[20][105][105];
int gcd(int a,int b) {return b==0?a:gcd(b,a%b);}
int lcm(int a,int b) {return a/gcd(a,b)*b;}
LL dfs(int pos,int sum,int rem,bool limit){
if(pos<=0) return sum==mod&&rem==0;
if(!limit&&dp[pos][sum][rem]!=-1)
return dp[pos][sum][rem];
LL ans=0;
int now;
int up=limit?bit[pos]:9;
for(int i=0;i<=up&&i<=mod-sum;i++){
now=(rem*10+i)%mod;
ans=ans+dfs(pos-1,sum+i,now,limit&&i==bit[pos]);
}
if(!limit) dp[pos][sum][rem]=ans;
return ans;
}
LL solve(LL x){
int len=0;
while(x){
bit[++len]=x%10;
x/=10;
}
LL ans=0;
for(int i=1;i<=9*len;i++){
memset(dp,-1,sizeof(dp));
mod=i;
ans+=dfs(len,0,0,true);
}
return ans;
}
int main(){
int T;
scanf("%d",&T);
int cas=1;
LL n;
while(T--){
scanf("%lld",&n);
printf("Case %d: %lld\n",cas++,solve(n));
}
return 0;
}