module Main
import Bool, Int, List, Array, ValueCast, StringCast, System, Misc, Trace
Start = length $
divide n s ls
where
n = toInt getCommandLine.[1]
ls = [1..n*n]
s = sum ls / n
divide n s ls = search ls
where
search [] = [[]]
search ls
= comb_sum s n ls
|> map (\f = let
(f0,fr) = (head f, tail f)
rr = filter (\e = not (e <= f0 || fr contains e)) ls
in [[f:t] \\ t <- search rr])
|> foldr (++) []
comb_sum :: Int Int [Int] -> [[Int]]
comb_sum s n [e:ee]
| e > s = []
= map ((:>) e) (comb_sum (s - e) ck ee) // <- この部分を修正
where
ck = reverse $ take (n-1) $ scan (+) 0 $ reverse $ ee
comb_sum 0 [] _ = [[]]
comb_sum _ [] _ = []
comb_sum _ _ [] = []
comb_sum s ck=:[c:cc] [e:ee]
| e > s = []
| e < s - c = comb_sum s ck ee
= map ((:>) e) (comb_sum (s-e) cc ee) ++ comb_sum s ck ee
lethevert
#5218()
[
Clean
]
Rating0/0=0.00
comb_sumで明らかに無駄な数え上げをしているところがあったので、それを削除しました
n=5で18秒まで減少しました
see: AltEnvライブラリを使っています
module Main import Bool, Int, List, Array, ValueCast, StringCast, System, Misc, Trace Start = length $ divide n s ls where n = toInt getCommandLine.[1] ls = [1..n*n] s = sum ls / n divide n s ls = search ls where search [] = [[]] search ls = comb_sum s n ls |> map (\f = let (f0,fr) = (head f, tail f) rr = filter (\e = not (e <= f0 || fr contains e)) ls in [[f:t] \\ t <- search rr]) |> foldr (++) [] comb_sum :: Int Int [Int] -> [[Int]] comb_sum s n [e:ee] | e > s = [] = map ((:>) e) (comb_sum (s - e) ck ee) // <- この部分を修正 where ck = reverse $ take (n-1) $ scan (+) 0 $ reverse $ ee comb_sum 0 [] _ = [[]] comb_sum _ [] _ = [] comb_sum _ _ [] = [] comb_sum s ck=:[c:cc] [e:ee] | e > s = [] | e < s - c = comb_sum s ck ee = map ((:>) e) (comb_sum (s-e) cc ee) ++ comb_sum s ck eeRating0/0=0.00-0+
[ reply ]