Submission #18299760


Source Code Expand

{-# LANGUAGE BangPatterns #-}

import Data.List
import qualified Data.Vector.Unboxed as U
import Data.Vector.Unboxed ((!), Vector)

main = sol <$> get >>= print

get = map read . words <$> getLine

sol [n,m] = foldl' f (Modulo 0) [0..n]
  where
  f s k = s + comb n k*sign k*perm m k*perm (m-k) (n-k)*perm (m-k) (n-k)
  fac :: U.Vector Int
  fac = U.scanl' (((`mod` p) .) . (*)) 1 $ U.generate m succ
  perm :: Int -> Int -> Modulo
  perm n r = Modulo (fac!n)*inv (fac!(n-r))
  comb :: Int -> Int -> Modulo
  comb n r = perm n r*inv (fac!r)

inv :: Int -> Modulo
inv 1 = Modulo 1
inv k = pwr (Modulo k) (p-2)

sign :: Int -> Modulo
sign x = if odd x then Modulo (-1) else Modulo 1

newtype Modulo = Modulo {getInt :: Int} deriving (Eq)

p = 10^9+7 :: Int

instance Num Modulo where
  Modulo a+Modulo b = Modulo ((a+b) `mod` p)
  Modulo a-Modulo b = Modulo ((a-b) `mod` p)
  Modulo a*Modulo b = Modulo ((a*b) `mod` p)
  abs = undefined
  signum = undefined
  fromInteger i = Modulo (fromIntegral i `mod` p)

instance Show Modulo where
  show (Modulo i) = show i

pwr :: Modulo -> Int -> Modulo
pwr !a !n
  | n == 0    = 1
  | odd n     = a*r
  | otherwise = r
  where
  q = pwr a (n `div` 2)
  r = q*q

Submission Info

Submission Time
Task E - NEQ
User karoyakani
Language Haskell (GHC 8.8.3)
Score 500
Code Size 1249 Byte
Status AC
Exec Time 1368 ms
Memory 8996 KiB

Compile Error

Loaded package environment from /home/contestant/.ghc/x86_64-linux-8.8.3/environments/default

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 12
Set Name Test Cases
Sample sample00, sample01, sample02
All handmade03, handmade04, handmade05, handmade06, handmade07, random08, random09, random10, random11, sample00, sample01, sample02
Case Name Status Exec Time Memory
handmade03 AC 7 ms 3900 KiB
handmade04 AC 18 ms 8020 KiB
handmade05 AC 13 ms 6036 KiB
handmade06 AC 20 ms 7356 KiB
handmade07 AC 1193 ms 8996 KiB
random08 AC 1368 ms 8952 KiB
random09 AC 477 ms 6700 KiB
random10 AC 608 ms 8516 KiB
random11 AC 163 ms 6684 KiB
sample00 AC 2 ms 4008 KiB
sample01 AC 2 ms 4020 KiB
sample02 AC 460 ms 8032 KiB