結果

問題 No.1417 100の倍数かつ正整数(2)
ユーザー phspls
提出日時 2022-11-23 00:53:31
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 106 ms / 3,000 ms
コード長 1,882 bytes
コンパイル時間 18,886 ms
コンパイル使用メモリ 378,236 KB
実行使用メモリ 19,968 KB
最終ジャッジ日時 2024-09-23 07:40:43
合計ジャッジ時間 14,919 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n = n.trim().chars().map(|c| c as usize - '0' as usize).collect::<Vec<_>>();

    let limit = n.len();
    let mut edp = vec![vec![vec![vec![vec![0usize; 3]; 3]; 2]; 2]; limit];
    let mut ldp = vec![vec![vec![vec![vec![0usize; 3]; 3]; 2]; 2]; limit];
    let cnt2s = vec![0, 0, 1, 0, 2, 0, 1, 0, 2, 0];
    let cnt5s = vec![0, 0, 0, 0, 0, 1, 0, 0, 0, 0];
    edp[0][0][0][cnt2s[n[0]]][cnt5s[n[0]]] = 1;
    for i in 0..n[0] {
        ldp[0][if i==0 { 1 } else { 0 }][0][cnt2s[i]][cnt5s[i]] += 1;
    }
    for i in 0..limit-1 {
        for leading0 in 0..2 {
            for cons0 in 0..2 {
                for cnt2 in 0..3 {
                    for cnt5 in 0..3 {
                        for j in 0..10 {
                            let nleading0 = if leading0 == 1 && j == 0 { 1 } else { 0 };
                            let ncons0 = if cons0 == 1 || (leading0 == 0 && j == 0) { 1 } else { 0 };
                            let ncnt2 = 2.min(cnt2 + cnt2s[j]);
                            let ncnt5 = 2.min(cnt5 + cnt5s[j]);
                            if j == n[i+1] {
                                edp[i+1][nleading0][ncons0][ncnt2][ncnt5] += edp[i][leading0][cons0][cnt2][cnt5];
                            } else if j < n[i+1] {
                                ldp[i+1][nleading0][ncons0][ncnt2][ncnt5] += edp[i][leading0][cons0][cnt2][cnt5];
                            }
                            ldp[i+1][nleading0][ncons0][ncnt2][ncnt5] += ldp[i][leading0][cons0][cnt2][cnt5];
                            ldp[i+1][nleading0][ncons0][ncnt2][ncnt5] %= MOD;
                        }
                    }
                }
            }
        }
    }
    println!("{}", (edp[limit-1][0][0][2][2] + ldp[limit-1][0][0][2][2]) % MOD);
}
0