Beautiful numbers
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 l i and r i (1 ≤ l i ≤ r i ≤ 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 l i to r i, inclusively).
Examples
Input
1
1 9
Output
9
Input
1
12 15
Output
2
一个数对它所有不为0的位取模,结果都为0,找出l到r区间所有这样的数
- 数位dp,每一位求目前为止这个数的大小,和模数,模数最大可能是2520
- 然后就得到dp[20][2521][2521], 就MLE了
- 虽然1-9的任意组合的最小公倍数最大为2520,但是只有48个模数
- 先求出所有的模数,然后再离散化,用mp[2521]映射到[49],这样就不会MLE 了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long num[100];
long long dp[100][2522][50];
long long ans[1200],mp[50000];
void init()
{
long long tail = -1;
for(long long i = 1; i<(1ll<<9); i++)
{
long long num = 1;
for(long long j = 0; j<9; j++)
{
if((i>>j)&1)
num = num*(j+1)/__gcd(num,j+1);
}
ans[++tail] = num;
}
sort(ans,ans+tail+1);
tail = unique(ans,ans+tail+1)-ans;
for(long long i = 0; i< tail; i++)
{
mp[ans[i]] = i;
// printf("%lld\n",ans[i]);
}
// printf("%lld\n",tail);
}
long long dfs(long long len,long long limit,long long sum,long long mod)
{
if(len==0)
return (sum%mod)==0?1:0;
if(!limit&&dp[len][sum][mp[mod]]!=-1)
return dp[len][sum][mp[mod]];
long long ans = 0;
long long up = limit?num[len]:9;
for(long long i = 0; i<=up; i++)
{
ans += dfs(len-1,limit&&i==up,(sum*10+i)%2520,i==0?mod:(mod==0?i:mod*i/__gcd(mod,i)));
}
if(!limit)
dp[len][sum][mp[mod]]=ans;
return ans;
}
long long solve(long long n)
{
long long tail = 0;
while(n)
{
num[++tail] = n%10;
n/=10;
}
return dfs(tail,1,0,1);
}
int main()
{
init();
long long n,m,t;
scanf("%lld",&t);
memset(dp,-1,sizeof(dp));
while(t--)
{
scanf("%lld %lld",&n,&m);
printf("%lld\n",solve(m)-solve(n-1));
}
return 0;
}