Submission #46395920


Source Code Expand

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

import Data.Array.ST
import Data.Array
import Control.Monad

main = do
  [n,x] <- bsGetLnInts
  ts <- bsGetLnInts
  let ans = abc323e n x ts
  print ans

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

abc323e :: Int -> Int -> [Int] -> Int
abc323e n x ts@(t1:_) = ans
  where
-- 1/n のmodP
    recipN = modRecip n
-- 時刻0~Xが、新たな曲を選択して始めるタイミングになっている確率
-- 時刻0は1、それ以降はTi後に1/nずつ配るDP
--    pa = distributingDPga (0,x) [(0,1)] df (reg . sum)
    pa = distributingDP (0,x) 0 [(0,1)] df add
    df t p = [(t1, q) | t1 <- takeWhile (x >=) $ map (t +) sts] where q = mul p recipN
    sts = sort ts
-- 時刻 X-T1+1~X の確率に、T1が選択される確率1/Nを掛けた総和が答え
    ans = mul recipN $ reg $ sum $ drop (x - t1 + 1) $ elems pa

-- mod
modBase = 998244353
reg x = mod x modBase
add x y = reg $ x + y
mul x y = reg $ x * y

-- @gotoki_no_joe
modRecip a = powerish mul 1 a (modBase - 2)
powerish mul i a b = foldl' {-'-} mul i [p | (True, p) <- zip bs ps]
  where
    bs = map odd $ takeWhile (0 <) $ iterate (flip div 2) b
    ps = iterate (\x -> mul x x) a

-- 配るDPをmuable arrayで元々の仕組み通りに実装
distributingDP :: Ix i => (i,i) -> x -> [(i,x)] -> (i -> x -> [(i,x)]) -> (x -> x -> x) -> Array i x
distributingDP bnds i inis df op = runSTArray $
  do
    arr <- newArray bnds i
    forM_ inis $ uncurry (writeArray arr)
    forM_ (range bnds) $ \i -> do
      x <- readArray arr i
      forM_ (df i x) $ \(j, y) -> do
        z <- readArray arr j
        writeArray arr j $! op y z
    return arr

Submission Info

Submission Time
Task E - Playlist
User joetheshootingst
Language Haskell (GHC 9.4.5)
Score 450
Code Size 1823 Byte
Status AC
Exec Time 71 ms
Memory 11444 KiB

Compile Error

app/Main.hs:9:1: warning: [-Wmissing-signatures]
    Top-level binding with no type signature: main :: IO ()
  |
9 | main = do
  | ^^^^

app/Main.hs:19:1: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘abc323e’:
        Patterns of type ‘Int’, ‘Int’, ‘[Int]’ not matched: _ _ []
   |
19 | abc323e n x ts@(t1:_) = ans
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^...

app/Main.hs:27:25: warning: [-Wname-shadowing]
    This binding for ‘t1’ shadows the existing binding
      bound at app/Main.hs:19:17
   |
27 |     df t p = [(t1, q) | t1 <- takeWhile (x >=) $ map (t +) sts] where q = mul p recipN
   |                         ^^

app/Main.hs:33:1: warning: [-Wmissing-signatures]
    Top-level binding with no type signature: modBase :: Int
   |
33 | modBase = 998244353
   | ^^^^^^^

app/Main.hs:34:1: warning: [-Wmissing-signatures]
    Top-level binding with no type signature: reg :: Int -> Int
   |
34 | reg x = mod x modBase
   | ^^^

app/Main.hs:35:1: warning: [-Wmissing-signatures]
    Top-level binding with no type signature: add :: Int -> Int -> Int
   |
35 | add x y = reg $ x + y
   | ^^^

app/Main.hs:36:1: warning: [-Wmissing-signatures]
    Top-level binding with no type signature: mul :: Int -> Int -> Int
   |
36 | mul x y = reg $ x * y
   | ^^^

app/Main.hs:39:1: warning: [-Wmissing-signatures]
    Top-level binding with no type signature: modRecip :: Int -> Int
   |
39 | modRecip a = powerish mul 1 a (modBase - 2)
   | ^^^^^^^^

app/Main.hs:40:1: warning: [-Wmissing-signatures]
    Top-level binding with no type signature:
      powerish :: Integral p => (b -> b -> b) -> b -> b -> p -> b
   |
40 | powerish mul i a b = foldl' {-'-} mul i [p | (True, p) <- zip bs ps]
   | ^^^^^^^^

app/Main.hs:40:10: warning: [-Wname-shadowing]
    This binding for ‘mul’ shadows the existing binding
      defined at app/Main.hs:36:1
   |
40 | powerish mul i a b = foldl' {-'-} mul i [p | (True, p) <- zip bs ps]
   |          ^^^

app/Main.hs:51:27: warning: [-Wname-shadowing]
    This binding for ‘i’ shadows the existing binding
      bound at app/Main.hs:47:21
   |
51 |     forM_ (range bnds) $ \i -> do
   |                           ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 3
AC × 30
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, random_00.txt, 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
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 6848 KiB
example_01.txt AC 2 ms 6828 KiB
example_02.txt AC 2 ms 8228 KiB
hand_00.txt AC 71 ms 10860 KiB
hand_01.txt AC 61 ms 11212 KiB
hand_02.txt AC 7 ms 11052 KiB
hand_03.txt AC 71 ms 11128 KiB
hand_04.txt AC 2 ms 7864 KiB
hand_05.txt AC 2 ms 7636 KiB
hand_06.txt AC 1 ms 7004 KiB
random_00.txt AC 2 ms 7012 KiB
random_01.txt AC 71 ms 11256 KiB
random_02.txt AC 61 ms 11108 KiB
random_03.txt AC 71 ms 10992 KiB
random_04.txt AC 71 ms 11112 KiB
random_05.txt AC 61 ms 11140 KiB
random_06.txt AC 61 ms 11016 KiB
random_07.txt AC 61 ms 11284 KiB
random_08.txt AC 61 ms 11224 KiB
random_09.txt AC 61 ms 11212 KiB
random_10.txt AC 2 ms 7028 KiB
random_11.txt AC 61 ms 11256 KiB
random_12.txt AC 61 ms 11172 KiB
random_13.txt AC 61 ms 11276 KiB
random_14.txt AC 61 ms 11256 KiB
random_15.txt AC 41 ms 11444 KiB
random_16.txt AC 41 ms 11364 KiB
random_17.txt AC 41 ms 11364 KiB
random_18.txt AC 41 ms 11216 KiB
random_19.txt AC 41 ms 11444 KiB