码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces - 55D - Beautiful numbers(数位DP,离散化)

时间:2019-12-06 00:16:45      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:http   链接   div   --   set   codeforce   main   hat   ret   

链接:

https://vjudge.net/problem/CodeForces-55D

题意:

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.

思路:

数位DP,但是数的范围太大不能直接记录,先优化。
我们有可以推出\(sum \% (n*x) \% x = sum \% x\)
证明如下:
\(sum = k*x+b\)
\((k * x+b)\% (n * x) \% x\)
\(k = k_a*n+k_b\)
\(((k_a*n+k_b)*x+b) \% (n*x) \% x\)
\((k_a*n*x+k_b*x+b) \% (n*x) \% x\)
\((k_b*x+b) \% x\)
\(b = (k*x+b) \% x = b\)
所以我们可以把值先模nx,取nx = 2520(1~9的lcm),
令Dp(i, j, k),表示i位置,模 n*x为j,k等于各位的lcm,(因为n%每个数都为0 = n%lcm = 0)
考虑lcm的个数,如果用2520来记录会爆内存,考虑lcm的值,每次都是两个数相乘/gcd,同时每个最多为9,则每个lcm都是2520的约数,枚举约数离散化。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;

LL Dp[30][2600][50];
int Hash[2600];
int dig[30];

LL Gcd(LL a, LL b)
{
    if (b == 0)
        return a;
    return Gcd(b, a%b);
}

LL Dfs(int pos, LL num, int lcm, bool lim)
{
    if (pos == -1)
        return num%lcm == 0;
    if (!lim && Dp[pos][num][Hash[lcm]] != -1)
        return Dp[pos][num][Hash[lcm]];
    int up = lim ? dig[pos] : 9;
    LL cnt = 0;
    for (int i = 0;i <= up;i++)
        cnt += Dfs(pos-1, (num*10+i)%2520, i ? lcm*i/Gcd(lcm, i) : lcm, lim && i == up);
    if (!lim)
        Dp[pos][num][Hash[lcm]] = cnt;
    return cnt;
}

LL Solve(LL x)
{
    int p = 0;
    while(x)
    {
        dig[p++] = x%10;
        x /= 10;
    }
    return Dfs(p-1, 0, 1, true);
}

int main()
{
    // freopen("test.in", "r", stdin);
    int cnt = 0;
    for (int i = 1;i <= 2520;i++)
    {
        if (2520%i == 0)
            Hash[i] = ++cnt;
    }
    memset(Dp, -1, sizeof(Dp));
    int t;
    scanf("%d", &t);
    while(t--)
    {
        LL a, b;
        scanf("%I64d %I64d", &a, &b);
        printf("%I64d\n", Solve(b)-Solve(a-1));
    }

    return 0;
}

CodeForces - 55D - Beautiful numbers(数位DP,离散化)

标签:http   链接   div   --   set   codeforce   main   hat   ret   

原文地址:https://www.cnblogs.com/YDDDD/p/11992755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!