import Data.List main = interact counts counts = unlines . collect 0 . foldr hist [] . map (length . words) . lines hist x hs = case lookup x hs of Just c -> insert (x,c+1) (filter ((/=x).fst) hs) Nothing -> insert (x,1) hs collect n [] = [] collect n (x:xs) = let (f,r) = break ((>2^n).fst) (x:xs) s = sum $ map snd f in ("2^"++show n++"\t"++show s) : collect (n+1) r