Submission #62829720


Source Code Expand

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

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

import Data.Array.Unboxed

main :: IO ()
main = do
  [n] <- bsGetLnInts
  ss <- bsGetLnInts
  let ans = abc392g n ss
  print ans

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

abc392g :: Int -> [Int] -> Int
abc392g _n ss = sum $ zipWith f ps (olol cs)
  where
    n = maximum ss
    arr = accumArray (+) 0 (0, n) [(s,1) | s <- ss] :: UArray Int Int
    ps = elems arr
    cs = convolution ps ps
    f 1 c = div (pred c) 2
    f 0 _ = 0
-- 偶数番目を取り出す
    olol (x:xs) = x : olol (drop 1 xs)
    olol _ = []

convolution :: [Int] -> [Int] -> [Int]
convolution as bs = runST $
  do
    ar <- newListArray bnds $ as ++ repeat 0 :: ST s (STUArray s Int Int)
    br <- newListArray bnds $ bs ++ repeat 0 :: ST s (STUArray s Int Int)
    fft ar root
    fft br root
    forM_ (range bnds) (\i -> do
      a <- readArray ar i
      b <- readArray br i
      writeArray ar i $ mul a b
      )
    fft ar invroot

    forM [0 .. na + pred nb] (fmap (mul divn) . readArray ar)
  where
-- 定数
    p = 998244353
    root = 3
    invroot = modRecip root
-- pを底とするモジュロ演算
    reg x = mod x p
    add x y = reg $ x + y
    mul x y = reg $ x * y
    modRecip a = reg $ fst $ loop a p
      where
        loop a 0 = (1, 0)
        loop a b = let (y, x) = loop b (mod a b)
                   in  (x, y - div a b * x)
    pow b e = loop e (reg b) 1
      where
        loop 0 _ acc = acc
        loop k c acc = loop (div k 2) (mul c c) $ if odd k then mul acc c else acc
-- 本体
    na = length as
    nb = length bs
    (n, w) = (n, length xs)
      where
        (xs, n:_) = span ((na + nb) >) $ iterate (2 *) 1
    bnds = (0, pred n)
    divn = modRecip n
-- 下請け
-- ビット反転
    bitReverse :: Int -> Int
    bitReverse j = sum [bit i | i <- [0 .. pred w], testBit j (pred w - i)]
-- 高速フーリエ変換
    fft a base =
      do
-- 配列ビット反転
        forM_ (range bnds) (\i -> do
          let j = bitReverse i
          when (i < j) $ do
            x <- readArray a i
            y <- readArray a j
            writeArray a i y
            writeArray a j x
            )
        forM_ (zip omega2N $ iterate (2 *) 1) (\(omega, dist) ->
          forM_ [0, dist + dist .. pred n] (\b -> do
            let (omegas1, omegas2) = splitAt dist $ iterate (mul omega) 1
            forM_ (zip3 omegas1 omegas2 [b ..]) (\(o1, o2, i) -> do
              f_even <- readArray a i
              f_odd  <- readArray a (i + dist)
              writeArray a  i         (reg $ f_even + o1 * f_odd)
              writeArray a (i + dist) (reg $ f_even + o2 * f_odd)
              )
            )
          )
      where
        omegaN = pow base (div (pred p) n)
        omega2N = reverse $ take w $ iterate (\x -> mul x x) omegaN

Submission Info

Submission Time
Task G - Fine Triplets
User joetheshootingst
Language Haskell (GHC 9.4.5)
Score 0
Code Size 3090 Byte
Status TLE
Exec Time 3344 ms
Memory 528736 KiB

Compile Error

app/Main.hs:29:5: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘f’:
        Patterns of type ‘a’, ‘a’ not matched:
            p _ where p is not one of {1, 0}
   |
29 |     f 1 c = div (pred c) 2
   |     ^^^^^^^^^^^^^^^^^^^^^^...

app/Main.hs:57:5: warning: [-Wunused-local-binds]
    Defined but not used: ‘add’
   |
57 |     add x y = reg $ x + y
   |     ^^^

app/Main.hs:61:14: warning: [-Wunused-matches]
    Defined but not used: ‘a’
   |
61 |         loop a 0 = (1, 0)
   |              ^

app/Main.hs:61:14: warning: [-Wname-shadowing]
    This binding for ‘a’ shadows the existing binding
      bound at app/Main.hs:59:14
   |
61 |         loop a 0 = (1, 0)
   |              ^

app/Main.hs:62:14: warning: [-Wname-shadowing]
    This binding for ‘a’ shadows the existing binding
      bound at app/Main.hs:59:14
   |
62 |         loop a b = let (y, x) = loop b (mod a b)
   |              ^

app/Main.hs:73:9: warning: [-Wincomplete-uni-patterns]
    Pattern match(es) are non-exhaustive
    In a pattern binding:
        Patterns of type ‘([Int], [Int])’ not matched: (_, [])
   |
73 |         (xs, n:_) = span ((na + nb) >) $ iterate (2 *) 1
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

app/Main.hs:73:14: warning: [-Wname-shadowing]
    This binding for ‘n’ shadows the existing binding
      bound at app/Main.hs:71:6
   |
73 |         (xs, n:_) = span ((na + nb) >) $ iterate (2 *) 1
   |              ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 3
AC × 14
TLE × 29
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 6832 KiB
sample_02.txt AC 1601 ms 276760 KiB
sample_03.txt AC 1 ms 6872 KiB
test_01.txt AC 1 ms 6756 KiB
test_02.txt TLE 3339 ms 501952 KiB
test_03.txt AC 3 ms 6540 KiB
test_04.txt AC 1 ms 6772 KiB
test_05.txt TLE 3338 ms 501852 KiB
test_06.txt TLE 3338 ms 501936 KiB
test_07.txt TLE 3339 ms 501956 KiB
test_08.txt TLE 3338 ms 501784 KiB
test_09.txt AC 2 ms 7208 KiB
test_10.txt TLE 3338 ms 501984 KiB
test_11.txt AC 2 ms 7244 KiB
test_12.txt TLE 3339 ms 500036 KiB
test_13.txt AC 4 ms 10956 KiB
test_14.txt TLE 3338 ms 501956 KiB
test_15.txt AC 4 ms 10964 KiB
test_16.txt TLE 3338 ms 500792 KiB
test_17.txt AC 31 ms 15676 KiB
test_18.txt TLE 3338 ms 498912 KiB
test_19.txt AC 31 ms 15880 KiB
test_20.txt TLE 3339 ms 501944 KiB
test_21.txt AC 333 ms 79528 KiB
test_22.txt TLE 3339 ms 519356 KiB
test_23.txt AC 344 ms 79052 KiB
test_24.txt TLE 3340 ms 519416 KiB
test_25.txt TLE 3339 ms 498956 KiB
test_26.txt TLE 3340 ms 528736 KiB
test_27.txt TLE 3338 ms 497900 KiB
test_28.txt TLE 3339 ms 503100 KiB
test_29.txt TLE 3339 ms 501048 KiB
test_30.txt TLE 3338 ms 501012 KiB
test_31.txt TLE 3338 ms 499052 KiB
test_32.txt TLE 3340 ms 518560 KiB
test_33.txt TLE 3339 ms 518516 KiB
test_34.txt TLE 3344 ms 518496 KiB
test_35.txt TLE 3339 ms 518468 KiB
test_36.txt TLE 3338 ms 518388 KiB
test_37.txt TLE 3339 ms 518384 KiB
test_38.txt TLE 3341 ms 518412 KiB
test_39.txt TLE 3339 ms 518368 KiB
test_40.txt TLE 3338 ms 518316 KiB