CodeForces - 55D Beautiful numbers —— 数位DP

本文针对CodeForces-55D问题提供了解决方案,该问题要求找出区间内能被其非零位数整除的所有数的数量。采用动态规划方法,通过计算从高位到当前位置的数的最小公倍数来解决问题。

题目链接: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。

 

 

代码如下:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const double eps = 1e-6;
 5 const int INF = 2e9;
 6 const LL LNF = 9e18;
 7 const int mod = 1e9+7;
 8 const int maxn = 20;
 9 
10 const int MOD = 2520;   //1……9的最小公倍数为2520;
11 
12 int digit[20], M[2550];
13 LL dp[20][50][2550];    //1 …… 9 组合而成的最小公倍数有48个,故离散化后50个足矣。
14 
15 LL gcd(LL a, LL b)
16 {
17     return b==0?a:gcd(b, a%b);
18 }
19 
20 LL dfs(int pos, LL lcm, LL num, bool lim)
21 {
22     if(!pos) return (num%lcm==0);   //除得尽
23     if(!lim && dp[pos][M[lcm]][num]!=-1) return dp[pos][M[lcm]][num];
24 
25     LL ret = 0;
26     int maxx = lim?digit[pos]:9;
27     for(int i = 0; i<=maxx; i++)
28         ret += dfs(pos-1, i?lcm*i/gcd(lcm, i):lcm, (num*10+i)%MOD, lim&&(i==maxx));
29 
30     if(!lim) dp[pos][M[lcm]][num] = ret;
31     return ret;
32 }
33 
34 LL solve(LL n)
35 {
36     int len = 0;
37     while(n)
38     {
39         digit[++len] = n%10;
40         n /= 10;
41     }
42     return dfs(len, 1, 0, true);
43 }
44 
45 int main()
46 {
47     LL T, n, m;
48     scanf("%lld", &T);
49     memset(dp,-1,sizeof(dp));
50 
51     int cnt = 0;
52     /*  原来还可以这样求一个集合的不同组合的最小公倍数的个数。
53         先算出这个集合所有元素的最小公倍数,那么我们可以得出一个结论:
54         这个最小公倍数必定能被其他元素组合的最小公倍数整除。根据这个结论,
55         我们就可以求出一个集合中不同元素组合的最小公倍数的个数
56     */
57     for(int i = 1; i<=MOD; i++)
58         if(MOD%i==0)
59             M[i] = ++cnt;
60     while(T--)
61     {
62         scanf("%lld%lld",&m,&n);
63         LL ans = solve(n) - solve(m-1);
64         printf("%lld\n", ans);
65     }
66     return 0;
67 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7911004.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值