A - Beautiful numbers(CF 55D)解析

博客解析了CF 55D题目的解法,重点在于利用动态规划求解在给定范围内能被其所有非零数字整除的整数数量。介绍了数位DP的思路,并详细说明了状态转移矩阵的设定,以及如何处理因数最大公倍数问题,以适应大整数范围。最后给出了实现代码。
摘要由CSDN通过智能技术生成

题目

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

解析

数位dp
离散
dp形式为dp[a][b][c]
a为数位
b为当前数字
c为组成这个数字的因子的最大公倍数(为了节省空间)

思路

具体框架还是数位dp那一套。
dp大小为dp[30][2520+10][50]
数位30很好理解
数字大小是2520是因为2520是1到9的最小公倍数。因为这个位置的数字是为了表示这个数字是什么,但是数字会过大,所以我们只要表示成因子和它一样的数字就行了,所以只需要控制在2520中就行了。
第三个数字 50,这个位置的数字是为了表示我们递归到这个位置的时候,这个数字可以被什么数字整除,所以就可以表示成这些数字的最大公因数,所以按道理来说范围也应该是2520,但是超范围了。于是我们要操作下。我们可以发现,这些数字组成的公因数(也就是2520的因子)是有限的,大概50个,我们映射下就行。
这里明白了就很好写了,下面附上代码。

代码

#include<bits/stdc++.h>
#define mset(a,b)   memset(a,b,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值