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
题意:
求L,R区间内有多少数能被自身的除了0之外的各个位整除。
代码:
//考虑到1~9的数的lcm是2520,可以dp[20][2520][2520]表示,但是内存受不了,因为0和1不用判断所以就只有7个数字需要判断是否//能整除原数因此可以 1<<8 把这七个数字压缩一下就好了。//这题还可以将最后一维离散化,因为2~9的数字随意组合的lcm只有48个,然后醉后一维就成了48,这样更快。#includeusing namespace std;typedef long long ll;const int maxn=100000;const ll mod=2520;int bit[20];ll dp[20][2600][1<<8];int check(int sum,int sta){ for(int i=2;i<=9;i++){ if((sta&(1<<(i-2)))&&(sum%i!=0)) return 0; } return 1;}ll dfs(int pos,int sum,int sta,bool limt){ if(pos==0) return check(sum,sta); if(!limt&&dp[pos][sum][sta]!=-1) return dp[pos][sum][sta]; ll ans=0; int maxb=(limt?bit[pos]:9); for(int i=0;i<=maxb;i++){ if(i<2) ans+=dfs(pos-1,(sum*10+i)%mod,sta,limt&&(i==maxb)); else ans+=dfs(pos-1,(sum*10+i)%mod,sta|(1<<(i-2)),limt&&(i==maxb)); } if(!limt) dp[pos][sum][sta]=ans; return ans;}ll solve(ll n){ int nu=0; while(n){ bit[++nu]=n%10; n/=10; } return dfs(nu,0,0,1);}int main(){ int t; ll L,R; memset(dp,-1,sizeof(dp)); scanf("%d",&t); while(t--){ scanf("%lld%lld",&L,&R); printf("%lld\n",solve(R)-solve(L-1)); } return 0;}