結果

問題 No.1085 桁和の桁和
ユーザー ks2m
提出日時 2020-06-20 02:33:51
言語 Java
(openjdk 23)
結果
AC  
実行時間 282 ms / 2,000 ms
コード長 956 bytes
コンパイル時間 2,738 ms
コンパイル使用メモリ 77,376 KB
実行使用メモリ 52,048 KB
最終ジャッジ日時 2024-11-06 17:41:26
合計ジャッジ時間 11,358 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		char[] t = sc.next().toCharArray();
		int d = sc.nextInt();
		sc.close();

		int sum = 0;
		int cnt = 0;
		for (int i = 0; i < t.length; i++) {
			if (t[i] == '?') {
				cnt++;
			} else {
				sum += t[i] - '0';
			}
		}

		if (d == 0) {
			if (sum == 0) {
				System.out.println(1);
			} else {
				System.out.println(0);
			}
			return;
		}

		int mod = 1000000007;
		long[][] dp = new long[cnt + 1][9];
		dp[0][0] = 1;
		for (int i = 0; i < cnt; i++) {
			for (int j = 0; j < 9; j++) {
				for (int j2 = 0; j2 < 10; j2++) {
					dp[i + 1][(j + j2) % 9] += dp[i][j];
				}
			}
			for (int j = 0; j < 9; j++) {
				dp[i + 1][j] %= mod;
			}
		}
		if (d == 9 && sum == 0) {
			System.out.println(dp[cnt][0] - 1);
			return;
		}
		sum %= 9;
		int c = (d - sum + 9) % 9;
		System.out.println(dp[cnt][c]);
	}
}
0