D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output
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.
Input
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
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).
Examples input
1 1 9
output
9
input
1 12 15
output
2
/*
求区间内有多少个数,能被它的每一位非0数字整除
非常有趣的数位dp,一个数能够被它每一位非0数字整除,意味着能整除这些数的lcm
lcm可以在递归的时候直接求出来,问题在于怎么知道当前数是否被每一个非零位整除
因为1-9的最小公倍数为2520,例如一个数x,每一个非零位的lcm为y.
若x=z*y,因为2520=k*y,那么x%2520也一定是y的倍数。
另外如果直接开dp[20][2520][2520]会MLE,所以记录所有的最小公倍数离散化即可。
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
#define LL long long
const int MOD = 2520;
LL dp[20][50][2550];
int dis[20];
int Hash[2550];
LL gcd(LL a, LL b){
return b?gcd(b,a%b):a;
}
LL dfs(int len, int num, int lcm, bool flag){
if(-1==len) return num%lcm == 0;
if(!flag && ~dp[len][Hash[lcm]][num]) return dp[len][Hash[lcm]][num];
LL ans = 0;
int end = flag?dis[len]:9;
for(int i=0; i<=end; i++)
ans += dfs(len-1, (num*10+i)%MOD, i?lcm*i/gcd(lcm,i):lcm, flag&&i==end);
if(!flag) dp[len][Hash[lcm]][num] = ans;
return ans;
}
LL solve(LL n){
int pos = 0;
while(n){
dis[pos++] = n%10;
n /= 10;
}
return dfs(pos-1, 0, 1, 1);
}
int main(){
int T;
scanf("%d", &T);
int cnt = 0;
for(int i=1; i<=MOD; i++)
if(MOD%i == 0)
Hash[i] = cnt++;
memset(dp, -1, sizeof(dp));
while(T--){
long long l, r;
scanf("%lld%lld", &l, &r);
printf("%lld\n", solve(r)-solve(l-1));
}
return 0;
}