Description
给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数。
Sample Input
10 19
Sample Output
3
HINT
1 ≤ a ≤ b ≤ 10^18
dp[i][j][k] 表示 第 i 位 数位和为 j 并且 取余后为 k 的符合条件的数字个数
枚举数字位的和,从1~162(max),这样就可以直接判断了
/*************************************************************************
> Author: wzw-cnyali
> Created Time: 2017/9/13 17:04:14
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#prag\
ma GCC optimize("O3")
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define REP(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i)
#define DREP(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i)
#define EREP(i, a) for(register int i = (be[a]); i != -1; i = nxt[i])
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mem(a, b) memset((a), b, sizeof(a))
template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template <class T>
T read(T sum = 0, T fg = 0)
{
char c = getchar();
while(c < '0' || c > '9') { fg |= c == '-'; c = getchar(); }
while(c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); }
return fg ? -sum : sum;
}
const int Size = 21;
LL dp[Size][163][163];
LL dfs(int *a, int pos, int sum1, int sum2, int k, int lim)
{
if(sum1 < 0) return 0;
if(pos == -1) return sum1 == 0 && sum2 == 0;
if(!lim && dp[pos][sum1][sum2] != -1) return dp[pos][sum1][sum2];
LL res = 0;
REP(i, 0, lim ? a[pos] : 9)
{
res += dfs(a, pos - 1, sum1 - i, (sum2 * 10 + i) % k, k, lim && i == a[pos]);
}
if(!lim) dp[pos][sum1][sum2] = res;
return res;
}
int a[Size], alen;
int b[Size], blen;
LL solve(LL L, LL R)
{
alen = 0; do { a[alen++] = L % 10; L /= 10; }while(L);
blen = 0; do { b[blen++] = R % 10; R /= 10; }while(R);
LL res = 0;
REP(i, 1, max(alen, blen) * 9)
{
mem(dp, -1);
res += dfs(b, blen - 1, i, 0, i, 1) - dfs(a, alen - 1, i, 0, i, 1);
}
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
LL L = read<LL>(), R = read<LL>();
printf("%lld\n", solve(L - 1, R));
return 0;
}