Skip to content

Instantly share code, notes, and snippets.

@roman
Created Feb 1, 2017
Embed
What would you like to do?
module Main where
import Control.Applicative
import Control.Monad (forM, replicateM_)
import qualified Data.Vector (freeze)
import qualified Data.Vector.Unboxed as V
import qualified Data.Vector.Unboxed.Mutable as VM
mkPolygonVector :: IO (V.Vector Int)
mkPolygonVector = do
polVec <- VM.replicate (10^5 + 1) 0
VM.write polVec 0 1
VM.write polVec 1 5
forM [2..10^5] $ \i -> do
prev <- VM.read polVec (pred i)
VM.write polVec i (prev + 1 + (i * 3))
V.freeze polVec
main :: IO ()
main = do
n <- readLn :: IO Int
polygonVec <- mkPolygonVector
replicateM_ n $ do
i <- readLn :: IO Int
print $ polygonVec V.! i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment