计算每个数二进制末尾 0 的个数(ν₂),取最小值即为所有数能同时除以 2 的最大次数。
OJ: atcoder
题目 ID: abc081_b
难度:入门
标签:haskell
日期: 2026-07-10 09:19
题意
思路
对每个数计算能除以 2 的次数(即二进制末尾 0 的个数,
代码
haskell
{-
Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
rbook: -> https://rbook.roj.ac.cn https://rbook2.roj.ac.cn
rainboy的学习导航网站: https://idx.roj.ac.cn
create_at: 2026-07-10 10:24
update_at: 2026-07-10 10:24
-}
-- 二进制末尾0的数量 : lowbit
--
-- 递归函数
calc2 n = if even n then 1 + calc2 (n `div` 2) else 0
main = do
n <- getLine
str <- getLine
let x = map read . words $ str :: [Int]
print $ minimum $ map calc2 x另一种写法,使用 getContents 一次读完输入:
haskell
{-
Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
rbook: -> https://rbook.roj.ac.cn https://rbook2.roj.ac.cn
rainboy的学习导航网站: https://idx.roj.ac.cn
create_at: 2026-07-10 10:09
update_at: 2026-07-10 10:09
-}
calc2 0 = 0
calc2 n = if even n then 1 + calc2 (n `div` 2) else 0
main = do
input <- getContents
let xs = map read (words input) :: [Int]
print $ minimum $ map calc2 (tail xs)复杂度
时间复杂度
总结
minimum 取最小值。
两种输入方式:getLine 逐行读或 getContents 一次读完。