complete 1660.rs 5242.rs 8406.rs 14024.rs

This commit is contained in:
2026-07-24 17:52:47 +09:00
parent 5b49d47300
commit 8f06210897
4 changed files with 165 additions and 0 deletions

View 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));
}