結果

問題 No.1286 Stone Skipping
ユーザー fukafukatanifukafukatani
提出日時 2020-11-13 22:01:25
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,285 bytes
コンパイル時間 1,110 ms
コンパイル使用メモリ 141,112 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-30 16:07:18
合計ジャッジ時間 2,219 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 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 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 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::*;

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>();
    if d == 1 {
        println!("1");
        return;
    }
    let mut max_bound = 0;
    while d > 2i128.pow(max_bound) {
        max_bound += 1;
    }
    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