結果

問題 No.1085 桁和の桁和
ユーザー phspls
提出日時 2023-02-01 23:57:31
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 1,103 bytes
コンパイル時間 12,359 ms
コンパイル使用メモリ 404,124 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-07-01 16:46:10
合計ジャッジ時間 14,424 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

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();

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