結果

問題 No.3 ビットすごろく
ユーザー frozenlibfrozenlib
提出日時 2018-06-14 14:38:17
言語 Rust
(1.77.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,740 bytes
コンパイル時間 2,817 ms
コンパイル使用メモリ 149,092 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 01:02:15
合計ジャッジ時間 1,872 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;
use std::str::FromStr;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = ReadEx::from(i);
    let n = i.read::<usize>();
    let r = solve(n);
    if r == usize::max_value() {
        writeln!(o, "-1")
    } else {
        writeln!(o, "{}", r)
    }.unwrap();
}
fn solve(n: usize) -> usize {
    fn bits(mut n: usize) -> usize {
        let mut c = 0;
        while n != 0 {
            if n % 2 == 1 {
                c += 1;
            }
            n /= 2;
        }
        c
    }
    let mut buf = vec![usize::max_value(); n + 1];
    let mut q = Vec::new();
    q.push(1);
    buf[0] = 0;
    buf[1] = 1;
    while let Some(i) = q.pop() {
        let mut update = |i0, i1| {
            let next = buf[i0] + 1;
            if buf[i1] > next {
                buf[i1] = next;
                q.push(i1);
            }
        };
        let b = bits(i);
        if b < i {
            update(i, i - b);
        }
        if i + b <= n {
            update(i, i + b);
        }
    }
    buf[n]
}

mod utils {
    use super::*;

    pub struct ReadEx<R: BufRead> {
        r: R,
        s: String,
    }

    impl<R: BufRead> ReadEx<R> {
        pub fn from(r: R) -> Self {
            ReadEx {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_line().parse().ok().unwrap()
        }
    }
}
0