链接:https://www.nowcoder.com/acm/contest/163/J
来源:牛客网
时间限制:C/C++ 8秒,其他语言16秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
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>
#define ll long long
using namespace std;
int n, shu[20];
ll dp[20][163][163][2];
ll dfs(int len, int sum, int val, int mod, bool limite)
{
if (len == 0)return sum == 0 && val == 0;
if (dp[len][sum][val][limite]!=-1)return dp[len][sum][val][limite];
ll cnt = 0;
int maxx = (limite ? shu[len] : 9);
for (int i = 0; i <= maxx; i++)
{
if(sum-i<0)break;
cnt += dfs(len-1, sum-i, (val*10+i)%mod, mod, limite && i==shu[len]);
}
dp[len][sum][val][limite] = cnt;
return cnt;
}
ll solve(ll x)
{
ll ans = 0;
int pos = 0;
while(x)
{
shu[++pos] = x%10;
x/=10;
}
for(int i=1;i<=9*pos;i++)
{
memset(dp,-1,sizeof(dp));
ans += dfs(pos,i,0,i,1);
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
int Case = 1;
while(t--)
{
ll r;
scanf("%lld",&r);
printf("Case %d: ",Case++);
printf("%lld\n",solve(r));
}
return 0;
}