Skip to content

Instantly share code, notes, and snippets.

@Dobiasd
Created September 22, 2014 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dobiasd/fca81f62c39f16a1744f to your computer and use it in GitHub Desktop.
Save Dobiasd/fca81f62c39f16a1744f to your computer and use it in GitHub Desktop.
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