36 lines
661 B
Rust
36 lines
661 B
Rust
use std::io::{read_to_string, stdin};
|
|
|
|
fn countf2x(x: u64) -> u64 {
|
|
// f(2^x - 1) = 2^(x-1) * x;
|
|
if x == 0 {
|
|
0
|
|
} else {
|
|
2u64.pow((x - 1) as u32) * x
|
|
}
|
|
}
|
|
|
|
fn countf(n: u64) -> u64 {
|
|
let mut x = 32u64;
|
|
let mut t = 1u64 << 32u64;
|
|
if n == 0 {
|
|
return 0;
|
|
}
|
|
while t & n == 0 {
|
|
t >>= 1;
|
|
x -= 1;
|
|
}
|
|
|
|
countf2x(x) + (n + 1 - t) + countf(n - t)
|
|
}
|
|
|
|
fn main() {
|
|
let temp = read_to_string(stdin()).unwrap();
|
|
let mut iter = temp
|
|
.split_ascii_whitespace()
|
|
.map(|x| x.parse::<usize>().unwrap());
|
|
|
|
let n = iter.next().unwrap() as u64;
|
|
|
|
println!("{}", countf(n));
|
|
}
|