From dee2ba0a7d0665fbe209b63f3651d2f29d9aa913 Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Thu, 12 Mar 2026 20:57:32 +0900 Subject: [PATCH] baekjoon 20260311-12 --- code_study/Baekjoon/python/15439.py | 1 + code_study/Baekjoon/swift/9527.swift | 33 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 code_study/Baekjoon/python/15439.py create mode 100644 code_study/Baekjoon/swift/9527.swift diff --git a/code_study/Baekjoon/python/15439.py b/code_study/Baekjoon/python/15439.py new file mode 100644 index 0000000..7b20822 --- /dev/null +++ b/code_study/Baekjoon/python/15439.py @@ -0,0 +1 @@ +print((lambda x: x*(x-1))(int(input()))) \ No newline at end of file diff --git a/code_study/Baekjoon/swift/9527.swift b/code_study/Baekjoon/swift/9527.swift new file mode 100644 index 0000000..1ef1c07 --- /dev/null +++ b/code_study/Baekjoon/swift/9527.swift @@ -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()