题意:一个数字,如果它能被所有的非零数字整除,就为 Beautiful numbers。
参考题解:点击打开链接
dp[位数][高位数字模2520的余数][最小公倍数]
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef __int64 LL;
LL c[2550],g[2550][10],dp[20][2550][100],num[20],top;
LL gcd(LL a,LL b){
return b ? gcd(b,a%b) : a;
}
void init(){
LL i,j,r;
memset(dp,-1,sizeof(dp));
for(i = 1; i <= 2520;i++){
for(j = 0;j < 10;j++)
g[i][j] = j ? i*j / gcd(i,j) : i;
}
for(i=1,r=0;i<=2520;i++){
if(2520 % i == 0) c[i] = r++;
}
}
LL dfs(LL i,LL s,LL m,bool e){
if(i == -1) return s%m ? 0 : 1;
if(!e && dp[i][s][c[m]] != -1) return dp[i][s][c[m]];
LL res = 0;
LL u = e ? num[i] : 9 , d;
for(d = 0 ; d <= u ; d++){
res += dfs(i-1,(s*10+d)%2520,g[m][d],e && d == u);
}
return e ? res : dp[i][s][c[m]] = res;
}
LL get(LL n){
top = 0;
for(;n;n/=10) num[top++] = n%10;
return dfs(top-1,0,1,1);
}
int main(){
init();
LL cas,l,r;
scanf("%I64d",&cas);
while(cas--){
scanf("%I64d%I64d",&l,&r);
printf("%I64d\n",get(r) - get(l-1));
}
}