fork download
  1. main :: IO ()
  2. main = interact
  3. $ (\(base:xs) -> concatMap (allGrays (read base) . read) xs)
  4.  
  5. allGrays :: Int -> Int -> String
  6. allGrays base x = unlines $ map eachGray [0..x-1]
  7. where
  8. eachGray = concatMap show
  9. . toGray base
  10. . toBase base (logB base x)
  11.  
  12. logB :: Int -> Int -> Int
  13. logB base = ceiling . logBase (fromIntegral base) . fromIntegral
  14.  
  15. toBase :: Int -> Int -> Int -> [Int]
  16. toBase base = toBase'
  17. where
  18. toBase' :: Int -> Int -> [Int]
  19. toBase' len 0 = replicate len 0
  20. toBase' len x = toBase' (len - 1) (x `div` base) ++ [x `mod` base]
  21.  
  22. toGray :: Int -> [Int] -> [Int]
  23. toGray base = toGray' 0
  24. where
  25. toGray' :: Int -> [Int] -> [Int]
  26. toGray' _ [] = []
  27. toGray' shift (x:xs) = (x + shift) `mod` base : toGray' (shift + base - x) xs
Success #stdin #stdout 0s 8388607KB
stdin
3
9
27
stdout
00
01
02
12
10
11
21
22
20
000
001
002
012
010
011
021
022
020
122
120
121
101
102
100
110
111
112
211
212
210
220
221
222
202
200
201