結果

問題 No.2302 Carry X Times
ユーザー tentententen
提出日時 2023-05-29 13:24:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 4,171 bytes
コンパイル時間 2,354 ms
コンパイル使用メモリ 88,112 KB
実行使用メモリ 53,044 KB
最終ジャッジ日時 2023-08-27 23:40:32
合計ジャッジ時間 6,371 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
52,404 KB
testcase_01 AC 80 ms
52,532 KB
testcase_02 AC 69 ms
52,352 KB
testcase_03 AC 43 ms
49,540 KB
testcase_04 AC 43 ms
47,388 KB
testcase_05 AC 45 ms
50,196 KB
testcase_06 AC 46 ms
50,856 KB
testcase_07 AC 46 ms
50,732 KB
testcase_08 AC 46 ms
50,856 KB
testcase_09 AC 46 ms
50,260 KB
testcase_10 AC 90 ms
52,652 KB
testcase_11 AC 91 ms
52,612 KB
testcase_12 AC 92 ms
52,672 KB
testcase_13 AC 92 ms
52,296 KB
testcase_14 AC 88 ms
52,816 KB
testcase_15 AC 93 ms
52,616 KB
testcase_16 AC 91 ms
52,852 KB
testcase_17 AC 93 ms
52,820 KB
testcase_18 AC 89 ms
52,464 KB
testcase_19 AC 92 ms
52,740 KB
testcase_20 AC 90 ms
52,648 KB
testcase_21 AC 91 ms
52,892 KB
testcase_22 AC 92 ms
52,904 KB
testcase_23 AC 92 ms
52,780 KB
testcase_24 AC 93 ms
53,044 KB
権限があれば一括ダウンロードができます

ソースコード

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