結果

問題 No.642 Two Operations No.1
ユーザー taotao54321taotao54321
提出日時 2018-10-08 13:10:29
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,040 bytes
コンパイル時間 4,296 ms
コンパイル使用メモリ 153,996 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-20 18:31:50
合計ジャッジ時間 1,533 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

use std::io::{ self, prelude::* };

fn clp2(mut x: u64) -> u64 {
    assert!(x > 0);
    x -= 1;
    x |= x >>  1;
    x |= x >>  2;
    x |= x >>  4;
    x |= x >>  8;
    x |= x >> 16;
    x |= x >> 32;
    x + 1
}

fn ntz(mut x: u64) -> u64 {
    if x == 0 { return 64; }
    let mut n = 1;
    if (x & 0x00000000FFFFFFFF) == 0 { n += 32; x >>= 32; }
    if (x & 0x000000000000FFFF) == 0 { n += 16; x >>= 16; }
    if (x & 0x00000000000000FF) == 0 { n +=  8; x >>=  8; }
    if (x & 0x000000000000000F) == 0 { n +=  4; x >>=  4; }
    if (x & 0x0000000000000003) == 0 { n +=  2; x >>=  2; }
    n - (x & 1)
}

fn f(mut n: u64) -> (u64, u64) {
    let mut a = 0;
    while n % 2 == 0 {
        n /= 2;
        a += 1;
    }
    (a, n)
}

fn main() {
    let mut s = String::new();
    io::stdin().read_to_string(&mut s).unwrap();
    let mut tokens = s.split_whitespace();

    let n: u64 = tokens.next().unwrap().parse().unwrap();

    let (a, b) = f(n);
    let c = clp2(b);

    let ans = ntz(c) + c-b + a;

    println!("{}", ans);
}
0