zoukankan      html  css  js  c++  java
  • CodeForces

    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

    题意:
    问所给区间内,有多少个数字,可以整除数位上的每一个正整数。
    思路:
    数位上可能的九个数字,最大的lcm就是2520.
    dfs中的参数设为除以2520的模数md,和之前数位的lcm即可。
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    
    #define fuck(x) cout<<#x<<" = "<<x<<endl;
    #define ls (t<<1)
    #define rs ((t<<1)+1)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 100086;
    const int inf = 2.1e9;
    const ll Inf = 999999999999999999;
    const int mod = 1000000007;
    const double eps = 1e-6;
    const double pi = acos(-1);
    vector<int> sca;
    int bit[20];
    ll dp[20][50][2600];
    
    ll LCM(ll a, ll b) {
        if (b == 0) { return a; }
        return a * b / __gcd(a, b);
    }
    
    ll dfs(int pos, ll lcm, ll md, bool limit) {
        int t = lower_bound(sca.begin(), sca.end(), lcm) - sca.begin();
        if (pos == -1) {
            if (md % lcm == 0) { return 1; }
            else { return 0; }
        }
        if (!limit&&dp[pos][t][md] != -1) { return dp[pos][t][md]; }
        ll ans = 0;
        int up = limit ? bit[pos] : 9;
        for (int i = 0; i <= up; i++) {
            ans += dfs(pos - 1, LCM(lcm, i), (md * 10 + i) % 2520, limit && i == up);
        }
        if (!limit) {
            dp[pos][t][md] = ans;
        }
        return ans;
    }
    
    ll solve(ll a) {
        if(a==0){ return 0;}
        int pos = 0;
        while (a) {
            bit[pos++] = a % 10;
            a /= 10;
        }
        return dfs(pos-1, 1, 0, true)-1;
    }
    
    int main() {
        sca.push_back(0);
        for (int i = 1; i <= 2520; i++) {
            if (2520 % i == 0) { sca.push_back(i); }
        }
        memset(dp, -1, sizeof(dp));
        int T;
        scanf("%d", &T);
        while (T--) {
            ll a, b;
            scanf("%lld%lld", &a, &b);
            printf("%lld
    ", solve(b)-solve(a-1));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    [luogu] P1440 求m区间内的最小值
    [NOI2014]起床困难综合症
    [SDOI2009]地图复原
    [USACO08JAN] Cow Contest
    【洛谷P5049】旅行(数据加强版)
    【NOIP2015】真题回顾
    【NOIP2014】真题回顾
    【UVA11987】Almost Union-Find
    【UVA11988】破损的键盘
    【UVA11134】传说中的车
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/10677971.html
Copyright © 2011-2022 走看看