ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CodeForces - 55D Beautiful numbers (数位dp)

2019-04-09 16:48:00  阅读:247  来源: 互联网

标签:Beautiful md lcm return ll CodeForces pos numbers include


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\n", solve(b)-solve(a-1));
    }
    return 0;
}
View Code

 

标签:Beautiful,md,lcm,return,ll,CodeForces,pos,numbers,include
来源: https://www.cnblogs.com/ZGQblogs/p/10677971.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有