fork download
  1. {-# LANGUAGE BangPatterns #-}
  2.  
  3. import Data.Bits (xor)
  4. import Data.List (sort)
  5.  
  6. mex :: [Int] -> Int
  7. mex = mexSorted 0 . sort
  8.  
  9. mexSorted :: Int -> [Int] -> Int
  10. mexSorted !i xs@(x : xs') | x < i = mexSorted i xs'
  11. mexSorted i xs@(x : xs') | x == i = mexSorted (i + 1) xs'
  12. mexSorted i _ = i
  13.  
  14. -- |@nimValues !! n@ is the nim-value (aka Sprague-Grundy number) of the game described in
  15. -- http://c...content-available-to-author-only...e.com/questions/16228 on the path with n vertices.
  16.  
  17. nimValues :: [Int]
  18. nimValues = 0 : 1 : map f [0 ..]
  19. where
  20. f s = mex $ zipWith xor (take (s `div` 2 + 1) nimValues) (reverse (take (s + 1) nimValues))
  21.  
  22. -- |Report the values of n (≤ 100) such that the first player loses in the game
  23. -- on the path with n vertices (including 0, meaning the empty graph).
  24.  
  25. main :: IO ()
  26. main = print $ map fst $ filter (\(_, nim) -> nim == 0) $ zip [0 :: Int .. 100] nimValues
Success #stdin #stdout 0.02s 3584KB
stdin
Standard input is empty
stdout
[0,3,7,23,27,37,41,57,61,71,75,91,95]