import Control.Monad import Data.Bits modv :: Int modv = 2147483647 gen :: Int -> Int -> [Int] gen mulv startv = lowbits r : gen mulv r where r = mulv * startv `rem` modv gen2 :: Int -> Int -> Int -> [Int] gen2 checkv mulv startv = if l .&. checkv == 0 then l : rest else rest where r = mulv * startv `rem` modv l = lowbits r rest = gen2 checkv mulv r lowbits :: Int -> Int lowbits n = n .&. 0xffff main :: IO () main = do [sA, sB] <- liftM (map (read . last . words) . lines) (readFile "15.in") print $ sum $ map (fromEnum . uncurry (==)) $ zip (take 40000000 $ gen 16807 sA) (take 40000000 $ gen 48271 sB) print $ sum $ map (fromEnum . uncurry (==)) $ zip (take 5000000 $ gen2 3 16807 sA) (take 5000000 $ gen2 7 48271 sB)