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. URAL 1176 Hyperchannels(欧拉回路路径)

    Hyperchannels Time limit: 1.0 secondMemory limit: 64 MB The Galaxy Empire consists of N planets. Hyp ...

  2. Linux驱动设计编译错误信息集锦

    1.warning: passing argument 2 of 'request_irq' from incompatible pointer type http://blog.sina.com.c ...

  3. JSBinding + SharpKit / 使用 Firefox 调试 JS

    已经准备好,google打不开,等打开了再贴图上来

  4. php开发memcached

    一.memcached 简介 memcached是高性能的分布式内存缓存服务器.一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度.提高可扩展 性.它可以应对任意 ...

  5. apk反编译生成程序的源代码和图片、XML配置、语言资源等文件

    Android应用的UI越来越漂亮,遇到喜欢的我们可以通过反编译,得到应用的源代码借鉴下别人的思想. 具体步骤: 1.下载 apktool 下载地址:https://code.google.com/p ...

  6. Jquery easyui的validatebox控件和正则表达式

    http://blog.csdn.net/dandanzmc/article/details/36421465 仔细观察jquery.validatebox.js文件,会发现它的验证其实还是采用的正则 ...

  7. OpenJudge计算概论-单词替换

    /*====================================================================== 单词替换 总时间限制: 1000ms 内存限制: 65 ...

  8. SLA了解

    许多企业正要求服务品质协议(SLA),SLA 可以保证企业为之付费的 IT 服务的可靠性.随着 Web 服务成为主流,客户将要求保证服务质量的 SLA.在本文中,Judith M. Myerson 说 ...

  9. pythonchallenge关卡破解

    第一关:pow(2,38) 第二关: import string table = str.maketrans(string.ascii_lowercase, string.ascii_lowercas ...

  10. 【转】深入PHP FTP类的详解

    FTP是一种文件传输协议,它支持两种模式,一种方式叫做Standard (也就是Active,主动方式),一种是 Passive (也就是PASV,被动方式). Standard模式 FTP 的客户端 ...