Codeforces 55D Beautiful numbers【数位dp】

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).

Sample test(s)
input
1
1 9
output
9
input
1
12 15
output
2


 

定义美丽数n:n可以被它的每一位数字整除。

题意:问你在区间[L, R]里面的美丽数个数。


思路:1-9的lcm为2520。

设置dp[i][j][k]表示 整数位长度为i、当前整数对2520取余为j,前i位lcm为k的合法数字。

这样会MLE,离散化下合法的lcm就可以了。


AC代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 1000000
#define eps 1e-8
#define MAXN (200000+10)
#define MAXM (100000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
int gcd(int a, int b){
    return b == 0 ? a : gcd(b, a%b);
}
int lcm(int a, int b){
    return a / gcd(a, b) * b;
}
int rec[2600];
LL dp[30][2520][50];
int Newyu(int s, int bit){
    return (s * 10 + bit) % 2520;
}
int Newlcm(int s, int bit){
    return bit == 0 ? s : lcm(s, bit);
}
void Init()
{
    int cnt = 0;
    for(int i = 1; i <= 2520; i++)
        if(2520 % i == 0)
            rec[i] = cnt++;
}
int bit[30];
LL DFS(int pos, int nowyu, int nowlcm, bool yes, bool zero)
{
    if(pos == -1) return nowyu % nowlcm == 0;
    if(!yes && dp[pos][nowyu][rec[nowlcm]] != -1) return dp[pos][nowyu][rec[nowlcm]];
    LL ans = 0;
    int End = yes ? bit[pos] : 9;
    for(int i = 0; i <= End; i++)
        ans += DFS(pos-1, zero&&i==0 ? 0 : Newyu(nowyu, i), zero&&i==0 ? 1 : Newlcm(nowlcm, i), yes&&i==End, zero&&i==0);
    if(!yes) dp[pos][nowyu][rec[nowlcm]] = ans;
    return ans;
}
LL Count(LL n)
{
    int len = 0;
    while(n)
    {
        bit[len++] = n % 10;
        n /= 10;
    }
    return DFS(len-1, 0, 1, 1, 1);
}
int main()
{
    Init(); CLR(dp, -1);
    int t; Ri(t);
    W(t)
    {
        LL n, m;
        scanf("%I64d%I64d", &n, &m);
        printf("%I64d\n", Count(m) - Count(n-1));
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值