結果

問題 No.260 世界のなんとか3
ユーザー koba-e964koba-e964
提出日時 2017-01-12 01:08:44
言語 Rust
(1.72.1)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 2,920 bytes
コンパイル時間 3,258 ms
コンパイル使用メモリ 143,624 KB
実行使用メモリ 16,664 KB
最終ジャッジ日時 2023-08-23 08:35:23
合計ジャッジ時間 7,330 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 239 ms
16,624 KB
testcase_04 AC 239 ms
16,664 KB
testcase_05 AC 45 ms
7,216 KB
testcase_06 AC 29 ms
5,464 KB
testcase_07 AC 162 ms
14,424 KB
testcase_08 AC 112 ms
14,552 KB
testcase_09 AC 61 ms
8,272 KB
testcase_10 AC 175 ms
13,208 KB
testcase_11 AC 171 ms
15,224 KB
testcase_12 AC 100 ms
12,244 KB
testcase_13 AC 28 ms
4,376 KB
testcase_14 AC 154 ms
14,432 KB
testcase_15 AC 38 ms
4,720 KB
testcase_16 AC 131 ms
11,964 KB
testcase_17 AC 106 ms
9,356 KB
testcase_18 AC 102 ms
8,520 KB
testcase_19 AC 132 ms
14,304 KB
testcase_20 AC 92 ms
9,864 KB
testcase_21 AC 83 ms
10,652 KB
testcase_22 AC 151 ms
12,488 KB
testcase_23 AC 14 ms
4,380 KB
testcase_24 AC 118 ms
13,284 KB
testcase_25 AC 143 ms
16,476 KB
testcase_26 AC 98 ms
16,492 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 239 ms
16,584 KB
testcase_29 AC 285 ms
16,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::*;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    return ret;
}
fn get_word() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.is_err() || res.ok().unwrap() == 0 || u8b[0] <= ' ' as u8 {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = std::string::String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}
fn parse<T: std::str::FromStr>(s: &str) -> T { s.parse::<T>().ok().unwrap() }

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { parse(&get_word()) }

const MOD: i64 = 1_000_000_007;

// http://kmjp.hatenablog.jp/entry/2015/08/03/0930
fn solve(a: Vec<i64>) -> i64 {
    let n = a.len();
    let mut dp = vec![vec![vec![vec![vec![0_i64; 8]; 3]; 2]; 2]; n + 1];
    dp[0][0][0][0][0] = 1;
    for i in 0 .. n {
        for more in 0 .. 2 {
            for i3 in 0 .. 2 {
                for m3 in 0 .. 3 {
                    for m8 in 0 .. 8 {
                        for x in 0 .. 10 {
                            if more == 0 && x > a[i] as usize { continue; }
                            let delta = dp[i][more][i3][m3][m8];
                            let ret = &mut dp[i + 1]
                                [more | (if x < a[i] as usize { 1 } else { 0 })]
                                [i3 | (if x == 3 { 1 } else { 0 })]
                                [(m3 * 10 + x) % 3]
                                [(m8 * 10 + x) % 8];
                            *ret += delta;
                            *ret %= MOD;
                        }
                    }
                }
            }
        }
    }
    let mut tot: i64 = 0;
    for more in 0 .. 2 {
        for i3 in 0 .. 2 {
            for m3 in 0 .. 3 {
                for m8 in 0 .. 8 {
                    if (i3 == 1 || m3 == 0) && m8 != 0 {
                        tot = (tot + dp[n][more][i3][m3][m8]) % MOD;
                    }
                }
            }
        }
    }
    tot
}

fn dec(mut a: Vec<i64>) -> Vec<i64> {
    let n = a.len();
    for i in (0 .. n).rev() {
        match a[i] {
            0 => a[i] = 9,
            _ => {
                a[i] -= 1;
                return a;
            }
        }
    }
    panic!();
}

fn main() {
    let a: Vec<i64> = get_word().chars()
        .map(|c| (c as u8 - 48) as i64).collect();
    let b: Vec<i64> = get_word().chars()
        .map(|c| (c as u8 - 48) as i64).collect();
    let ares = solve(dec(a));
    let bres = solve(b);
    println!("{}", (bres - ares + MOD) % MOD);
}
0