結果
| 問題 |
No.1286 Stone Skipping
|
| コンテスト | |
| ユーザー |
fukafukatani
|
| 提出日時 | 2020-11-13 22:01:25 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 1 ms / 2,000 ms |
| コード長 | 1,285 bytes |
| コンパイル時間 | 12,747 ms |
| コンパイル使用メモリ | 399,344 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-23 09:52:52 |
| 合計ジャッジ時間 | 13,836 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
#![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)
}
fukafukatani