|
| 1 | +-- https://github.com/minoki/my-atcoder-solutions |
| 2 | +{-# LANGUAGE TypeApplications #-} |
| 3 | +{-# LANGUAGE BangPatterns #-} |
| 4 | +{-# LANGUAGE TypeFamilies #-} |
| 5 | +import Data.Char (isSpace) |
| 6 | +import Data.Int (Int64) |
| 7 | +import Data.List (unfoldr) |
| 8 | +import Control.Monad |
| 9 | +import qualified Data.Vector.Unboxing as U |
| 10 | +import qualified Data.Vector.Unboxing.Mutable as UM |
| 11 | +import qualified Data.ByteString.Char8 as BS |
| 12 | + |
| 13 | +main = do |
| 14 | + [n,k] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine |
| 15 | + let v :: U.Vector IntMod |
| 16 | + v = U.create $ do |
| 17 | + v <- UM.replicate (k+1) 0 |
| 18 | + forM_ [1..k] $ \i -> do |
| 19 | + UM.write v i $! fromIntegral (k `quot` i) ^ n |
| 20 | + return v |
| 21 | + w :: U.Vector IntMod |
| 22 | + w = U.create $ do |
| 23 | + w <- U.thaw v |
| 24 | + forM_ [k,k-1..1] $ \i -> do |
| 25 | + s <- sum <$> sequence [ UM.read w j | j <- [2*i,3*i..k] ] |
| 26 | + UM.modify w (subtract s) i |
| 27 | + return w |
| 28 | + print $ sum [ fromIntegral i * w U.! i | i <- [1..k] ] |
| 29 | + |
| 30 | +modulus :: Int64 |
| 31 | +modulus = 10^9 + 7 |
| 32 | + |
| 33 | +newtype IntMod = IntMod { getIntMod :: Int64 } deriving Eq |
| 34 | + |
| 35 | +instance Show IntMod where |
| 36 | + show (IntMod x) = show x |
| 37 | + |
| 38 | +instance Num IntMod where |
| 39 | + IntMod x + IntMod y = IntMod ((x + y) `rem` modulus) |
| 40 | + IntMod x - IntMod y = IntMod ((x - y) `mod` modulus) |
| 41 | + IntMod x * IntMod y = IntMod ((x * y) `rem` modulus) |
| 42 | + negate (IntMod x) = IntMod (negate x `mod` modulus) |
| 43 | + fromInteger x = IntMod (fromInteger (x `mod` fromIntegral modulus)) |
| 44 | + abs = undefined; signum = undefined |
| 45 | + |
| 46 | +{-# RULES |
| 47 | +"fromIntegral/Int64->IntMod" forall (x :: Int64). |
| 48 | + fromIntegral x = IntMod (x `mod` modulus) |
| 49 | +"fromIntegral/Int->IntMod" forall (x :: Int). |
| 50 | + fromIntegral x = IntMod (fromIntegral x `mod` modulus) |
| 51 | + #-} |
| 52 | + |
| 53 | +instance U.Unboxable IntMod where |
| 54 | + type Rep IntMod = Int64 |
0 commit comments