题目链接:https://vjudge.net/problem/CodeForces-55D

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

题解:

题意:求一段区间内满足“能整除所有位上的数(非0)”的数的个数。

1.可知:如果一个数能被一个集合内的所有数整除,那么它必定能被这个集合上的数的最小公倍数整除。所以,在处理的时候,我们只需要记录最小公倍数。经过计算,1到9的最小公倍数为2520.

2.dp[pos][lcm][num]:处理到pos位,从最高位到pos位上的数的最小公倍数为lcm,且从最高位到pos位所形成的数%2520为num。

代码如下:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = ; const int MOD = ; //1……9的最小公倍数为2520; int digit[], M[];
LL dp[][][]; //1 …… 9 组合而成的最小公倍数有48个,故离散化后50个足矣。 LL gcd(LL a, LL b)
{
return b==?a:gcd(b, a%b);
} LL dfs(int pos, LL lcm, LL num, bool lim)
{
if(!pos) return (num%lcm==); //除得尽
if(!lim && dp[pos][M[lcm]][num]!=-) return dp[pos][M[lcm]][num]; LL ret = ;
int maxx = lim?digit[pos]:;
for(int i = ; i<=maxx; i++)
ret += dfs(pos-, i?lcm*i/gcd(lcm, i):lcm, (num*+i)%MOD, lim&&(i==maxx)); if(!lim) dp[pos][M[lcm]][num] = ret;
return ret;
} LL solve(LL n)
{
int len = ;
while(n)
{
digit[++len] = n%;
n /= ;
}
return dfs(len, , , true);
} int main()
{
LL T, n, m;
scanf("%lld", &T);
memset(dp,-,sizeof(dp)); int cnt = ;
/* 原来还可以这样求一个集合的不同组合的最小公倍数的个数。
先算出这个集合所有元素的最小公倍数,那么我们可以得出一个结论:
这个最小公倍数必定能被其他元素组合的最小公倍数整除。根据这个结论,
我们就可以求出一个集合中不同元素组合的最小公倍数的个数
*/
for(int i = ; i<=MOD; i++)
if(MOD%i==)
M[i] = ++cnt;
while(T--)
{
scanf("%lld%lld",&m,&n);
LL ans = solve(n) - solve(m-);
printf("%lld\n", ans);
}
return ;
}

CodeForces - 55D Beautiful numbers —— 数位DP的更多相关文章

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

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

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

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

  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. 高通android7.0刷机工具使用介绍

    刷机工具安装 1. 安装QPST.WIN.2.7 Installer-00448.3 2. 安装python2.7,并配置其环境变量 刷机方法 1.将编译后的刷机文件拷贝到如下目录:SC20_CE_p ...

  2. 【HDOJ6225】Little Boxes(Java)

    题意:输入整数a,b,c,d,输出他们的和 a, b, c, d ≤ 2^62 思路:ANS可能会炸long long 队友直接上Java import java.math.BigInteger; i ...

  3. BZOJ4723: [POI2017]Flappy Bird

    $n \leq 500000$个水管,每秒横坐标加一,纵坐标如果你点击就+1否则-1,问从$(0,0)$飞到$m$处最少点多少次,或者说明无解. 如果能飞到某个水管的高度区间$[L,R]$,那么答案肯 ...

  4. Android Service服务-(转)

    Service是Android系统中提供的四大组件之一.它是运行在后台的一种服务,一般声明周期较长,不直接与用户进行交互.   服务不能自己运行,需要通过调用Context.startService( ...

  5. OSI模型详解

    OSI 七层模型通过七个层次化的结构模型使不同的系统不同的网络之间实现可靠的通讯,因此其最主要的功能就是帮助不同类型的主机实现数据传输 . 完成中继功能的节点通常称为中继系统.在OSI七层模型中,处于 ...

  6. 提高google网站访问速度

    修改:C:\Windows\System32\drivers\etc\hosts文件 # google websites.203.208.46.180 ssl.gstatic.com203.208.4 ...

  7. android 获得屏幕状态

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  8. 创建es索引-格式化和非格式化

    创建es索引-格式化和非格式化 学习了:https://www.imooc.com/video/15768 索引有结构化和非结构化的区分: 1, 先创建索引,然后POST修改mapping 首先创建索 ...

  9. Override和Overload差别,Overloaded的方法能否够改变返回值的类型?

    Overload是重载的意思, Override是覆盖的意思,也就是重写. 重载Overload表示同一个类中能够有多个名称同样的方法,但这些方法的參数列表各不同样(即參数个数或类型不同). 重写Ov ...

  10. atitit. hb 原生sql跨数据库解决原理 获得hb 数据库类型执行期获得Dialect

    atitit. hb 原生sql跨数据库解决原理 获得hb 数据库类型执行期获得Dialect   #-----原理 Hibernate 执行期获得Dialect   2010-07-28 12:59 ...