Description
给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数。
Input
Output
Sample Input
10 19
Sample Output
3
HINT
【约束条件】1 ≤ a ≤ b ≤ 10^18
Source
分析:数位DP,枚举一下所有数字之和。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<ctime>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXN 100000
using namespace std;
typedef long long ll;
ll l,r,a[22],dp[22][172][172];
ll dfs(int pos,int p,int sta1,int sta2,int limit)
{
if(pos == -1) return (sta1 == 0 && sta2 == p);
if(sta2 > p || (pos+1)*9+sta2 < p) return 0;
if(!limit && dp[pos][sta1][sta2] >= 0) return dp[pos][sta1][sta2];
int up = limit? a[pos] : 9;
ll ans = 0;
for(int i = 0;i <= up;i++)
{
ans += dfs(pos-1,p,(sta1*10+i)%p,sta2+i,limit && i == up);
}
if(!limit) dp[pos][sta1][sta2] = ans;
return ans;
}
ll solve(ll x)
{
int pos = 0;
while(x)
{
a[pos++] = x % 10;
x /= 10;
}
if(!pos) return 0ll;
ll ans = 0;
for(int i = 1;i <= pos*9;i++)
{
memset(dp,-1,sizeof(dp));
ans += dfs(pos-1,i,0,0,1);
}
return ans;
}
int main()
{
cin>>l>>r;
cout<<solve(r)-solve(l-1)<<endl;
}