Skip to content

Commit

Permalink
past202206-open e
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Jan 17, 2023
1 parent 55e36f8 commit 97f612c
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions past202206-open/src/bin/e.rs
@@ -1,10 +1,42 @@
use proconio::{input, marker::Usize1};
use proconio::input;

fn main() {
input! {
n: usize,
a: [Usize1; n],
};
let ans = n - a.len();

if n == 1 {
println!("1");
return;
}

// 1 = 1 = 1^2
// 1+3 = 4 = 2^2
// 1+3+5 = 9 = 3^2
// 1+3+5+7 = 16 = 4^2
// 1+3+5+7+9 = 25 = 5^2
// ... = 10^18 = (10^9)^2
let mut ng = 1_usize;
let mut ok = 1_000_000_000_usize;
while ok - ng > 1 {
let mid = ng + (ok - ng) / 2;
if n <= mid.pow(2) {
ok = mid;
} else {
ng = mid;
}
}

let left = ok - 1;
let right = ok;
let len = right.pow(2) - left.pow(2);
let index = n - left.pow(2) - 1;
// 1 , 2 1 2 , 3 2 1 2 3 , 4 3 2 1 2 3 4
// 1 4 9 16
let ans = if index <= len / 2 {
right - index
} else {
right - len / 2 + index - (len / 2)
};
println!("{}", ans);
}

0 comments on commit 97f612c

Please sign in to comment.