結果
問題 | No.1407 Kindness |
ユーザー | ikd |
提出日時 | 2021-02-26 22:44:02 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 38 ms / 2,000 ms |
コード長 | 3,160 bytes |
コンパイル時間 | 20,446 ms |
コンパイル使用メモリ | 384,476 KB |
実行使用メモリ | 22,132 KB |
最終ジャッジ日時 | 2024-10-02 15:26:50 |
合計ジャッジ時間 | 16,200 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,248 KB |
testcase_04 | AC | 1 ms
5,248 KB |
testcase_05 | AC | 0 ms
5,248 KB |
testcase_06 | AC | 1 ms
5,248 KB |
testcase_07 | AC | 1 ms
5,248 KB |
testcase_08 | AC | 1 ms
5,248 KB |
testcase_09 | AC | 1 ms
5,248 KB |
testcase_10 | AC | 1 ms
5,248 KB |
testcase_11 | AC | 1 ms
5,248 KB |
testcase_12 | AC | 38 ms
21,052 KB |
testcase_13 | AC | 10 ms
6,448 KB |
testcase_14 | AC | 10 ms
7,136 KB |
testcase_15 | AC | 33 ms
20,336 KB |
testcase_16 | AC | 18 ms
11,948 KB |
testcase_17 | AC | 16 ms
10,508 KB |
testcase_18 | AC | 16 ms
10,176 KB |
testcase_19 | AC | 32 ms
19,424 KB |
testcase_20 | AC | 27 ms
17,952 KB |
testcase_21 | AC | 3 ms
5,248 KB |
testcase_22 | AC | 4 ms
5,248 KB |
testcase_23 | AC | 25 ms
17,528 KB |
testcase_24 | AC | 37 ms
21,996 KB |
testcase_25 | AC | 14 ms
10,176 KB |
testcase_26 | AC | 17 ms
11,528 KB |
testcase_27 | AC | 3 ms
5,248 KB |
testcase_28 | AC | 32 ms
20,016 KB |
testcase_29 | AC | 2 ms
5,248 KB |
testcase_30 | AC | 36 ms
21,012 KB |
testcase_31 | AC | 9 ms
6,464 KB |
testcase_32 | AC | 33 ms
19,592 KB |
testcase_33 | AC | 11 ms
7,172 KB |
testcase_34 | AC | 21 ms
12,576 KB |
testcase_35 | AC | 9 ms
6,820 KB |
testcase_36 | AC | 2 ms
5,248 KB |
testcase_37 | AC | 35 ms
22,132 KB |
ソースコード
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() } }