Submission #65137836


Source Code Expand

import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List

import Data.Array

main :: IO ()
main = do
  nmk@(n:_) <- bsGetLnInts
  ws <- replicateM n (head <$> bsGetLnInts)
  let ans = abc243f nmk ws
  print ans

bsGetLnInts :: IO [Int]
bsGetLnInts = unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine

abc243f :: [Int] -> [Int] -> Int
abc243f [n,m,k] ws = pr ! (1,k,m)
  where
-- 階乗の逆数
    rf = listArray (0, k) $ scanl' (\v i -> mul v $ modRecip i) 1 [1 ..]
-- 分母 ΣWi の逆数
    de = modRecip $ sum ws
-- Wi/ΣWi 一度のくじでiを得る確率
    p1 = listArray (1, n) $ map (mul de) ws
-- Wi
    w1 = listArray (1, n) ws
-- DP
    bnds = ((1,0,0),(succ n,k,m))
    pr = listArray bnds $ map pf $ range bnds
    pf (_,0,0) = prodd [1 .. k]
    pf (_,_,0) = 0
    pf (_,0,_) = 0
    pf (i,_,_) | n < i = 0
    pf (i,p,q) = summ
      [ prodd [rf ! vi, pp, pr ! (succ i, p - vi, if vi == 0 then q else pred q)]
      | (vi, pp) <- zip [0 .. p - pred q] $ iterate (mul pi) 1
      ]
      where
        wi = w1 ! i
        pi = p1 ! i

modBase :: Int
modBase =  998244353

reg :: Int -> Int
reg x = mod x modBase
add, mul :: Int -> Int -> Int
add x y = reg $ x + y
mul x y = reg $ x * y

summ :: [Int] -> Int
summ = foldl' add 0

prodd :: [Int] -> Int
prodd = foldl' mul 1

modRecip :: Int -> Int
modRecip v = fst $ loop v modBase
  where
    loop _ 0 = (1, 0)
    loop a b = let (y, x) = loop b (mod a b)
               in  (x, y - div a b * x)

Submission Info

Submission Time
Task F - Lottery
User joetheshootingst
Language Haskell (GHC 9.4.5)
Score 500
Code Size 1582 Byte
Status AC
Exec Time 31 ms
Memory 30196 KiB

Compile Error

app/Main.hs:19:1: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘abc243f’:
        Patterns of type ‘[Int]’, ‘[Int]’ not matched:
            [] _
            [_] _
            [_, _] _
            (_:_:_:_:_) _
   |
19 | abc243f [n,m,k] ws = pr ! (1,k,m)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

app/Main.hs:41:9: warning: [-Wunused-local-binds]
    Defined but not used: ‘wi’
   |
41 |         wi = w1 ! i
   |         ^^

app/Main.hs:42:9: warning: [-Wname-shadowing]
    This binding for ‘pi’ shadows the existing binding
      imported from ‘Prelude’ at app/Main.hs:1:1
      (and originally defined in ‘GHC.Float’)
   |
42 |         pi = p1 ! i
   |         ^^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 4
AC × 28
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
Case Name Status Exec Time Memory
random_01.txt AC 31 ms 26704 KiB
random_02.txt AC 21 ms 17228 KiB
random_03.txt AC 31 ms 30196 KiB
random_04.txt AC 21 ms 22976 KiB
random_05.txt AC 2 ms 8320 KiB
random_06.txt AC 11 ms 16644 KiB
random_07.txt AC 5 ms 13148 KiB
random_08.txt AC 2 ms 8188 KiB
random_09.txt AC 31 ms 26696 KiB
random_10.txt AC 11 ms 13784 KiB
random_11.txt AC 31 ms 22964 KiB
random_12.txt AC 21 ms 15824 KiB
random_13.txt AC 11 ms 17612 KiB
random_14.txt AC 5 ms 12432 KiB
random_15.txt AC 2 ms 8552 KiB
random_16.txt AC 2 ms 8660 KiB
random_17.txt AC 31 ms 26704 KiB
random_18.txt AC 1 ms 6812 KiB
random_19.txt AC 2 ms 8324 KiB
random_20.txt AC 1 ms 6860 KiB
random_21.txt AC 2 ms 7824 KiB
random_22.txt AC 1 ms 6792 KiB
random_23.txt AC 1 ms 6872 KiB
random_24.txt AC 1 ms 6728 KiB
sample_01.txt AC 1 ms 6740 KiB
sample_02.txt AC 2 ms 6792 KiB
sample_03.txt AC 1 ms 6756 KiB
sample_04.txt AC 2 ms 7344 KiB