Data.List.find を使った巧妙な仕掛け
競プロ典型 90 問 16 日目 - Minimum Coins(★3)
import Control.Monad
import qualified Data.ByteString.Char8 as C
import Data.List
import Data.Maybe
main = sol <$> readLn <*> get >>= print
get = unfoldr (C.readInt . C.dropWhile (==' ')) <$> C.getLine
sol n as = fromJust $ find (\s -> any (f s) [z0,z0-1..0]) [z0..9999]
where
[a,b,c] = sort as
z0 = n `div` c
f s z = l>=0 && l `mod` (b-a)==0 && k>=0 && k `mod` (b-a)==0
where
m = n-c*z
l = b*(s-z)-m
k = m-a*(s-z)
いいなと思ったら応援しよう!
ありがとう