結果

問題 No.1407 Kindness
ユーザー ikdikd
提出日時 2021-02-26 22:44:02
言語 Rust
(1.77.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 3,160 bytes
コンパイル時間 1,517 ms
コンパイル使用メモリ 184,028 KB
実行使用メモリ 22,128 KB
最終ジャッジ日時 2024-04-10 13:23:08
合計ジャッジ時間 3,121 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 0 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 0 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 0 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 0 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 34 ms
20,988 KB
testcase_13 AC 8 ms
6,944 KB
testcase_14 AC 10 ms
7,072 KB
testcase_15 AC 30 ms
20,276 KB
testcase_16 AC 19 ms
12,064 KB
testcase_17 AC 14 ms
10,264 KB
testcase_18 AC 14 ms
10,176 KB
testcase_19 AC 28 ms
19,364 KB
testcase_20 AC 26 ms
17,932 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 24 ms
17,512 KB
testcase_24 AC 36 ms
21,968 KB
testcase_25 AC 13 ms
10,060 KB
testcase_26 AC 18 ms
11,528 KB
testcase_27 AC 3 ms
6,940 KB
testcase_28 AC 31 ms
20,056 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 34 ms
20,984 KB
testcase_31 AC 9 ms
6,944 KB
testcase_32 AC 29 ms
19,516 KB
testcase_33 AC 10 ms
7,052 KB
testcase_34 AC 19 ms
12,696 KB
testcase_35 AC 9 ms
6,940 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 32 ms
22,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const MO: u64 = 1_000_000_000 + 7;
use std::collections::HashMap;
fn solve(s: &[u64], carry: bool, memo: &mut HashMap<(usize, bool), u64>) -> u64 {
    let len = s.len();
    if len == 1 {
        let u = s[0] - carry as u64;
        return 1 + (1..=u).sum::<u64>() % MO;
    }
    if let Some(&ans) = memo.get(&(len, carry)) {
        return ans;
    }
    let t = &s[..(len - 1)];
    let ans = match (s.last(), carry) {
        (Some(0), true) => {
            let ans = (0..=9).sum::<u64>() * solve(t, true, memo);
            (ans + 1) % MO
        }
        (Some(&d), carry) => {
            let d = d - carry as u64;
            let ans = (0..=d).sum::<u64>() * solve(t, false, memo)
                + ((d + 1)..=9).sum::<u64>() * solve(t, true, memo);
            (ans + 1) % MO
        }
        _ => unreachable!(),
    };
    memo.insert((len, carry), ans);
    ans
}

fn main() {
    let stdin = std::io::stdin();
    let mut rd = ProconReader::new(stdin.lock());

    let s: Vec<char> = rd.get_chars();
    let s: Vec<u64> = s.into_iter().map(|d| d as u64 - '0' as u64).collect();

    let mut memo = HashMap::new();
    let ans = solve(&s, false, &mut memo);
    println!("{}", (MO + ans - 1) % MO);
}

pub struct ProconReader<R> {
    r: R,
    l: String,
    i: usize,
}

impl<R: std::io::BufRead> ProconReader<R> {
    pub fn new(reader: R) -> Self {
        Self {
            r: reader,
            l: String::new(),
            i: 0,
        }
    }
    pub fn get<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        <T as std::str::FromStr>::Err: std::fmt::Debug,
    {
        self.skip_blanks();
        assert!(self.i < self.l.len()); // remain some character
        assert_ne!(&self.l[self.i..=self.i], " ");
        let rest = &self.l[self.i..];
        let len = rest.find(' ').unwrap_or_else(|| rest.len());
        // parse self.l[self.i..(self.i + len)]
        let val = rest[..len]
            .parse()
            .unwrap_or_else(|e| panic!("{:?}, attempt to read `{}`", e, rest));
        self.i += len;
        val
    }
    fn skip_blanks(&mut self) {
        loop {
            match self.l[self.i..].find(|ch| ch != ' ') {
                Some(j) => {
                    self.i += j;
                    break;
                }
                None => {
                    let mut buf = String::new();
                    let num_bytes = self
                        .r
                        .read_line(&mut buf)
                        .unwrap_or_else(|_| panic!("invalid UTF-8"));
                    assert!(num_bytes > 0, "reached EOF :(");
                    self.l = buf
                        .trim_end_matches('\n')
                        .trim_end_matches('\r')
                        .to_string();
                    self.i = 0;
                }
            }
        }
    }
    pub fn get_vec<T>(&mut self, n: usize) -> Vec<T>
    where
        T: std::str::FromStr,
        <T as std::str::FromStr>::Err: std::fmt::Debug,
    {
        (0..n).map(|_| self.get()).collect()
    }
    pub fn get_chars(&mut self) -> Vec<char> {
        self.get::<String>().chars().collect()
    }
}
0