CodeForces 55D Beautiful numbers 数位dp 离散化

A - Beautiful numbers
Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
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
1
1 9
Output
9
Input
1
12 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 long
using 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值