A - Beautiful numbers(CF 55D)

A - 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 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

分析:

刚学dp,这dp搞我一天了,不过还真有意思的,里面又悟出来一点东西,dp方程要敢想

状态:

d p [ p o s ] [ P r e S u m ] [ P r e L c m ] dp[pos][PreSum][PreLcm] dp[pos][PreSum][PreLcm] 是值pre*****(pos个*号,*号代表这个数随便取。这个数的前缀是pre),这个dp代表前缀为PreSum且前缀的lcm为PreLcm后pos个数满足条件的个数是多少。

举个例子:520***就是PreSum为520,PreLcm为10,其对应的dp方程为 d p [ 3 ] [ 520 ] [ 10 ] dp[3][520][10] dp[3][520][10]

故dp方程为:

d p [ p o s + 1 ] [ P r e S u m ] [ P r e L c m ] = ∑ i = = 0 9 d p [ p o s ] [ P r e S u m ∗ 10 + i ] [ L C M ( P r e L c m , i ) ] dp[pos+1][PreSum][PreLcm]=\sum_{i==0}^{9}dp[pos][PreSum*10+i][LCM(PreLcm,i)] dp[pos+1][PreSum][PreLcm]=i==09dp[pos][PreSum10+i][LCM(PreLcm,i)]

当pos==0时候(递归边界)

若满足PreSum%PreLcm==0就返回1,否则返回0

这个需要注意两个点:

  1. 第三个参数最大为2520,但是真正能取的都是2520的因数(2520从哪来的呢,2520是LCM(1,2,3…9)),也就那48个,所以可以将第三个参数映射一下(相当于离散化),减少空间
  2. 第二个参数会很大吧,但是第三个参数是来干什么的?不是最后的时候取余第三个参数的吗!,又因为2520是所有可能第三个参数的倍数,故 P r e S u m % P r e L c m = = 0 PreSum\%PreLcm==0 PreSum%PreLcm==0等效于 P r e S u m % 2520 % P r e L c m = = 0 PreSum\%2520\%PreLcm==0 PreSum%2520%PreLcm==0(当然取余后的值可不相等),所以可以让第二个参数取余2520,控制范围,减少空间

详情看代码

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<cmath>
#include<vector>
#include<cstring>
#include<string>
#include<iostream>
#include<iomanip>
#define mset(a,b)   memset(a,b,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=1e5+10;
const int branch=26;
const int inf=0x3f;
const int MOD=2520;
ll dp[30][MOD+10][50];
int index[MOD+10];
int num[30];
void init()
{
    mset(dp,-1);
    int tot=0;
    /*将MOD仅有的因子离散化 每个值对应于一个下标*/
    for(int i=1;i<=MOD;++i)
        if(MOD%i==0)
            index[i]=++tot;
}
int Gcd(int a,int b)
{
    int temp;
    while(a%b)
    {
        temp=a%b;
        a=b;
        b=temp;
    }
    return b;
}
int Lcm(int a,int b)
{
    return a/Gcd(a,b)*b;
}
ll dfs(int pos,int pre_sum,int pre_lcm,bool is_top);
ll calc(ll val)
{
    int pos=0;
    do{
        num[++pos]=val%10;
        val/=10;
    }
    while(val);//位数从下标1开始存
    return dfs(pos,0,1,true);
}
ll dfs(int pos,int pre_sum,int pre_lcm,bool is_top)
{
    if(!pos)
        return pre_sum%pre_lcm==0;
    if(!is_top&&dp[pos][pre_sum][index[pre_lcm]]!=-1)
        return dp[pos][pre_sum][index[pre_lcm]];
    int endd=is_top?num[pos]:9;
    ll res=0ll;
    for(int i=0;i<=endd;++i)
    {
        int lcm,sum;
        lcm=pre_lcm;
        if(i)
            lcm=Lcm(lcm,i);
        sum=(pre_sum*10+i)%MOD;
        res+=dfs(pos-1,sum,lcm,is_top&&i==endd);
    }
    if(!is_top)
        dp[pos][pre_sum][index[pre_lcm]]=res;
    return res;
}
int main()
{
    int t;
    ll a,b;
    init();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld %lld",&a,&b);
        printf("%lld\n",calc(b)-calc(a-1));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: vue-admin-beautiful-plus 是一个基于 Vue.js 和 Element UI 的后台管理系统模板,它提供了丰富的组件和功能,可以快速搭建出美观、易用的后台管理系统。它支持多种主题和布局,可以根据不同的需求进行定制。此外,它还提供了丰富的文档和示例,方便开发者快速上手。 ### 回答2: vue-admin-beautiful-plus是一个开源的基于Vue.js的后台管理系统解决方案。它是在vue-admin-template和ant-design-vue的基础上进行二次开发而来的,并加入了一些额外的功能和特性。 vue-admin-beautiful-plus提供了一套易于使用、美观且功能强大的界面组件,使得开发者能够更加便捷地构建自己的后台管理系统。它包含了诸如菜单栏、面包屑导航、表格、表单、图表和各种常用的UI组件等等,同时也支持自定义主题样式。 除了基本的界面组件,vue-admin-beautiful-plus还提供了一些高级功能,例如路由权限管理、动态菜单生成、多语言支持等等。这些功能能够大大提高开发效率,同时也能够满足不同项目的需求。 值得一提的是,vue-admin-beautiful-plus还提供了一些内置的业务组件和模板,例如用户管理、角色管理、权限管理等等,这些组件和模板可以在快速开发过程中起到很好的辅助作用。 总之,vue-admin-beautiful-plus是一个功能丰富、易于使用且美观的后台管理系统解决方案,它能够帮助开发者快速构建高质量的后台管理系统。无论是个人项目还是企业项目,都可以考虑使用vue-admin-beautiful-plus来提升开发效率和用户体验。 ### 回答3: vue-admin-beautiful-plus 是一个基于 Vue.js 框架的管理后台模板。它是从 vue-admin-beautiful 项目中改良出来的,加入了更多的功能和样式选择。这个模板提供了许多常见的管理后台功能页面,例如仪表盘、表格、表单、图表等,可以帮助开发人员快速搭建一个美观且功能强大的管理后台系统。 vue-admin-beautiful-plus使用了 Element Plus UI 框架,它是 Element UI 的升级版本,提供了更丰富和成熟的组件库。开发人员可以通过自定义指令和组件参数的方式,非常方便地扩展和定制页面的功能和样式。 该模板还提供了许多实用的插件和工具,例如权限管理插件、国际化插件等,可以帮助开发人员更好地管理和维护后台系统。另外,vue-admin-beautiful-plus 还支持响应式设计,适配了不同尺寸的设备,使得后台系统在手机、平板和电脑等各种设备上都能良好地展示和使用。 总的来说,vue-admin-beautiful-plus 是一个功能齐全、易用且美观的管理后台模板,能够帮助开发人员快速构建出强大的管理后台系统。无论是个人开发者还是企业开发团队,都能够在该模板的基础上进行二次开发和定制,以满足自己的具体需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值