Submission #62673977
Source Code Expand
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List
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 = convo
where
convo as bs = map (mul divn) $ take n c
where
na = length as
nb = length bs
-- 答えの長さ以上の2のべき
(n, w) = (n, length xs)
where
(xs, n:_) = span ((na + pred nb) >) $ iterate (2 *) 1
-- 0パディング
as0 = as ++ repeat 0
bs0 = bs ++ repeat 0
-- 正方向
das = ntt as0 n w $ drop (23 - w) root
dbs = ntt bs0 n w $ drop (23 - w) root
-- まとめ
dc = zipWith mul das dbs
c = ntt dc n w $ drop (23 - w) invroot
-- 最後
divn = modRecip n
-- 補助関数
olol (x:xs) = x : olol (drop 1 xs)
olol _ = []
-- みかか
ntt as 1 _ _oot = take 1 as
ntt as len w (r:rs) =
zipWith add (d_evens ++ d_evens) $
zipWith mul (d_odds ++ d_odds) $
iterate (mul r) 1
where
evens = olol as
odds = olol $ tail as
len2 = div len 2
w1 = pred w
d_evens = ntt evens len2 w1 rs
d_odds = ntt odds len2 w1 rs
-- 定数準備
p = 998244353
root = cycle $ take 23 $ iterate (\x -> mul x x) (pow 3 119)
invroot = map modRecip root
-- 補助関数
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
Submission Info
| Submission Time | |
|---|---|
| Task | G - Fine Triplets |
| User | joetheshootingst |
| Language | Haskell (GHC 9.4.5) |
| Score | 0 |
| Code Size | 2357 Byte |
| Status | TLE |
| Exec Time | 3388 ms |
| Memory | 1463692 KiB |
Compile Error
app/Main.hs:24: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}
|
24 | f 1 c = div (pred c) 2
| ^^^^^^^^^^^^^^^^^^^^^^...
app/Main.hs:40:13: warning: [-Wincomplete-uni-patterns]
Pattern match(es) are non-exhaustive
In a pattern binding:
Patterns of type ‘([Int], [Int])’ not matched: (_, [])
|
40 | (xs, n:_) = span ((na + pred nb) >) $ iterate (2 *) 1
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/Main.hs:40:18: warning: [-Wname-shadowing]
This binding for ‘n’ shadows the existing binding
bound at app/Main.hs:38:10
|
40 | (xs, n:_) = span ((na + pred nb) >) $ iterate (2 *) 1
| ^
app/Main.hs:56:5: warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In an equation for ‘ntt’:
Patterns of type ‘[Int]’, ‘a’, ‘p’, ‘[Int]’ not matched:
_ p _ [] where p is not one of {1}
|
56 | ntt as 1 _ _oot = take 1 as
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^...
app/Main.hs:70:55: warning: [-Wtype-defaults]
• Defaulting the type variable ‘a0’ to type ‘Integer’ in the following constraints
(Integral a0) arising from a use of ‘pow’ at app/Main.hs:70:55-57
(Num a0) arising from the literal ‘119’ at app/Main.hs:70:61-63
• In the second argument of ‘iterate’, namely ‘(pow 3 119)’
In the second argument of ‘($)’, namely
‘iterate (\ x -> mul x x) (pow 3 119)’
In the second argument of ‘($)’, namely
‘take 23 $ iterate (\ x -> mul x x) (pow 3 119)’
|
70 | root = cycle $ take 23 $ iterate (\x -> mul x x) (pow 3 119)
| ^^^
app/Main.hs:78:14: warning: [-Wunused-matches]
Defined but not used: ‘a’
|
78 | loop a 0 = (1, 0)
| ^
app/Main.hs:78:14: warning: [-Wname-shadowing]
This binding for ‘a’ shadows the existing binding
bound at app/Main.hs:76:14
|
78 | loop a 0 = (1, 0)
| ^
app/Main.hs:79:14: warning: [-Wname-shadowing]
This binding for ‘a’ shadows the existing binding
bound at app/Main.hs:76:14
|
79 | loop a b = let (y, x) = loop b (mod a b)
| ^
Judge Result
| Set Name | Sample | All | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 600 | ||||||||
| Status |
|
|
| 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 | 7104 KiB |
| sample_02.txt | TLE | 3388 ms | 1437824 KiB |
| sample_03.txt | AC | 2 ms | 7392 KiB |
| test_01.txt | AC | 1 ms | 6976 KiB |
| test_02.txt | TLE | 3375 ms | 1195040 KiB |
| test_03.txt | AC | 2 ms | 7044 KiB |
| test_04.txt | AC | 2 ms | 6760 KiB |
| test_05.txt | TLE | 3374 ms | 1194924 KiB |
| test_06.txt | TLE | 3377 ms | 1194896 KiB |
| test_07.txt | TLE | 3375 ms | 1194952 KiB |
| test_08.txt | TLE | 3374 ms | 1195036 KiB |
| test_09.txt | AC | 3 ms | 9500 KiB |
| test_10.txt | TLE | 3380 ms | 1227776 KiB |
| test_11.txt | AC | 3 ms | 9560 KiB |
| test_12.txt | TLE | 3375 ms | 1221436 KiB |
| test_13.txt | AC | 21 ms | 20096 KiB |
| test_14.txt | TLE | 3375 ms | 1193900 KiB |
| test_15.txt | AC | 21 ms | 19080 KiB |
| test_16.txt | TLE | 3377 ms | 1184752 KiB |
| test_17.txt | AC | 926 ms | 206552 KiB |
| test_18.txt | TLE | 3377 ms | 1226696 KiB |
| test_19.txt | AC | 1016 ms | 205376 KiB |
| test_20.txt | TLE | 3376 ms | 1230788 KiB |
| test_21.txt | TLE | 3358 ms | 892228 KiB |
| test_22.txt | TLE | 3378 ms | 1280436 KiB |
| test_23.txt | TLE | 3362 ms | 943312 KiB |
| test_24.txt | TLE | 3380 ms | 1313420 KiB |
| test_25.txt | TLE | 3376 ms | 1222832 KiB |
| test_26.txt | TLE | 3375 ms | 1219188 KiB |
| test_27.txt | TLE | 3388 ms | 1463692 KiB |
| test_28.txt | TLE | 3376 ms | 1227960 KiB |
| test_29.txt | TLE | 3377 ms | 1226800 KiB |
| test_30.txt | TLE | 3377 ms | 1226888 KiB |
| test_31.txt | TLE | 3377 ms | 1226784 KiB |
| test_32.txt | TLE | 3372 ms | 1156148 KiB |
| test_33.txt | TLE | 3373 ms | 1156284 KiB |
| test_34.txt | TLE | 3376 ms | 1229412 KiB |
| test_35.txt | TLE | 3374 ms | 1190964 KiB |
| test_36.txt | TLE | 3374 ms | 1174700 KiB |
| test_37.txt | TLE | 3373 ms | 1186272 KiB |
| test_38.txt | TLE | 3375 ms | 1174396 KiB |
| test_39.txt | TLE | 3372 ms | 1159140 KiB |
| test_40.txt | TLE | 3376 ms | 1219120 KiB |