Description
10 19
3
【约束条件】1 ≤ a ≤ b ≤ 10^18
这个题因为在搜索的过程中数位的数字和一直在变,导致mod也在变。
所以我们要遍历并固定mod,找出对于每一个mod,使得各位数字和刚好等于mod且能整除原数的数的个数
dp[ i ][ j ][ k ] 表示第 i 位为止得到的数的各位数字之和模 mod 等于 (mod - j),得到的数模 mod 等于 k
然后去dp
代码:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
#define fi first
#define se second
LL a,b;
LL dp[20][200][200],mod;
LL n[20];
LL dfs(LL pos,LL sum,bool limit,LL num)
{
if(pos==-1)
{
return (sum==mod&&num==0)?1:0;
}
if(dp[pos][mod-sum][num]>-1&&!limit)return dp[pos][mod-sum][num];
LL res=0;
LL up=min(mod-sum,limit?n[pos]:9);
for(LL i=max(mod-sum-9*pos,0LL);i<=up;i++)
{
res+=dfs(pos-1,sum+i,limit&&i==up,(num*10+i)%mod);
}
if(!limit)dp[pos][mod-sum][num]=res;
return res;
}
LL solve(LL z)
{
LL len=0;
while(z)n[len++]=z%10,z/=10;
LL res=0;
for(mod=1;mod<=9*len;mod++) { memset(dp,-1,sizeof(dp));res+=dfs(len-1,0,true,0);}
return res;
}
int main()
{
memset(dp,-1,sizeof(dp));
while(scanf("%lld%lld",&a,&b)!=-1)
{
a--;
printf("%lld\n",solve(b)-solve(a));
}
return 0;
}
本人蒟蒻,如有错误,还望指正