fork(1) download
  1. module Main where
  2.  
  3. genarrs :: Int -> [(Int, Int)] -- Generate all possible inner 1d arrays
  4. genarrs s = genarr 0 s
  5. where
  6. genarr _ 0 = []
  7. genarr a b | a<=b = [(a,b)] ++ (genarr (a+1) b)
  8. | a>b = genarr 0 (b-1)
  9.  
  10. cntoness :: (Int,Int) -> [Int] -> Int
  11. cntoness (a,b) xs | a > b = 0
  12. | a > (length xs) || b > (length xs) = 0
  13. | a <= b = (xs!!a) + cntoness ((a+1),b) xs -- Input array consists only of ones and zeros
  14. -- so we can use simple sum for count.
  15.  
  16. cntonesa :: ((Int,Int), (Int,Int)) -> [[Int]] -> Int
  17. cntonesa (ab, (a,b)) xs | a > b = 0
  18. | a > (length xs) || b > (length xs) = 0
  19. | a <= b = (cntoness ab (xs!!a)) + cntonesa (ab, ((a+1),b)) xs
  20.  
  21. genarrs2d :: Int -> Int -> [((Int, Int), (Int, Int))] -- Generate all possible inner 2d arrays
  22. genarrs2d a b = [(x, y) | x <- genarrs a, y <- genarrs b]
  23.  
  24. readMatr :: Int -> Int -> IO [[Int]]
  25. readMatr w h = sequence . replicate h $ fmap (map read . take w . words) getLine
  26.  
  27. main :: IO ()
  28. main = do
  29. sizesln <- getLine
  30. let nums = map read $ words sizesln
  31. let w = nums!!0
  32. let h = nums!!1
  33. let n = nums!!2
  34. arr <- readMatr w h
  35. let cnts = map (\x -> (cntonesa x) arr) (genarrs2d (w-1) (h-1)) -- Coounts of ones in each array
  36. let fones = length $ filter (==n) cnts -- Length of array constsists of only counts equals n
  37. print fones
Success #stdin #stdout 0s 6340KB
stdin
3 4 2
1 0 1
0 1 1
1 0 0
1 1 0
stdout
12