CodeForces 55DBeautiful numbers 数位dp 离散化

来源:互联网 发布:死亡之翼的背脊数据库 编辑:程序博客网 时间:2024/04/29 05:00
A - Beautiful numbers
Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 55D
Appoint description: 

Description

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).

Sample Input

Input
11 9
Output
9
Input
112 15
Output

2





规定一个数如果能够被自己每位非零的数整除则称漂亮数

求区间a到b的漂亮数的个数

因为个位数只能是1~9所以只要判断是否是1~9的lcm的因子就好了

lcm = 2520 20*2520*2520会超空间

考虑到2520内就48个因子所以进行离散化


ACcode:

#include <cstdio>#include <cstring>#include <iostream>#define maxn 3000#define mod 2520#define ll long longusing namespace std;ll dp[21][maxn][50];int data[21];int hash[maxn];int gcd(int a,int b){return !b?a:gcd(b,a%b);}int lcm(int  a,int b){return a/gcd(a,b)*b;}void has(){    int cnt=0;    for(int i=1;i<=mod;i++){        if(mod%i==0){            hash[i]=++cnt;        }    }}ll dfs(int pos,int pre_mod,int pre_lcm,bool limit){    if(!pos)return pre_mod%pre_lcm==0;    if(!limit&&dp[pos][pre_mod][hash[pre_lcm]]!=-1)return dp[pos][pre_mod][hash[pre_lcm]];    int ed=limit?data[pos]:9;    ll ans=0;    for(int d=0;d<=ed;d++)        ans+=dfs(pos-1,(pre_mod*10+d)%mod,lcm(pre_lcm,d?d:1),limit&&d==ed);    return limit?ans:dp[pos][pre_mod][hash[pre_lcm]]=ans;}ll fun(ll a){    int len=0;    while(a){        data[++len]=a%10;        a/=10;    }    return dfs(len,0,1,1);}void doit(){    ll a,b;    scanf("%I64d%I64d",&a,&b);    printf("%I64d\n",fun(b)-fun(a-1));}int main(){    has();    int loop;memset(dp,-1,sizeof(dp));    scanf("%d",&loop);    while(loop--)doit();}


0 0
原创粉丝点击