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

Examples
input
1
1 9
output
9
input
1
12 15
output
2

题目链接:http://codeforces.com/problemset/problem/55/D

话说这是我第一次做codeforces,感觉他的提交好吊,就类似WF一样,看着好爽!!(我现在还不知道怎么在cf的题库里搜索题目,哪位大牛知道,指点一下,感激不尽!)

分析:一个数能被它的所有非零数位整除,则能被它们的最小公倍数整除,而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,这样就不会超了。

分析链接:http://www.cnblogs.com/algorithms/archive/2012/09/02/2668021.html

题解:看了好几天,最后弄了个容易懂的代码(如上),看懂了,首先要把那个数对2520取模(因为这个数如果想要符合的话,首先要被2520整除,因为2520是1~9的lcm),这只是第一个条件(此时取模之后存在mod参数之中),第二个条件是对所有出现过的位数上的值取模,如果mod对位数上取模==0,就可以了。

#include<cstdio>
#include<cstring>
#include<iostream> using namespace std; #define N 19
#define MOD 2520
typedef __int64 LL;
int t[],cnt;
LL dp[N][MOD][];
int dig[N]; int GCD(int a,int b) {return b?GCD(b,a%b):a;}
int LCM(int a,int b) {return a/GCD(a,b)*b;} void init()
{
cnt=;
for (int i=;i<=MOD;i++)
{
if (!(MOD%i)) t[cnt++]=i;
}
memset(dp,-,sizeof(dp));
} int Binary_search(int x)
{
int mid,l=,r=cnt;
while(l<r-)
{
mid=l+r>>;
if (t[mid]>x) r=mid;
else l=mid;
}
return l;
} LL dfs(int pos,int mod,int lcmid,bool f)
{
if (pos<) return !(mod%t[lcmid]);
LL &aa=dp[pos][mod][lcmid];
if (!f&&aa!=-) return aa;
int end=f?dig[pos]:;
LL res=;
for (int i=;i<=end;i++)
{
int nmod=(mod*+i)%MOD;
int nlcmid=lcmid;
if (i) nlcmid=Binary_search(LCM(t[lcmid],i));
res+=dfs(pos-,nmod,nlcmid,f&&i==end);
}
return f?res:aa=res;
} LL sol(LL x)
{
int ind=;
dig[]=;//这个一定不要少了,如果没有这个,1 9的时候会错
for (;x;x/=) dig[ind++]=x%;
dfs(ind-,,,);
} int main()
{
int o;
cin>>o;
init();
while(o--)
{
LL x,y;
cin>>x>>y;
cout<<sol(y)-sol(x-)<<endl;
} return ;
}

codeforces 55D - Beautiful numbers(数位DP+离散化)的更多相关文章

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

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

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

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

  3. Codeforces - 55D Beautiful numbers (数位dp+数论)

    题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...

  4. codeforces 55D. Beautiful numbers 数位dp

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

  5. FZU2179/Codeforces 55D beautiful number 数位DP

    题目大意: 求  1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...

  6. CF 55D. Beautiful numbers(数位DP)

    题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...

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

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

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

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

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

随机推荐

  1. hive学习笔记_hive的表创建

    创建hive表注意事项 一.表分隔符必须与读取的数据文件一致,比如例子的分隔符为 '\t'(制表符),hive下默认分隔符是制表符. 二.最好指定分区作为数据之间的区分. 三.创建完表可以desc+表 ...

  2. background-position 用法详细介绍

    语法: background-position : length || length background-position : position || position 取值: length  : ...

  3. MySQL 5.5: InnoDB Change Buffering

    To speed up bulk loading of data, InnoDB implements an insert buffer, a special index in the InnoDB ...

  4. 爬虫入门---Python2和Python3的不同

    Python强大的功能使得在写爬虫的时候显得十分的简单,但是Python2和Python3在这方面有了很多区别. 本人刚入门爬虫,所以先写一点小的不同. 以爬取韩寒的一篇博客为例子: 在Python2 ...

  5. 开发环境安装 Java Mysql MyEclipse Android Adt

    一.安装 JDK 1.官网下载JDK最新版本,下载地址如下: http://www.oracle.com/technetwork/java/javase/downloads/index.html 这里 ...

  6. $(function(){})和$(document).ready(function(){}) 的用法

    当文档载入完毕就执行,以下几种效果是等价的:1. $(function(){ //这个就是jQuery ready()的简写,即下2的简写 // do something }); 2. $(docum ...

  7. ClassNotFoundException异常的解决方法

    java.lang.ClassNotFoundException 说是某个类没有找到,找了下,发现这个类是在项目里面的,那么久奇怪了,为什么应用找不到这个类, 然后用mvn install -Dmav ...

  8. 为什么匿名内部类只能访问其所在方法中的final类型的局部变量?

    大部分时候,类被定义成一个独立的程序单元.在某些情况下,也会把一个类放在另一个类的内部定义,这个定义在其他类内部的类就被称为内部类,包含内部类的类也被称为外部类. class Outer { priv ...

  9. win7下MariaDB10.0的my.ini配置文件的位置

    msi版本的,安装后在安装目录下的\data\my.ini 常用的配置选项: 1.修改默认的存储引擎 在配置文件my.ini(linxu下为my.cnf) 中的 [mysqld] 下面加入defaul ...

  10. 通用的业务编码规则设计实现[转:http://www.cnblogs.com/xqin/p/3708367.html]

    一.背景 每一个企业应用中不可避免的都会涉及到业务编码规则的问题,比如订单管理系统中的订单编号,比如商品管理系统中的商品编码,比如项目管理系统中的项目编码等等,这一系列的编码都需要管理起来,那么它们的 ...