CF 55D - Beautiful numbers(数位DP)

题意:

如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers。
求区间 [a,b] 中 Beautiful numbers 的个数。

分析:先分析出,2~9 的最大的最小公倍数是 2520({5,7,8,9}),先预处理出所有可能的最小公倍数m[c]

dp[i][d][c]表示长度i, 余数d,最小公倍数是m[c]的个数。

#include<cstdio>
#include<cstring>
#define mod 2520
ll dp[35][2520][100];
int bit[64],m[200],mnum;
int gcd(int a,int b){
    int tmp;
    while(a%b){
        tmp=b;
        b=a%b;
        a=tmp;
    }
    return b;
}
int lcm(int a,int b){
    return a*b/gcd(a,b);
}//查最小公倍数的标号
int tfind(int x){
    int ll=1,rr=mnum;
    while(ll<=rr){
        int mid=(ll+rr)>>1;
        if(m[mid]<x)ll=mid+1;
        else rr=mid-1;
    }
    return ll;
}
void init(){
    memset(dp,-1,sizeof(dp));
    mnum=0;
    for(int i=1;i<=mod;++i)
        if(mod%i==0)
        m[++mnum]=i;
}
ll dfs(int i,int d,int c,int e){
    if(i==0)return d%m[c]?0:1;
    if(!e&&dp[i][d][c]!=-1)return dp[i][d][c];
    int l=e?bit[i]:9;
    ll num=0;
    for(int j=0;j<=l;++j)
    {
        int td=(d*10+j)%mod;
        int tc=c;
        if(j)tc=tfind(lcm(m[c],j));
        num+=dfs(i-1,td,tc,e&&(j==l));
    }
    return e?num:dp[i][d][c]=num;
}
ll solve(ll x){
    int len=0;
    while(x){
        bit[++len]=x%10;
        x/=10;
    }
    return dfs(len,0,1,1);
}
int main()
{
    int t;
    scanf("%d",&t);
    init();
    ll x,y;
    while(t--){
        scanf("%I64d%I64d",&x,&y);
        printf("%I64d\n",solve(y)-solve(x-1));
    }
return 0;
}
时间: 2024-12-19 08:44:21

CF 55D - Beautiful numbers(数位DP)的相关文章

CodeForces 55D Beautiful numbers 数位DP+数学

题意大概是,判断一个正整数区间内有多少个整数能被它自身的每一个非零的数字整除. 因为每一个位置上的整数集s = {0,1,2,3,4,5,6,7,8,9} lcm(s) = 2520 现在有一个整数t是由s中一个或者多个数字构成的,记为abcde,显然t = a*10^4+b*10^3+c*10^2+d*10^1+e 要使得t能被a,b,c,d,e整除,必然有t % lcm(a,b,c,d,e) = 0 因为a,b,c,d,e去重之后一定是s的一个子集,所以lcm(s)一定是lcm(a,b,c,

CodeForces 55D Beautiful numbers(数位dp&amp;&amp;离散化)

题目链接:[kuangbin带你飞]专题十五 数位DP A - Beautiful numbers 题意 ps:第一道数位dp,题真好,虽然是参考大牛方法悟过才a,但仍收获不少. 求一个区间内的Beautiful numbers有多少个.Beautiful numbers指:一个数能整除所有组成它的非0数字. 例如15可以被1和5整除,所以15是Beautiful numbers. 思路 Beautiful numbers指:一个数能整除所有组成它的非0数字. 等同于 一个数能整除 所有组成它的

CodeForces 55D - Beautiful numbers - [数位DP+离散化]

题目链接:https://cn.vjudge.net/problem/CodeForces-55D 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

CodeForces - 55D - Beautiful numbers(数位DP,离散化)

链接: https://vjudge.net/problem/CodeForces-55D 题意: 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

CodeForces 55D Beautiful numbers (数位DP)

题意:给求给定区间中该数能整除每一位的数的数量. 析:dp[i][j][k] 表示前 i 位,取模2520为 j,最小倍数是 k,但是这样,数组开不下啊,那怎么办呢,其实,0-9的最小公倍数的不同各类并没有那么多, 其实就48种,所以我们可以给这48个一个编号,然后就能开出来了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <strin

Codeforces 55D Beautiful numbers 数位dp(入门

题目链接:点击打开链接 题意: 我们认为一个数 num 能被每一位上的数字整除(expect 0) 那么这个数num就是合法的. 给出区间[l,r] ,问这个区间内有多少个合法的数. 首先solve(long x) 返回 [0, x] 内的合法个数,答案就是 solve(r) - solve(l-1); 以1234567为例 flag表示当前这位是能任意填,还是只能填<=该位对应的数字 若当前搜索的是第三位,且第二位已经填了0或1,则此时第三位可以任意填. 若第二位填的是2,则第三位只能填 [0

Codeforces Beta Round #51---D. Beautiful numbers(数位dp, 巧妙)

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 num

CF55D Beautiful numbers (数位dp)

题目链接 题解 一个数能被一些数整除,那么一定被这些数的\(lcm\)整除 那么我们容易想到根据\(lcm\)设状态 我们可以发现有用的\(lcm\)只有\(48\)个 那么按照一般的数位\(dp\) 设出状态:\(f_{i,j,k,0/1}\)表示前\(i\)位,\(lcm=j\),模\(lcm\)的余数是\(k\),是否达到上界 但是这样子是无法转移的(因为新添加一个数模数可能会产生变化) 那么我们把模数统一成\(2520\) 复杂度\(O(T*L*48*2500*2)\) 其中\(L\)是

CF 55D Beautiful numbers (数位DP)

题意: 如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018) 思路: 数字很大,不能暴力.但是想要知道一个数是否为Beautiful number时,至少得等到它的所有位都出现吧?不然如何确定其实可以被整除的呢? 分析一下,类似2232和3232等这样的数字,这两个只是出现了2和3而已,他们的lcm都是6,所以有可以压缩统计的地方就是lcm,开一维来存储.接下来考虑