CodeForces55D

本文介绍了一种算法,用于计算特定范围内能被其所有非零数字整除的整数数量。通过使用动态规划和数学方法,文章详细展示了如何高效地解决这个问题。

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).

Example
Input
1
1 9
Output
9
Input
1
12 15
Output
2

用每个数除以数位的LCM
但是会发现每个数%2520就可以存状态了
2520是1到9的LCM

坑点 0算一个

#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<memory.h>
#include<algorithm>
using namespace std;
long long shi[21];
long long dp[21][7000][100];
long long mp[9000000];
int gcd(int a, int b)
{
    if (b == 0) return a;
    return gcd(b, a%b);
}
long long lcm(long long q, long long w)
{
    return q*w / gcd(q, w);
}
long long bte[30];
long long qw = 0;
long long dfs(long long weishu, long long biaoji, long long benshen, long long quanti)
{
    if (!mp[quanti])mp[quanti] = ++qw;
    if (weishu == 0)
    {
    //  if (qiandao == 0)return 0;
        if (benshen%quanti)return 0;
        return 1;
    }
    if (biaoji&&dp[weishu][benshen][mp[quanti]] != -1)return dp[weishu][benshen][mp[quanti]];
    long long da = 0;
    long long bianjie = biaoji ? 9 : bte[weishu];
    for (int a = 0;a <= bianjie;a++)
    {
        int xyb = benshen * 10 + a;
        xyb %= 2520;
        int xyq = quanti;
        if (a)xyq = lcm(quanti, a);
        da += dfs(weishu - 1, biaoji || a < bianjie,xyb, xyq);
    }
    if (biaoji)dp[weishu][benshen][mp[quanti]] = da;
    return da;
}
long long jiejue(long long q)
{
    if (q == 0)return 1;
    if (q < 0)return 0;
    //if (q < 10)return q;
    long long y = q;
    bte[0] = 0;
    while (y)
    {
        bte[++bte[0]] = y % 10;
        y /= 10;
    }
    return dfs(bte[0], 0, 0, 1);
}
int main()
{
#define int long long
    int T;
    cin >> T;
    shi[0] = 1;
    memset(dp, -1, sizeof(dp));
    for (int a = 1;a <= 20;a++)shi[a] = shi[a - 1] * 10;
    while (T--)
    {
        int n, m;
        cin >> n >> m;
        int yuy = jiejue(m) - jiejue(n - 1);
        cout << yuy << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值