complete 1660.rs 5242.rs 8406.rs 14024.rs
This commit is contained in:
35
storage/jungol/rs/completed/1660.rs
Normal file
35
storage/jungol/rs/completed/1660.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
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));
|
||||
}
|
||||
Reference in New Issue
Block a user