{-# LANGUAGE BangPatterns #-} import Control.Monad import Data.IORef import qualified Data.Vector.Fusion.Stream.Monadic as VFSM import qualified Data.Vector.Unboxed as VU import qualified Data.Vector.Unboxed.Mutable as VUM main :: IO () main = do k <- readLn :: IO Int ret <- newIORef (0.0 :: Double) as <- VU.unsafeThaw . VU.fromList $ ([2, 3, 5, 7, 11, 13] :: [Int]) bs <- VU.unsafeThaw . VU.fromList $ ([4, 6, 8, 9, 10, 12] :: [Int]) rep 6 $ \i -> rep 6 $ \j -> do a <- VUM.unsafeRead as i b <- VUM.unsafeRead bs j when (a * b == k) $ modifyIORef ret (+ 1.0) modifyIORef ret (\u -> u / 36) print =<< readIORef ret stream :: Monad m => Int -> Int -> VFSM.Stream m Int stream !l !r = VFSM.Stream step l where step x | x < r = return $ VFSM.Yield x (x + 1) | otherwise = return $ VFSM.Done {-# INLINE [0] step #-} {-# INLINE [1] stream #-} rep :: Monad m => Int -> (Int -> m ()) -> m () rep n = flip VFSM.mapM_ (stream 0 n)