結果

問題 No.2302 Carry X Times
ユーザー tenten
提出日時 2023-05-29 13:24:41
言語 Java
(openjdk 23)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 4,171 bytes
コンパイル時間 3,864 ms
コンパイル使用メモリ 84,404 KB
実行使用メモリ 39,304 KB
最終ジャッジ日時 2024-12-28 09:49:36
合計ジャッジ時間 6,408 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static int[][][][][] dp;
    static char[] inputs;
    static final int MOD = 998244353;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int t = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (t-- > 0) {
            inputs = sc.next().toCharArray();
            int x = sc.nextInt();
            dp = new int[inputs.length][2][2][][];
            int[][] ans = dfw(0, 0, 0);
            sb.append((ans[0][x] + ans[1][x]) % MOD).append("\n");
        }
        System.out.print(sb);
    }
    
    static int[][] dfw(int idx, int aType, int bType) {
        if (dp[idx][aType][bType] == null) {
            int[][] ans = new int[2][20];
            int aMax;
            if (aType == 1) {
                aMax = 9;
            } else {
                aMax = inputs[idx] - '0';
            }
            int bMax;
            if (bType == 1) {
                bMax = 9;
            } else {
                bMax = inputs[idx] - '0';
            }
            if (idx == inputs.length - 1) {
                for (int a = 0; a <= aMax; a++) {
                    for (int b = 0; b <= bMax; b++) {
                        if (a + b >= 10) {
                            ans[1][1]++;
                        } else {
                            ans[0][0]++;
                        }
                    }
                }
            } else {
                for (int a = 0; a <= aMax; a++) {
                    for (int b = 0; b <= bMax; b++) {
                        int[][] prev;
                        if (aType == 0 && a == aMax) {
                            if (bType == 0 && b == bMax) {
                                prev = dfw(idx + 1, 0, 0);
                            } else {
                                prev = dfw(idx + 1, 0, 1);
                            }
                        } else {
                            if (bType == 0 && b == bMax) {
                                prev = dfw(idx + 1, 1, 0);
                            } else {
                                prev = dfw(idx + 1, 1, 1);
                            }
                        }
                        for (int i = 0; i < 19; i++) {
                            for (int j = 0; j < 2; j++) {
                                if (a + b + j >= 10) {
                                    ans[1][i + 1] += prev[j][i];
                                    ans[1][i + 1] %= MOD;
                                } else {
                                    ans[0][i] += prev[j][i];
                                    ans[0][i] %= MOD;
                                }
                            }
                        }
                    }
                }
            }
            dp[idx][aType][bType] = ans;
        }
        return dp[idx][aType][bType];
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0