Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Control.Applicative
import Control.Arrow
import Control.Monad.Primitive
import Control.Monad.ST
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
import qualified Data.ByteString.Char8 as B
(|>) :: a -> (a -> b) -> b
(|>) x y = y x
infixl 0 |>
readInts :: B.ByteString -> [Int64]
readInts = B.split ' ' >>> mapMaybe (B.readInt >>> liftA fst) >>>
map fromIntegral
main :: IO ()
main = do
[n, m] <- (splitOn " " >>> map read) <$> getLine
inputLines <- B.split '\n' <$> B.getContents
let [a, b, c] = map readInts inputLines
putStrLn $ concat . intersperse " " . map show $ updateA2 a b c n
updateA2 :: [Int64] -> [Int64] -> [Int64] -> Int64 -> [Int64]
updateA2 as bs cs n = result
where factorsFromA = zip [1..n] as
contributesFromBC = zip bs cs
factorsFromBC = (compress contributesFromBC) >>= (generateMultiples n)
allFactors = factorsFromA ++ factorsFromBC
(_, result) = unzip (compress allFactors)
compress :: Integral a => [(a,a)] -> [(a,a)]
compress = compressInner . sort
compressInner :: Integral a => [(a,a)] -> [(a,a)]
compressInner (x: []) = [x]
compressInner ((current,currentValue) : xs) = if (current == i)
then (i, limit (v * currentValue)) : tail
else (current, currentValue) : (i, v) : tail
where ((i, v):tail) = compressInner xs
limit :: Integral a => a -> a
limit x = mod x 1000000007
generateMultiples :: Integral a => a -> (a,a) -> [(a,a)]
generateMultiples n (index, value) = map (\i -> (i,value)) indexValues
where indexValues = [index * i | i <- [1..(div n index)]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.