結果

問題 No.1286 Stone Skipping
ユーザー fukafukatanifukafukatani
提出日時 2020-11-13 22:00:23
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,575 bytes
コンパイル時間 797 ms
コンパイル使用メモリ 142,632 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-30 02:52:45
合計ジャッジ時間 1,886 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 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,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 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 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 WA -
testcase_19 AC 1 ms
4,376 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,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn dist(mut start: i128, bound: i32) -> i128 {
    let mut ret = 0;
    for _ in 0..bound {
        ret += start;
        start /= 2;
    }
    ret
}

fn main() {
    let d = read::<i128>();
    let mut max_bound = 0;
    while d > 2i128.pow(max_bound) {
        max_bound += 1;
    }
    debug!(max_bound);
    let criterion = |mid, bound: i32| dist(mid, bound) <= d;

    for bound in (1..max_bound + 1).rev() {
        let (ok, _) = binary_search(1, 100000_00000_00000_001, bound as i32, criterion);
        if dist(ok, bound as i32) == d {
            println!("{}", ok);
            return;
        }
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn binary_search<F>(lb: i128, ub: i128, bound: i32, criterion: F) -> (i128, i128)
where
    F: Fn(i128, i32) -> bool,
{
    assert_eq!(criterion(lb, bound), true);
    assert_eq!(criterion(ub, bound), false);
    let mut ok = lb;
    let mut ng = ub;
    while ng - ok > 1 {
        let mid = (ng + ok) / 2;
        if criterion(mid, bound) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    (ok, ng)
}
0