結果

問題 No.1644 Eight Digits
ユーザー satou_a_mansatou_a_man
提出日時 2021-08-13 22:03:20
言語 Haskell
(9.8.2)
結果
AC  
実行時間 14 ms / 1,000 ms
コード長 1,177 bytes
コンパイル時間 4,885 ms
コンパイル使用メモリ 173,824 KB
実行使用メモリ 8,832 KB
最終ジャッジ日時 2024-04-14 17:58:24
合計ジャッジ時間 6,536 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
8,576 KB
testcase_01 AC 13 ms
8,576 KB
testcase_02 AC 14 ms
8,704 KB
testcase_03 AC 13 ms
8,704 KB
testcase_04 AC 14 ms
8,704 KB
testcase_05 AC 14 ms
8,576 KB
testcase_06 AC 14 ms
8,704 KB
testcase_07 AC 14 ms
8,832 KB
testcase_08 AC 14 ms
8,704 KB
testcase_09 AC 14 ms
8,576 KB
testcase_10 AC 14 ms
8,576 KB
testcase_11 AC 14 ms
8,576 KB
testcase_12 AC 14 ms
8,576 KB
testcase_13 AC 14 ms
8,576 KB
testcase_14 AC 14 ms
8,704 KB
testcase_15 AC 14 ms
8,576 KB
testcase_16 AC 13 ms
8,576 KB
testcase_17 AC 13 ms
8,704 KB
testcase_18 AC 13 ms
8,704 KB
testcase_19 AC 13 ms
8,576 KB
testcase_20 AC 13 ms
8,576 KB
testcase_21 AC 13 ms
8,576 KB
testcase_22 AC 14 ms
8,704 KB
testcase_23 AC 14 ms
8,576 KB
testcase_24 AC 13 ms
8,576 KB
testcase_25 AC 13 ms
8,576 KB
testcase_26 AC 14 ms
8,576 KB
testcase_27 AC 13 ms
8,576 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

-- 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
0