Beautiful numbers

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做法;
   三维dp,分别是位置,当前lcm的位置,当前模数;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+10;
const int N=5e4+10;
const int M=N*10*10;
using namespace std;
inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,k,t,num[20],lc[1<<10],a[1<<10],cnt;
ll dp[20][50][2521],p,q;
void init()
{
    int i,j;
    rep(i,0,(1<<10)-1)
    {
        lc[i]=1;
        rep(j,1,9)
        {
            if((1<<j)&i)
            {
                lc[i]=lc[i]*j/gcd(lc[i],j);
            }
        }
        a[cnt++]=lc[i];
    }
    sort(a,a+cnt);
    cnt=unique(a,a+cnt)-a;
    rep(i,0,(1<<10)-1)lc[i]=lower_bound(a,a+cnt,lc[i])-a;
}
ll dfs(int pos,int x,int y,int z)
{
    int i;
    if(pos<0)return z%a[lc[y]]==0;
    if(x&&dp[pos][lc[y]][z]!=-1)return dp[pos][lc[y]][z];
    int now=x?9:num[pos];
    ll ret=0;
    rep(i,0,now)
    {
        ret+=dfs(pos-1,x||i<num[pos],y|(1<<i),(z*10+i)%2520);
    }
    return x?dp[pos][lc[y]][z]=ret:ret;
}
ll gao(ll x)
{
    int pos=0;
    while(x)num[pos++]=x%10,x/=10;
    return dfs(pos-1,0,0,0LL);
}
int main()
{
    int i,j;
    memset(dp,-1,sizeof(dp));
    init();
    scanf("%d",&t);
    while(t--)scanf("%lld%lld",&p,&q),printf("%lld\n",gao(q)-gao(p-1));
    return 0;
}
posted @ 2017-02-10 20:30  mxzf0213  阅读(227)  评论(0编辑  收藏  举报