結果

問題 No.260 世界のなんとか3
ユーザー ntk-ta01ntk-ta01
提出日時 2021-07-05 17:24:56
言語 Rust
(1.77.0)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 3,031 bytes
コンパイル時間 1,208 ms
コンパイル使用メモリ 155,836 KB
実行使用メモリ 12,772 KB
最終ジャッジ日時 2023-09-14 04:12:32
合計ジャッジ時間 5,840 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 259 ms
12,684 KB
testcase_04 AC 261 ms
12,772 KB
testcase_05 AC 49 ms
5,872 KB
testcase_06 AC 34 ms
4,576 KB
testcase_07 AC 175 ms
11,160 KB
testcase_08 AC 121 ms
11,424 KB
testcase_09 AC 67 ms
6,332 KB
testcase_10 AC 194 ms
10,364 KB
testcase_11 AC 186 ms
11,972 KB
testcase_12 AC 110 ms
9,592 KB
testcase_13 AC 31 ms
4,376 KB
testcase_14 AC 169 ms
11,176 KB
testcase_15 AC 43 ms
4,380 KB
testcase_16 AC 145 ms
9,312 KB
testcase_17 AC 115 ms
7,464 KB
testcase_18 AC 111 ms
6,712 KB
testcase_19 AC 146 ms
11,084 KB
testcase_20 AC 102 ms
7,728 KB
testcase_21 AC 90 ms
8,336 KB
testcase_22 AC 164 ms
9,624 KB
testcase_23 AC 15 ms
4,376 KB
testcase_24 AC 128 ms
10,408 KB
testcase_25 AC 162 ms
12,764 KB
testcase_26 AC 99 ms
12,756 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 196 ms
12,740 KB
testcase_29 AC 321 ms
12,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

fn main() {
    input! {
        a: String,
        b: String,
    }
    const MOD: i32 = 1_000_000_007;
    println!("{}", (digit_dp(b, false) - digit_dp(a, true) + MOD) % MOD);
}

fn digit_dp(s: String, less_only: bool) -> i32 {
    const MOD: i32 = 1_000_000_007;
    let mut ds = vec![];
    for c in s.chars() {
        let n = c as i32 - 48;
        ds.push(n);
    }
    let mut dp = vec![vec![vec![vec![vec![0; 8]; 3]; 2]; 2]; ds.len() + 1];
    // dp[i][is_less][has_3][mod3][mod8]
    dp[0][0][0][0][0] = 1;
    for i in 0..ds.len() {
        for j in 0..=1 {
            for k in 0..=1 {
                for m in 0..3 {
                    for n in 0..8 {
                        let lim = if j == 1 { 9 } else { ds[i] };
                        for d in 0..=lim {
                            let is_less: usize = if j == 1 || d < lim { 1 } else { 0 };
                            let has_3: usize = if k == 1 || d == 3 { 1 } else { 0 };
                            let mod_3: usize = (m + d as usize) % 3;
                            let mod_8: usize = (n * 10 + d as usize) % 8;
                            dp[i + 1][is_less][has_3][mod_3][mod_8] += dp[i][j][k][m][n];
                            dp[i + 1][is_less][has_3][mod_3][mod_8] %= MOD;
                        }
                    }
                }
            }
        }
    }
    let mut answer = 0;
    for j in 0..=1 {
        if less_only && j == 0 {
            continue;
        }
        for k in 0..=1 {
            for m in 0..=2 {
                for n in 0..8 {
                    if (k == 1 || m == 0) && n != 0 {
                        answer += dp[ds.len()][j][k][m][n];
                        answer %= MOD;
                    }
                }
            }
        }
    }
    answer
}
0