题目链接: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. Vmware error:无法获得 VMCI 驱动程序的版本: 句柄无效。

    error:无法获得 VMCI 驱动程序的版本: 句柄无效.驱动程序“vmci.sys”的版本不正确.请尝试重新安装 VMware Workstation.开启模块 DevicePowerOn 的操作 ...

  2. VS2015 “GENERATERESOURCE”任务意外失败 解决方法

    昨天把项目解决方案Copy到另外的机器上执行,遭遇了一场"任务意外失败",网上搜索一下,顺利解决了,在此记录一下. Visual Studio.net 工程更换机器编译时遇到”Ge ...

  3. Java线程的学习_线程池

    系统启动一个新线程需要很高的成本,因为它涉及与操作系统交互.在这种情况下,使用线程池可以很好地提高性能,尤其是当程序中需要创建大量生存期很短暂的线程时. 线程池在系统启动时即创建大量空闲的线程,程序将 ...

  4. 乘法运算(codevs 3254)

    题目描述 Description 编制一个乘法运算的程序. 从键盘读入2个100以内的正整数,进行乘法运算并以竖式输出. 输入描述 Input Description 输入只有一行,是两个用空格隔开的 ...

  5. Codevs 2801 LOL盖伦的蹲草计划

    题目描述 Description 众所周知,LOL这款伟大的游戏,有个叫盖伦的英雄.他的伟大之处在于他特别喜欢蹲草丛阴人(XL:蹲草阴人也算英雄?!CZQ:没办法,个个都是这么玩的).某日,德玛西亚与 ...

  6. 球形空间产生器 BZOJ 1013

    球形空间产生器 [问题描述] 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便 ...

  7. [MFC] CString小用例

    在实际项目中需要对数字转成的字符串进行格式处理, 如: 50.500000,需要转化成50.5; 1.00000,需要转化成1, 对于编号而言要求都是4位,不足前面补0. 如1转成0001,222转成 ...

  8. iOS推送的开启与关闭

    开启: 
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationType ...

  9. 用“道”的思想解决费用流问题---取/不取皆是取 (有下界->有上界) / ACdreamoj 1171

    题意: 给一个矩阵,给出约束:i(0<i<n)行至少去ai个数,j行至少取bi个数,要求取的数值之和最小. 开始一见,就直接建了二分图,但是,发现这是有下界无上界最小费用流问题,肿么办.. ...

  10. 关于xshell无法连接到centos的问题

    1.xshell无法连接到centos:拒绝连接(无线网) 在xshell ping centos出现: 解决方法: 1. 2.重启下网卡: [root@localhost ~]# /etc/init ...