题目链接:http://codeforces.com/problemset/problem/55/D
题意:求区间[L,R]有多少个Beautiful numbers。Beautiful numbers为能被组成该数字的各个数字整除的数字。
思路:由于所有1-9数字的Lcm为2520。我们用f[i][j][k]表示到达第i位,前面的数字模2520的余数为j,前面数字的Lcm为k,那么最后j%k=0即可。
i64 n,m;
int a[20],num;
int mp[2600];
i64 f[20][2600][55][2];
int cnt;
void init()
{
int i;
FOR1(i,N) if(N%i==0) mp[i]=++cnt;
}
int Gcd(int x,int y)
{
if(!y) return x;
return Gcd(y,x%y);
}
int Lcm(int x,int y)
{
if(y==0) return x;
return x*y/Gcd(x,y);
}
i64 DFS(int dep,int flag,int allZero,int mod,int lcm)
{
if(dep==-1) return mod%lcm==0&&!allZero;
if(!flag&&f[dep][mod][mp[lcm]][allZero]!=-1) return f[dep][mod][mp[lcm]][allZero];
int Max=flag?a[dep]:9;
int i;
i64 ans=0;
for(i=0;i<=Max;i++)
{
ans+=DFS(dep-1,flag&&i==Max,allZero&&i==0,(mod*10+i)%2520,Lcm(lcm,i));
}
if(!flag) f[dep][mod][mp[lcm]][allZero]=ans;
return ans;
}
i64 cal(i64 x)
{
if(x<=9) return x;
num=0;
while(x)
{
a[num++]=x%10;
x/=10;
}
return DFS(num-1,1,1,0,1);
}
int main()
{
init(); clr(f,-1);
rush()
{
scanf("%I64d%I64d",&n,&m);
printf("%I64d\n",cal(m)-cal(n-1));
}
}