Skip to content

Instantly share code, notes, and snippets.

@roman
Created February 1, 2017 03:33
Show Gist options
  • Save roman/8cd0aeac252fe86b875411f8aa8c0d20 to your computer and use it in GitHub Desktop.
Save roman/8cd0aeac252fe86b875411f8aa8c0d20 to your computer and use it in GitHub Desktop.
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