CodeForces 55D Beautiful numbers (树形DP)

想了半天,总算想出来了。这题刚上来的思路很明显是11维DP。。但是明显不可取。。

这题的关键在于只要两个数前面的拥有的数字是一样的,而且此时与其最小公倍数的模是一样的,那么这时候就可以认为对所有的数字取模都是相等的,那么后面的总情况数属于完美数的情况也是相同的。

只要想到这步的话,那么基本思路就出来了,我第一次居然脑残的记录lcm与模2520(2到9的最小公倍数),首先lcm相同并不代表出现的数字相同先不说,仅仅这个最大值就太大了,lcm最大是2520,这样的话就需要开一个20*2520*2520的数组,我一看给的内存挺大的,就这么写了,然后提交。。果然MLE  on TEST 1.。。。

然后才用的二进制状压记录2到9出现的数,因为0到1不用记录,所以只需要开1<<8大小就可以了。

这样空间复杂度就少了很多。然后就这样一改,果然AC了。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=2520;
const int INF=0x3f3f3f3f;
const double eqs=1e-8;
LL dp[19][257][2530], c[20], lcm[257];
int gcd(int x, int y)
{
    return y==0?x:gcd(y,x%y);
}
int getlcm(int x, int y)
{
    if(!x) return y;
    return x*y/gcd(x,y);
}
int find1(int x, int y)
{
    if(y<=1) return x;
    y-=2;
    if(x&(1<<y)) return x;
    return x|(1<<y);
}
int init()
{
    int ans=1;
    int i, j;
    lcm[0]=1;
    for(i=1;i<=255;i++){
        ans=1;
        for(j=2;j<=9;j++){
            if(i&(1<<(j-2))){
                ans=getlcm(ans,j);
            }
        }
        lcm[i]=ans;
    }
}
LL dfs(int cnt, int maxd, int zero, int z, int mods)
{
    if(cnt==-1) return !(mods%lcm[z]);
    if(zero&&maxd&&dp[cnt][z][mods]!=-1) return dp[cnt][z][mods];
    int i, r=maxd?9:c[cnt];
    LL ans=0;
    for(i=0;i<=r;i++){
            ans+=dfs(cnt-1,maxd||i<r,zero||i,find1(z,i),(mods*10+i)%mod);
        }
    if(zero&&maxd) dp[cnt][z][mods]=ans;
    return ans;
}
LL Cal(LL x)
{
    int i, cnt=0;
    while(x){
        c[cnt++]=x%10;
        x/=10;
    }
    return dfs(cnt-1,0,0,0,0);
}
int main()
{
    int t;
    LL l, r;
    memset(dp,-1,sizeof(dp));
    init();
    scanf("%d",&t);
    while(t--){
        scanf("%I64d%I64d",&l,&r);
        printf("%I64d\n",Cal(r)-Cal(l-1));
    }
    return 0;
}
时间: 2024-08-26 11:55:01

CodeForces 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 55D. Beautiful numbers(数位DP,离散化)

Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得只需要记录搜到当前位出现了哪些数字作为状态即可,明显是假算法...感觉这是一道数位DP好题.可以这样思考:一个数要想被其各位数字分别都整除,等价于它被那些数字的LCM整除.因此记录当前位,当前数对(1~9的LCM)取模的结果,当前出现的数字的LCM这三个值作为状态才合理,即dp[pos][sum][

CodeForces - 55D 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

CodeForces 55D Beautiful numbers

Beautiful numbers 题目链接 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