結果

問題 No.1243 約数加算
ユーザー ngtkanangtkana
提出日時 2020-07-01 20:30:59
言語 Rust
(1.77.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,168 bytes
コンパイル時間 1,126 ms
コンパイル使用メモリ 150,688 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 14:25:47
合計ジャッジ時間 1,899 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::{convert::identity, io::stdin};

pub trait Boolinator: Sized {
    fn as_some_from<T, F>(self, some: F) -> Option<T>
    where
        F: FnOnce() -> T;
}

impl Boolinator for bool {
    fn as_some_from<T, F>(self, some: F) -> Option<T>
    where
        F: FnOnce() -> T,
    {
        if self {
            Some(some())
        } else {
            None
        }
    }
}

pub enum Either<L, R> {
    Left(L),
    Right(R),
}

fn main() {
    let q = {
        let mut s = String::new();
        stdin().read_line(&mut s).unwrap();
        s.trim_end().to_owned().parse::<usize>().unwrap()
    };

    for _ in 0..q {
        let (a, b) = {
            let mut s = String::new();
            stdin().read_line(&mut s).unwrap();
            let s = s.trim_end().to_owned();
            let mut s = s.split_whitespace();
            (
                s.next().unwrap().parse::<u64>().unwrap(),
                s.next().unwrap().parse::<u64>().unwrap(),
            )
        };

        let ans: Vec<u64> = (0..60)
            .map(|i| Either::Left(i))
            .chain((0..60).rev().map(|i| Either::Right(i)))
            .scan(a, |a, e| {
                Some(match e {
                    Either::Left(i) => {
                        let x = 1u64 << i;
                        ((*a >> i & 1) == 1 && *a + x <= b).as_some_from(|| {
                            *a += x;
                            x
                        })
                    }
                    Either::Right(i) => {
                        let x = 1u64 << i;
                        (*a + x <= b).as_some_from(|| {
                            *a += x;
                            x
                        })
                    }
                })
            })
            .filter_map(identity)
            .collect();

        println!("{}", ans.len());
        println!(
            "{}",
            ans.iter()
                .map(ToString::to_string)
                .enumerate()
                .fold("".to_string(), |acc, (i, s)| if i == 0 {
                    s
                } else {
                    acc + " " + s.as_str()
                })
        );
    }
}
0