結果
問題 | No.3 ビットすごろく |
ユーザー |
|
提出日時 | 2020-10-08 17:09:27 |
言語 | Rust (1.83.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,669 bytes |
コンパイル時間 | 13,321 ms |
コンパイル使用メモリ | 376,936 KB |
実行使用メモリ | 7,296 KB |
最終ジャッジ日時 | 2024-07-20 06:00:16 |
合計ジャッジ時間 | 30,360 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | WA * 5 OLE * 6 -- * 22 |
ソースコード
use std::io::{stdin, Read, StdinLock};use std::str::FromStr;struct Input {n: usize,}fn read_input(cin_lock: &mut StdinLock) -> Input {let n = next_token(cin_lock);Input { n }}fn f(n: u32, stack: i32, mut check_points: Vec<i32>) -> Vec<i32> {println!("{:?}", check_points);if check_points.len() <= n as usize {return check_points;}if check_points[n as usize] >= 0 {return check_points;}check_points[n as usize] = stack;let check_points = f(n + n.count_ones(), stack + 1, check_points);let check_points = f(n - n.count_ones(), stack + 1, check_points);return check_points;}fn solve(input: Input) -> i32 {let check_points = vec![-1; input.n + 1];return f(1, 1, check_points)[input.n];}#[cfg(test)]mod tests {use super::*;#[test]fn test_solve() {assert_eq!(solve(Input { n: 1 }), 1);assert_eq!(solve(Input { n: 2 }), 2);assert_eq!(solve(Input { n: 3 }), 3);assert_eq!(solve(Input { n: 4 }), -1);assert_eq!(solve(Input { n: 5 }), 4);assert_eq!(solve(Input { n: 11 }), 9);assert_eq!(solve(Input { n: 300 }), 9);}}fn next_token<T: FromStr>(cin_lock: &mut StdinLock) -> T {cin_lock.by_ref().bytes().map(|c| c.unwrap() as char).skip_while(|c| c.is_whitespace()).take_while(|c| !c.is_whitespace()).collect::<String>().parse::<T>().ok().unwrap()}fn main() {let cin = stdin();let mut cin_lock = cin.lock();let input = read_input(&mut cin_lock);println!("{}", solve(input));}