AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 吐槽
  • App
  • 登录/注册

AcWing 4636. 美丽数

作者: 作者的头像   炽热的 ,  2023-03-15 22:45:05 ,  所有人可见 ,  阅读 42


1


#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long i64;

int len;
int nums[20];
int mp[2530], val[60], idx;
i64 f[20][2530][60];

int __lcm(int a, int b) {
    return a * b / __gcd(a, b);
}

i64 dfs(int pos, int Mod, int lcm, int limit) {
    if (!pos) return Mod % val[lcm] == 0;
    if (!limit && ~f[pos][Mod][lcm]) return f[pos][Mod][lcm];
    int up = limit ? nums[pos] : 9;
    i64 res = 0;
    for (int i = 0; i <= up; i ++ ) 
        res += dfs(pos - 1, (Mod * 10 + i) % 2520, i ? mp[__lcm(val[lcm], i)] : lcm, limit && i == up);
    return limit ? res : f[pos][Mod][lcm] = res;
}

i64 calc(i64 x) {
    len = 0;
    while (x) nums[ ++ len] = x % 10, x /= 10;
    return dfs(len, 0, 1, 1);
}

void solve() {
    i64 l, r;
    cin >> l >> r;
    cout << calc(r) - calc(l - 1) << endl;
}

int main() {
    memset(f, -1, sizeof f);
    for (int i = 1; i <= 2520; i ++ ) 
        if (2520 % i == 0) 
            mp[i] = ++ idx, val[idx] = i;

    int _; cin >> _;
    while (_ -- ) solve();
    return 0;
}

0 评论

你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息