Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.
The first line of the input contains the number of cases t (1 ≤ t ≤ 10). Each of the next t lines contains two natural numbers li and ri (1 ≤ li ≤ ri ≤ 9 ·1018).
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
Output should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).
1 1 9
9
1 12 15
2
一个美丽数就是可以被它的每一位的数字整除的数。
给定一个区间,求美丽数的个数。
巧妙的用lcm乱搞。。。
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
const int mod=2520;
ll dp[30][2520+10][50];
int index[2520+10],bit[30];
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b)
{
return a*b/gcd(a,b);
}
ll dfs(int len,int psum,int plcm,bool e)
{
if(len==0)
return psum%plcm==0;
if(!e&&dp[len][psum][index[plcm]]!=-1)
return dp[len][psum][index[plcm]];
int n=e?bit[len]:9;
ll ans=0;
for(int i=0;i<=n;i++)
{
int nsum=(psum*10+i)%mod,nlcm=plcm;
if(i)
nlcm=lcm(i,nlcm);
ans+=dfs(len-1,nsum,nlcm,e&&i==n);
}
return e?ans:dp[len][psum][index[plcm]]=ans;
}
int tail;
void cg(ll x)
{
tail=0;
while(x)
{
bit[++tail]=x%10;
x/=10;
}
}
ll work(ll x)
{
cg(x);
return dfs(tail,0,1,1);
}
int main()
{
memset(dp,-1,sizeof(dp));
int tail=0;
for(int i=1;i<=mod;i++)
if(mod%i==0)
index[i]=tail++;
int T;
cin>>T;
while(T--)
{
ll l,r;
cin>>l>>r;
cout<<work(r)-work(l-1)<<endl;
}
}