baekjoon 20260311-12

This commit is contained in:
songyc macbook 2026-03-12 20:57:32 +09:00
parent 04c483ae12
commit dee2ba0a7d
2 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1 @@
print((lambda x: x*(x-1))(int(input())))

View File

@ -0,0 +1,33 @@
var dp: [Int64] = Array(repeating: 0, count: 55)
for n in 1...54 {
dp[n] = 2*dp[n-1] + (1 << (n-1))
}
func countOne(_ N: Int64) -> Int64 {
var res: Int64 = 0
var n: Int64 = N
for i in stride(from: 54, to: 0, by: -1) {
let LSB: Int64 = (1 << (i-1)) & n
if LSB != 0{
res += dp[i-1]
res += n - (1 << (i-1)) + 1
}
n &= ((1 << (i-1)) - 1)
}
return res
}
func main() {
guard let input = readLine() else { return }
let nums: [Int64] = input.split(separator: " ").compactMap{Int64($0)}
let ans: Int64 = countOne(nums[1]) - countOne(nums[0]-1)
print(ans)
}
main()