• CF55D Beautiful numbers


    链接:https://www.luogu.org/problemnew/show/CF55D

    题意翻译

    题目描述

    Volodya是一个很皮的男♂孩。他认为一个能被它自己的每一位数上的数整除的数是很妙的。我们先忽略他的想法的正确性(如需证明请百度“神奇海螺”),只回答在l到r之间有多少个很妙的数字。

    输入输出格式

    输入:总共有t个询问:

    第一行:t;

    接下来t行:每行两个数l和r。

    注意:请勿使用%lld读写长整型(虽然我也不知道为什么),请优先使用cin(或者是%I64d)。

    输出:t行,每行为一个询问的答案。

    题目描述

    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.

    输入输出格式

    输入格式:

    The first line of the input contains the number of cases tt ( 1<=t<=101<=t<=10 ). Each of the next tt lines contains two natural numbers l_{i}li and r_{i}ri ( 1<=l_{i}<=r_{i}<=9·10^{18}1<=li<=ri<=91018 ).

    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 should contain tt numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from l_{i}li to r_{i}ri , inclusively).

    输入输出样例

    输入样例#1: 复制
    1
    1 9
    
    输出样例#1: 复制
    9
    
    输入样例#2: 复制
    1
    12 15
    
    输出样例#2: 复制
    2

    题解:数位dp
    dp不用每次清零
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    const int M = 2530;
    ll dp[20][50][M];
    int digit[20], cnt, tot, bg[M];
    int gcd(int a, int b){ return b ? gcd(b, a%b) : a;}
    
    void init(){
        for(int i = 1; i <= 2520; i++)
            if(2520 % i == 0) bg[i] = ++cnt; 
    }
    
    ll dfs(int dep, bool f, int t, int lcm, int yu){
        if(!dep) return ! (yu % lcm);
        if(!f && dp[dep][bg[lcm]][yu] != -1) return dp[dep][bg[lcm]][yu];
        int i = f ? digit[dep] : 9;
        ll tmp = 0;
        for(; i >= 0; i--){
            int now = lcm;
            if(i) now *= i / gcd(i, lcm);
            tmp += dfs(dep - 1, f & (i == digit[dep]), i, now, (yu*10 + i) % 2520);
        }
        if(!f)dp[dep][bg[lcm]][yu] = tmp;
        return tmp;
    }
    
    
    ll get(ll a){
        tot = 0;
        while(a){
            digit[++tot] = a%10;
            a /= 10;
        }
        ll ans = dfs(tot, 1, 0, 1, 0);
        return ans;
    }
    
    
    int main(){
        int T;
        scanf("%d", &T);
        init();
        memset(dp, -1, sizeof(dp));
        while(T--){
            ll L, R;
            scanf("%I64d%I64d", &L, &R);
            ll ans1 = get(R);
            ll ans2 = get(L - 1);
            //cout<<ans1<<" "<<ans2;
            printf("%I64d
    ", ans1 - ans2);
        }
    }
    View Code
  • 相关阅读:
    组合模式
    迭代器模式
    命令模式
    装饰者模式
    观察者模式
    策略模式
    适配器模式和外观模式
    Servlet
    Java 递归
    Java 反射
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/9396013.html
Copyright © 2020-2023  润新知