• + 0 comments

    Haskell

    module Main where
    
    import Data.Bits (Bits (shiftR, testBit))
    
    solve :: Integer -> Integer
    solve x = go x 0 0
      where
        go 0 idx acc = acc
        go n idx acc =
            if testBit n 0
                then go (shiftR n 1) (idx + 1) acc
                else go (shiftR n 1) (idx + 1) (acc + 2 ^ idx)
    
    main :: IO ()
    main = getLine >> getContents >>= mapM_ (print . solve . read) . lines