题目描述
NIBGNAUK is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by the sum of its digits.
We will not argue with this and just count the quantity of beautiful numbers from 1 to N.
输入描述:
The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve. Each test case contains a line with a positive integer N (1 ≤ N ≤ 1012).
输出描述:
For each test case, print the case number and the quantity of beautiful numbers in [1, N].
示例1
输入
2 10 18
输出
Case 1: 10 Case 2: 12
思路:数位DP,做的太少,码上,还需理解。
#include<bits/stdc++.h>
using namespace std;
long long n;
int a[20],mod;
long long dp[20][200][200];
long long dfs(int pos,int state,int tmod,bool limit){
if(!pos) return (state==mod)&&(!tmod);
if(state>mod) return 0;
if(!limit&&dp[pos][state][tmod]!=-1) return dp[pos][state][tmod];
int up=limit?a[pos]:9;
long long cnt=0;
for(int i=0;i<=up;i++) cnt+=dfs(pos-1,state+i,(tmod*10+i)%mod,limit&&i==a[pos]);
return limit?cnt:dp[pos][state][tmod]=cnt;
}
long long cal(long long n){
int len=0;
long long nn=n;
while(nn){
a[++len]=nn%10;
nn/=10;
}
long long ans=0;
for(int i=1;i<=9*len;i++){
mod=i;
memset(dp,-1,sizeof(dp));
ans+=dfs(len,0,0,true);
}
return ans;
}
int main(){
int t,cas=0;
scanf("%d",&t);
while(t--){
scanf("%lld",&n);
printf("Case %d: %lld\n",++cas,cal(n));
}
}