CF 55D 离散化各个位数的倍数

本文介绍了一种使用数位动态规划(数位DP)解决特定类型数学问题的方法,特别是针对那些需要判断一个数是否能被其所有非零位数整除的问题。通过将数位DP与数学中最少公倍数的概念相结合,文章详细阐述了解题过程,并提供了一个具体的CF55D题目的解题思路及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门


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

Sample test(s)
input
1
1 9
output
9
input
1
12 15
output
2

/**
CF 55D 数位dp(一个数是组成它的所有位数对应数的倍数)
题目大意:找出给定区间内有多少数,其本身是所有组成它的各个位上的数的倍数
解题思路:(kuangbin)一个数能被它的所有非零数位整除,则能被它们的最小公倍数整除,而1到9的最小公倍数为2520,
           数位DP时我们只需保存前面那些位的最小公倍数就可进行状态转移,到边界时就把所有位的lcm求出了,
           为了判断这个数能否被它的所有数位整除,我们还需要这个数的值,显然要记录值是不可能的,其实我们只
           需记录它对2520的模即可,这样我们就可以设计出如下数位DP:dfs(pos,mod,lcm,f),pos为当前
           位,mod为前面那些位对2520的模,lcm为前面那些数位的最小公倍数,f标记前面那些位是否达到上限,
           这样一来dp数组就要开到19*2520*2520,明显超内存了,考虑到最小公倍数是离散的,1-2520中可能
           是最小公倍数的其实只有48个,经过离散化处理后,dp数组的最后一维可以降到48,这样就不会超了。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
#define me(x,y) memset(x,y,sizeof(x));
#define LL long long
typedef long long ll;
const int mod=2520;
int a[25];
LL dp[25][2550][50];
int index[2550];
int gcd(int a,int b)
{
    if(b==0)return a;
    else return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}
void init()
{
    int num=0;
    for(int i=1;i<=mod;i++)
    {
        if(mod%i==0)
            index[i]=num++;
    }
}
LL dfs(int pos,int presum,int prelcm,int limit)
{
    if(pos<=0) return presum%prelcm==0;
   // if(sta<0) return 0;
    if(!limit && dp[pos][presum][index[prelcm]]!=-1)
        return dp[pos][presum][index[prelcm]];
    int up=limit ? a[pos] : 9;
    LL ans=0;
    for(int i=0;i<=up;i++)
    {
        int nowsum=(presum*10+i)%mod;
        int nowlcm=prelcm;
        if(i) nowlcm=lcm(nowlcm,i);
        ans+=dfs(pos-1,nowsum,nowlcm,limit&&i==up);
    }
    if(!limit) dp[pos][presum][index[prelcm]]=ans;
    return ans;
}

LL solve(LL x)
{
    int pos=0;
    while(x)
    {
        a[++pos]=x%10;
        x/=10;
    }
    return dfs(pos,0,1,1);
}

int main()
{
    int t;
    LL l,r;
    init();
    cin>>t;
    me(dp,-1);
    while(t--)
    {
        cin>>l>>r;
        //while(~scanf("%d%d",&l,&r)&&(l+r))
        //while(~scanf("%d",&r))

        printf("%I64d\n",solve(r)-solve(l-1));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值