-- permutation prefixes :: [a] -> [[a]] prefixes [] = [[]] prefixes (x : xs) = [] : [ x : ys | ys <- prefixes xs] suffixes :: [a] -> [[a]] suffixes [] = [[]] suffixes xs@(_ : xs') = xs : suffixes xs' interleave :: a -> [a] -> [[a]] interleave x ys = [pre ++ [x] ++ suf | (pre,suf) <- zip (prefixes ys) (suffixes ys)] permutation :: [a] -> [[a]] permutation [] = [[]] permutation (x : xs) = [zs | ys <- permutation xs, zs <- interleave x ys] digits :: Int -> [Int] digits n | n < 10 = [n] | otherwise = (n `mod` 10) : digits (n `div` 10) -- >>> digits 12345678 -- [8,7,6,5,4,3,2,1] -- >>> permutation $ digits 123 -- [[3,2,1],[2,3,1],[2,1,3],[3,1,2],[1,3,2],[1,2,3]] -- >>> length $ permutation $ digits 12345678 -- 40320 -- g :: [Int] -> Int g [] = undefined g [n] = n g (n : ns) = n + 10 * g ns digits2Int :: [Int] -> Int digits2Int ns = g ns -- >>> digits2Int $ digits 1234 -- 1234 f :: Int -> Int f k = length [n | ds <- permutation $ digits 12345678, let n = digits2Int ds, n `mod` k == 0] -- >>> f 69921 -- [78451362,31254687,82716543,42861573,85723146,72438156,38526471,54328617] -- main :: IO () main = do k <- readLn :: IO Int putStrLn $ show $ f k