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≤liri≤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

  1. /*
  2. a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits.
  3. 问一个区间内[l,r]有多少个Beautiful数字
  4. 范围9*10^18
  5. 数位统计问题,构造状态也挺难的,我想不出,我的思维局限在用递推去初始化状态,而这里的状态定义也比较难
  6. 跟pre的具体数字有关
  7. 问了NotOnlySuccess的,豁然开朗  Orz
  8. 一个数字要被它的所有非零位整除,即被他们的LCM整除,可以存已有数字的Mask,但更好的方法是存它们的LCM{digit}
  9. int MOD = LCM{1,2,9} = 5 * 7 * 8 * 9 = 2520
  10. 按照定义,数字x为Beautiful :
  11. x % LCM{digit[xi]} = 0
  12. 即 x % MOD % LCM{digit[xi]} = 0
  13. 所以可以只需存x % MOD,范围缩小了
  14. 而在逐位统计时,假设到了pre***(pre指前面的一段已知的数字,而*是任意变)
  15. ( preSum * 10^pos + next )  % MOD % LCM(preLcm , nextLcm)
  16. =  ( preSum * 10 ^ pos % MOD + next % MOD ) % LCM(preLcm , nextLcm)
  17. == 0
  18. 而next,nextLcm是变量,上面的比较式的意义就是
  19. 在已知pos , preSum , preLcm情况下有多少种(next,nextLcm)满足式子为0
  20. 而这个就是一个重复子问题所在的地方了,需要记录下来,用记忆化搜索
  21. dfs(pos , preSum , preLcm , doing)
  22. 加一个标记为doing表示目前是在计算给定数字的上限,还是没有上限,即***类型的
  23. 这样就将初始化以及逐位统计写在一个dfs了,好神奇!!!
  24. 还有一点,10以内的数字情况为2^3 , 3^2 , 5 , 7
  25. 所以最小公倍数组合的情况只有4*3*2*2 = 48
  26. 可以存起来,我看NotOnlySuccess的写法是
  27. for(int i = 1 ; i <= MOD ; i ++)
  28. {
  29. if(MOD % i == 0)
  30. index = num++;
  31. }
  32. 很棒!!
  33. 所以复杂度大概为19*2520*48*10(状态数*决策数)
  34. 我觉得这题状态的设计不能跟具体数字分开,否则会很难设计吧
  35. 所以用记忆化搜索,存起来
  36. 用具体数字去计算,重复的子问题跟pre关系比较密切
  37. 有一个比较重要的切入点就是LCM,还有%MOD缩小范围,才能存储
  38. 还有优化到只需%252的,更快
  39. 不过我觉得%2520比较好理解
  40. */
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long int LL;

const int MOD=2520;

LL Gcd(LL a,LL b)
{
    return (a==0)?b:Gcd(b%a,a);
}

LL Lcm(LL a,LL b)
{
    if(a>b) swap(a,b);
    return a/Gcd(a,b)*b;
}

LL dp[20][MOD+10][50],lcm[MOD+10],bit[20];

void Init()
{
    int num=0;
    for(int i=1;i<=MOD;i++)
    {
        if(MOD%i==0)
            lcm=num++;
    }
    memset(dp,-1,sizeof(dp));
}

LL dfs(int pos,int presum,int preLcm,bool limit)
{
    if(pos==-1)
        return presum%preLcm==0;
    if(!limit&&~dp[pos][presum][lcm[preLcm]]) return dp[pos][presum][lcm[preLcm]];
    int end=limit?bit[pos]:9;
    LL ans=0;
    for(int i=0;i<=end;i++)
    {
        int newsum=(presum*10+i)%MOD;
        int newLcm=preLcm;
        if(i)
        {
            newLcm=Lcm(preLcm,i);
        }
        ans+=dfs(pos-1,newsum,newLcm,limit&&i==end);
    }
    if(!limit)
        dp[pos][presum][lcm[preLcm]]=ans;
    return ans;
}

LL calu(LL x)
{
    int pos=0;
    while(x)
    {
        bit[pos++]=x%(10LL);
        x/=(10LL);
    }
    return dfs(pos-1,0,1,true);
}

int main()
{
    int t;
    scanf("%d",&t);
    Init();
    while(t--)
    {
        LL a,b;
        scanf("%I64d%I64d",&a,&b);
        printf("%I64d\n",calu(b)-calu(a-1));
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

CodeForces 55D Beautiful numbers的更多相关文章

  1. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  2. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  3. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  4. CodeForces - 55D Beautiful numbers —— 数位DP

    题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...

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

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

  6. CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)

    题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...

  7. CodeForces 55D Beautiful numbers(数位dp+数学)

    题目链接:http://codeforces.com/problemset/problem/55/D 题意:一个美丽数就是可以被它的每一位的数字整除的数. 给定一个区间,求美丽数的个数. 显然这是一道 ...

  8. CodeForces 55D Beautiful numbers(数位dp)

    数位dp,三个状态,dp[i][j][k],i状态表示位数,j状态表示各个位上数的最小公倍数,k状态表示余数 其中j共有48种状态,最大的是2520,所以状态k最多有2520个状态. #include ...

  9. codeforces 55D. Beautiful numbers 数位dp

    题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...

随机推荐

  1. AngularJs ngList、ngRepeat、ngModelOptions

    ngList 在文本输入的分隔的字符串和字符串数组间做转换,可以是一个固定的字符串分隔符(默认逗号)或正则表达式. 格式:ng-list=”value” value:表达式  通过这个值分隔字符串. ...

  2. 强连通分量的Tarjan算法

    资料参考 Tarjan算法寻找有向图的强连通分量 基于强联通的tarjan算法详解 有向图强连通分量的Tarjan算法 处理SCC(强连通分量问题)的Tarjan算法 强连通分量的三种算法分析 Tar ...

  3. python3,交互模式,无法使用ctrl和方向键,需要和ctrl一块用

    转自csdn博客 http://blog.csdn.net/pumaadamsjack/article/details/52447989 https://pypi.python.org/pypi/re ...

  4. Vector & ArrayList 的主要区别

    1) 同步性:Vector是线程安全的,也就是说是同步的 ,而ArrayList 是线程序不安全的,不是同步的 数2. 2)数据增长:当需要增长时,Vector默认增长为原来一倍 ,而ArrayLis ...

  5. 通过System.getProperties()获取系统参数

    Properties props=System.getProperties(); //系统属性    System.out.println("Java的运行环境版本:"+props ...

  6. Linux安装配置sun-java

    一(不推荐) 1. 下载源码与解压 将下载的源码包,移动到/opt目录下: $ sudo mv ~/Downloads/jdk-8u65-linux-x64.tar.gz  /opt/ 解压: $ s ...

  7. springMVC的注解@RequestParam与@PathVariable的区别

    1.在SpringMVC后台控制层获取参数的方式主要有两种, 一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. ...

  8. spring-data-jpa Repository的基本知识

    1.项目中的Repository对象的使用 2.Repository 引入的两种方式 继承和使用注解 3.Repository接口的定义 Repository 接口是 spring Data 的一个核 ...

  9. HTML教程

    HTML文档可以包含的内容 通过不同的标签,HTML文档可以包含不同的内容,比如文本,链接,图片,列表,表格,表单,框架等. 文本 HTML对文本的支持是最丰富的,你可以设置不同级别的标题,分段和换行 ...

  10. 第二次冲刺-Runner站立会议01

    今天做了什么:主要看了gridview的使用方法 遇到的困难:与适配器的链接不会 明天准备做什么:尽量将gridview与baseadapter适配器连接起来