AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

神奇数(前导零限制)

作者: 作者的头像   不知名的fE ,  2025-06-11 23:55:43 · 河北 ,  所有人可见 ,  阅读 2


0


神奇数

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static final int N = 210, mod = 998244353;
    static int[] a = new int[N];
    static long[][][] f = new long[N][10][2];
    static int n;

    //is_num避开非数的干扰
    static long dfs(int u, int lim, boolean is_num, int op, int yu) {
        if (u == n) {
            if (!is_num) return 0;
            if (lim == 1 && op > a[u]) return 0;
            return (yu % op == 0) ? 1 : 0;
        }
        if (is_num && f[u][yu][lim] != -1)
            return f[u][yu][lim];

        long res = 0;
        int up = (lim == 1) ? a[u] : 9;
        for (int i = 0; i <= up; i++) {
            res = (res + dfs(u + 1, lim & (i == up ? 1 : 0), is_num || i != 0, op, (yu + i) % op)) % mod;
        }

        if (is_num) f[u][yu][lim] = res;

        return res;
    }

    static void init() {
        for (int i = 0; i < N; i++)
            for (int j = 0; j < 10; j++) {
                Arrays.fill(f[i][j], -1);
            }
    }

    static long get(String t) {
        n = t.length();
        for (int i = 1; i <= n; i++) a[i] = t.charAt(i - 1) - '0';
        long res = 0;
        for (int op = 1; op <= 9; op++) {
            init();
            res = (res + dfs(1, 1, false, op, 0)) % mod;
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        long ans = -get(s) + check();
        ans += get(sc.nextLine());

        System.out.println((ans % mod + mod) % mod);
    }

    static int check() {
        if (a[n] == 0) return 0;
        int sum = 0;
        for (int i = 1; i < n; i++) sum += a[i];
        return sum % a[n] == 0 ? 1 : 0;
    }
}

0 评论

App 内打开
你确定删除吗?
1024
x

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