結果

問題 No.3 ビットすごろく
ユーザー cra77756176cra77756176
提出日時 2022-11-17 23:19:44
言語 Rust
(1.77.0)
結果
AC  
実行時間 633 ms / 5,000 ms
コード長 1,275 bytes
コンパイル時間 1,791 ms
コンパイル使用メモリ 188,212 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 13:18:21
合計ジャッジ時間 10,841 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 43 ms
4,348 KB
testcase_04 AC 6 ms
4,348 KB
testcase_05 AC 195 ms
4,348 KB
testcase_06 AC 54 ms
4,348 KB
testcase_07 AC 20 ms
4,348 KB
testcase_08 AC 118 ms
4,348 KB
testcase_09 AC 309 ms
4,348 KB
testcase_10 AC 413 ms
4,348 KB
testcase_11 AC 264 ms
4,348 KB
testcase_12 AC 183 ms
4,348 KB
testcase_13 AC 31 ms
4,348 KB
testcase_14 AC 393 ms
4,348 KB
testcase_15 AC 616 ms
4,348 KB
testcase_16 AC 525 ms
4,348 KB
testcase_17 AC 596 ms
4,348 KB
testcase_18 AC 25 ms
4,348 KB
testcase_19 AC 631 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 398 ms
4,348 KB
testcase_23 AC 633 ms
4,348 KB
testcase_24 AC 632 ms
4,348 KB
testcase_25 AC 616 ms
4,348 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 36 ms
4,348 KB
testcase_28 AC 504 ms
4,348 KB
testcase_29 AC 272 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 236 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::{
    collections::HashSet,
    io::{self, BufRead},
};

fn main() {
    io::stdin().lock().lines().for_each(|l| {
        let goal = l.unwrap().parse::<usize>().unwrap();
        if goal == 1 {
            println!("1");
            return;
        }

        let pop_counts = (0..=goal)
            .map(|n| n.count_ones() as usize)
            .collect::<Vec<_>>();

        let mut positions = HashSet::new();
        positions.insert(1);
        let mut reached = positions.clone();

        for i in 2.. {
            let mut new_positions = HashSet::new();
            for p in &positions {
                let left = p - pop_counts[*p];
                if left >= 1 {
                    new_positions.insert(left);
                }
                let right = p + pop_counts[*p];
                if right <= goal {
                    new_positions.insert(right);
                }
            }

            if new_positions.contains(&goal) {
                println!("{}", i);
                break;
            } else if new_positions.iter().all(|p| reached.contains(p)) {
                println!("-1");
                break;
            }

            positions = new_positions.clone();
            reached.extend(new_positions);
        }
    });
}
0