Submission #62745146
Source Code Expand
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List
import Data.Array.Unboxed
import Data.Bits
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 = elems $ 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] -> UArray Int Int
convolution as bs = c3
where
-- 定数
p = 998244353
root = 3
-- p = 1541406721 -- こっちだと結果がおかしい
-- root = 103
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 + pred nb) >) $ iterate (2 *) 1
bnds = (0, pred n)
a0 = listArray bnds $ as ++ repeat 0
b0 = listArray bnds $ bs ++ repeat 0
a1 = fft a0 root
b1 = fft b0 root
c1 = listArray bnds $ zipWith mul (elems a1) (elems b1)
c2 = fft c1 invroot
divn = modRecip n
c3 = amap (mul divn) c2
-- 下請け
-- ビット反転
bitReverse :: Int -> Int
bitReverse j = sum [bit i | i <- [0 .. pred w], testBit j (pred w - i)]
-- 配列ビット反転
bitSwap = ixmap bnds bitReverse
-- 高速フーリエ変換
fft :: UArray Int Int -> Int -> UArray Int Int
fft a base = foldl' step (bitSwap a) $ zip omega2N $ iterate (2 *) 1
where
omegaN = pow base (div (pred p) n)
omega2N = reverse $ take w $ iterate (\x -> mul x x) omegaN
step arr (omega, dist) = array bnds $ concat
[ [(i, add f_even $ mul o1 f_odd), (i + dist, add f_even $ mul o2 f_odd)]
| b <- [0, k .. pred n]
, (o1, o2, i) <- zip3 omegas1 omegas2 [b .. b + pred dist]
, let f_even = arr ! i
, let f_odd = arr ! (i + dist)
]
where
k = dist + dist
m = div n k
(omegas1, omegas2) = splitAt dist $ iterate (mul omega) 1
Submission Info
| Submission Time | |
|---|---|
| Task | G - Fine Triplets |
| User | joetheshootingst |
| Language | Haskell (GHC 9.4.5) |
| Score | 600 |
| Code Size | 2809 Byte |
| Status | AC |
| Exec Time | 2831 ms |
| Memory | 368884 KiB |
Compile Error
app/Main.hs:25: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}
|
25 | f 1 c = div (pred c) 2
| ^^^^^^^^^^^^^^^^^^^^^^...
app/Main.hs:46:14: warning: [-Wunused-matches]
Defined but not used: ‘a’
|
46 | loop a 0 = (1, 0)
| ^
app/Main.hs:46:14: warning: [-Wname-shadowing]
This binding for ‘a’ shadows the existing binding
bound at app/Main.hs:44:14
|
46 | loop a 0 = (1, 0)
| ^
app/Main.hs:47:14: warning: [-Wname-shadowing]
This binding for ‘a’ shadows the existing binding
bound at app/Main.hs:44:14
|
47 | loop a b = let (y, x) = loop b (mod a b)
| ^
app/Main.hs:58:9: warning: [-Wincomplete-uni-patterns]
Pattern match(es) are non-exhaustive
In a pattern binding:
Patterns of type ‘([Int], [Int])’ not matched: (_, [])
|
58 | (xs, n:_) = span ((na + pred nb) >) $ iterate (2 *) 1
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/Main.hs:58:14: warning: [-Wname-shadowing]
This binding for ‘n’ shadows the existing binding
bound at app/Main.hs:56:6
|
58 | (xs, n:_) = span ((na + pred nb) >) $ iterate (2 *) 1
| ^
app/Main.hs:89:13: warning: [-Wunused-local-binds]
Defined but not used: ‘m’
|
89 | m = div n k
| ^
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 600 / 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 | 6992 KiB |
| sample_02.txt | AC | 1226 ms | 189700 KiB |
| sample_03.txt | AC | 1 ms | 7024 KiB |
| test_01.txt | AC | 1 ms | 7008 KiB |
| test_02.txt | AC | 2720 ms | 368028 KiB |
| test_03.txt | AC | 1 ms | 6796 KiB |
| test_04.txt | AC | 1 ms | 6940 KiB |
| test_05.txt | AC | 2710 ms | 367888 KiB |
| test_06.txt | AC | 2691 ms | 367868 KiB |
| test_07.txt | AC | 2670 ms | 367976 KiB |
| test_08.txt | AC | 2771 ms | 367108 KiB |
| test_09.txt | AC | 2 ms | 7648 KiB |
| test_10.txt | AC | 2811 ms | 367980 KiB |
| test_11.txt | AC | 2 ms | 7652 KiB |
| test_12.txt | AC | 2831 ms | 368884 KiB |
| test_13.txt | AC | 4 ms | 11644 KiB |
| test_14.txt | AC | 2791 ms | 367900 KiB |
| test_15.txt | AC | 4 ms | 11652 KiB |
| test_16.txt | AC | 2790 ms | 368380 KiB |
| test_17.txt | AC | 31 ms | 17236 KiB |
| test_18.txt | AC | 2730 ms | 367036 KiB |
| test_19.txt | AC | 31 ms | 16936 KiB |
| test_20.txt | AC | 2810 ms | 367452 KiB |
| test_21.txt | AC | 262 ms | 57172 KiB |
| test_22.txt | AC | 2659 ms | 320864 KiB |
| test_23.txt | AC | 252 ms | 49036 KiB |
| test_24.txt | AC | 2649 ms | 320840 KiB |
| test_25.txt | AC | 2690 ms | 368048 KiB |
| test_26.txt | AC | 2679 ms | 321832 KiB |
| test_27.txt | AC | 2740 ms | 357708 KiB |
| test_28.txt | AC | 2800 ms | 367008 KiB |
| test_29.txt | AC | 2730 ms | 368104 KiB |
| test_30.txt | AC | 2750 ms | 368000 KiB |
| test_31.txt | AC | 2740 ms | 368056 KiB |
| test_32.txt | AC | 2699 ms | 338412 KiB |
| test_33.txt | AC | 2718 ms | 304484 KiB |
| test_34.txt | AC | 2749 ms | 304440 KiB |
| test_35.txt | AC | 2759 ms | 338416 KiB |
| test_36.txt | AC | 2730 ms | 338312 KiB |
| test_37.txt | AC | 2739 ms | 338240 KiB |
| test_38.txt | AC | 2749 ms | 338248 KiB |
| test_39.txt | AC | 2760 ms | 338228 KiB |
| test_40.txt | AC | 2760 ms | 338164 KiB |