55D(数位dp)

5 篇文章 0 订阅

D. Beautiful numbers
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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

题意:让你找出L,R之间所有的美数,美数的定义是这个数所有的非零数字能够被这个数本身整除。

解题思路:我觉得这个题出的很好,很巧妙,一个数能够整除一些数字,那么这个数肯定可以整除这些数字的最小公倍数,知道了这个,我们就可以直接数位dp了,dp[i][sum][lcm]表示前i位,累计和为sum,前面所有非零数字的最小公倍数为lcm时所有的美数个数,但是单纯这样写会爆内存,我们可以知道,0-9的最小公倍数是2520,所以在sum这一维我们可以对2520取模,对于lcm这一维,因为2520的因子一共就48个,而中间出现的lcm肯定是2520的一个因子,所以lcm这一维我们可以状态压缩一下,最后dp[64][2530][50]就够了,然后最后一维hash一下就行。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;//2520
ll dp[64][2522][50];//dp[i][psum][plcm],表示i位数,累计和为psum,前面的数的最小公倍数为plcm
int num[100];
int pri[100];
map<int, int> mp;
int cnt;
int gcd(int a, int b)
{
    return a == 0 ? b : gcd(b % a, a);
}
int Lcm(int a, int b)
{
    int term = (a * b);
    term /= gcd(a, b);
    return term;
}
ll dfs(int i, int psum, int plcm, int limit)
{
    if(!limit && dp[i][psum][plcm] != -1) return dp[i][psum][plcm];
    if(i == 0)
    {
        if(psum == 0) return 1;
        if(psum % pri[plcm] == 0) return 1;
        else return 0;
    }
    int up = (limit == 0) ? 9 : num[i];
    ll ans = 0;
    for(int p = 0; p <= up; p++)
    {
        int term = (psum * 10) + p;
        int res;
        if(p != 0) res = Lcm(pri[plcm], p);
        else res = pri[plcm];
        term %= 2520;
        ans += dfs(i - 1, term, mp[res], limit && (p == num[i]));
    }
    if(!limit) dp[i][psum][plcm] = ans;
    return ans;
}
ll solve(ll x)
{
    int len = 0;
    memset(num, 0, sizeof(num));
    while(x)
    {
        num[++len] = x % 10;
        x /= 10;
    }
    return dfs(len, 0, 1, 1);
}
int main()
{
    int T;
    scanf("%d", &T);
    mp.clear();
    memset(dp, -1, sizeof(dp));
    cnt = 0;
    for(int i = 1; i <= 2520; i++)
    {
        if(2520 % i == 0)
        {
            pri[++cnt] = i;
            mp[i] = cnt;
        }
    }
    while(T--)
    {
        ll l, r;
        scanf("%I64d%I64d", &l, &r);
        if(l > r) swap(l, r);
        ll re = solve(r) - solve(l - 1);
        printf("%I64d\n", re);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值