• + 0 comments

    Haskell

    I'm kind of surprised to see implementations that AND over the list in the discussion; a Haskell foldl1 (.&.) [a..b] (basically the same thing) would often timeout.

    module Main where
    
    import Data.Bits (shiftL, shiftR, (.&.))
    import Control.Monad (replicateM_)
    
    solve :: [Integer] -> Integer
    solve [a, b] = commonPrefix a b
      where
        commonPrefix x y
            | x == y = x
            | otherwise = commonPrefix (x `shiftR` 1) (y `shiftR` 1) `shiftL` 1
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            getLine >>= print . solve . map read . words