結果

問題 No.1085 桁和の桁和
ユーザー phsplsphspls
提出日時 2023-01-05 23:52:44
言語 Rust
(1.77.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,577 bytes
コンパイル時間 13,718 ms
コンパイル使用メモリ 397,616 KB
実行使用メモリ 14,080 KB
最終ジャッジ日時 2024-05-07 03:35:12
合計ジャッジ時間 15,782 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 4 ms
6,944 KB
testcase_14 AC 12 ms
6,944 KB
testcase_15 AC 27 ms
7,296 KB
testcase_16 AC 27 ms
7,040 KB
testcase_17 AC 14 ms
6,944 KB
testcase_18 AC 8 ms
6,940 KB
testcase_19 AC 25 ms
6,944 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 16 ms
6,940 KB
testcase_22 AC 22 ms
6,940 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 14 ms
6,940 KB
testcase_26 AC 28 ms
7,552 KB
testcase_27 AC 23 ms
6,944 KB
testcase_28 AC 31 ms
8,192 KB
testcase_29 AC 17 ms
6,940 KB
testcase_30 AC 16 ms
6,940 KB
testcase_31 AC 27 ms
7,040 KB
testcase_32 AC 19 ms
6,944 KB
testcase_33 AC 61 ms
13,924 KB
testcase_34 AC 60 ms
13,984 KB
testcase_35 AC 61 ms
14,080 KB
testcase_36 AC 61 ms
13,952 KB
testcase_37 AC 61 ms
13,952 KB
testcase_38 AC 61 ms
13,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const MOD: usize = 1e9 as usize + 7;

fn main() {
    let mut t = String::new();
    std::io::stdin().read_line(&mut t).ok();
    let t = t.trim().chars().collect::<Vec<_>>();
    let mut d = String::new();
    std::io::stdin().read_line(&mut d).ok();
    let d: usize = d.trim().parse().unwrap();

    if d == 0 {
        if t.iter().any(|&c| c != '?' && c != '0') {
            println!("0");
        } else {
            println!("1");
        }
        return;
    }
    let total = t.iter().filter(|&&c| c != '?').map(|&c| c as usize - '0' as usize).sum::<usize>();
    let total = if total == 0 { 0 } else if total % 9 == 0 { 9 } else { total % 9 };
    let cnt = t.iter().filter(|&&c| c == '?').count();
    let mut dp = vec![vec![0usize; 10]; cnt+1];
    dp[0][0] = 1;
    for i in 0..cnt {
        for j in 0..=9 {
            if dp[i][j] == 0 { continue; }
            for k in 0..=9 {
                if j == 0 && k == 0 {
                    dp[i+1][0] += dp[i][j];
                    dp[i+1][0] %= MOD;
                    continue;
                }
                let idx = (j+k) % 9;
                let idx = if idx == 0 { 9 } else { idx };
                dp[i+1][idx] += dp[i][j];
                dp[i+1][idx] %= MOD;
            }
        }
    }
    let mut result = 0usize;
    for i in 0..10 {
        let idx = if i + total > 0 {
                if (i+total) % 9 == 0 { 9 } else { (i+total)%9 }
            } else {
                0
            };
        if idx == d {
            result += dp[cnt][i];
        }
    }
    println!("{}", result);
}
0