Bertown is under siege! The attackers have blocked all the ways out and their cannon is bombarding the city. Fortunately, Berland intelligence managed to intercept the enemies' shooting plan. Let's introduce the Cartesian system of coordinates, the origin of which coincides with the cannon's position, the Ox axis is directed rightwards in the city's direction, the Oy axis is directed upwards (to the sky). The cannon will make n more shots. The cannon balls' initial speeds are the same in all the shots and are equal to V, so that every shot is characterized by only one number alphai which represents the angle at which the cannon fires. Due to the cannon's technical peculiarities this angle does not exceed 45 angles (π / 4). We disregard the cannon sizes and consider the firing made from the point (0, 0).The balls fly according to the known physical laws of a body thrown towards the horizon at an angle: vx(t) = V·cos(alpha) vy(t) = V·sin(alpha)  –  g·t x(t) = V·cos(alpha)·t y(t) = V·sin(alpha)·t  –  g·t2 / 2Think of the acceleration of gravity g as equal to 9.8.Bertown defends m walls. The i-th wall is represented as a vertical segment (xi, 0) - (xi, yi). When a ball hits a wall, it gets stuck in it and doesn't fly on. If a ball doesn't hit any wall it falls on the ground (y = 0) and stops. If the ball exactly hits the point (xi, yi), it is considered stuck. Your task is to find for each ball the coordinates of the point where it will be located in the end.

The first line contains integers n and V (1 ≤ n ≤ 104, 1 ≤ V ≤ 1000) which represent the number of shots and the initial speed of every ball. The second line contains n space-separated real numbers alphai (0 < alphai < π / 4) which represent the angles in radians at which the cannon will fire. The third line contains integer m (1 ≤ m ≤ 105) which represents the number of walls. Then follow m lines, each containing two real numbers xi and yi (1 ≤ xi ≤ 1000, 0 ≤ yi ≤ 1000) which represent the wall’s coordinates. All the real numbers have no more than 4 decimal digits. The walls may partially overlap or even coincide.

Print n lines containing two real numbers each — calculate for every ball the coordinates of its landing point. Your answer should have the relative or absolute error less than 10 - 4.

import Data.List (sort)
import Data.Maybe (fromJust)
import qualified Data.ByteString.Char8 as C

readInt = fst . fromJust . C.readInt

readDouble bs =
  if C.null bs'
    then fromIntegral a
    else fromIntegral (a * c + b) / fromIntegral c
  where
    Just (a, bs') = C.readInt bs
    t = C.tail bs'
    b = readInt t
    c = 10 ^ C.length t

pairs (x:y:z) = (x, y): pairs z
pairs _ = []

main = do
  (n':v':input) <- fmap C.words C.getContents
  let n = readInt n'
      v = readInt v'
      (a',(m':z')) = splitAt n input
  let a = map readDouble a'
      m = readInt m'
      z = map readDouble z'
  putStrLn $ unwords $ map show $ concatMap snd $ sort $
    gao n (fromIntegral v) (zip a [0 ..]) $ pairs z

gao :: Int -> Double -> [(Double, Int)] -> [(Double, Double)] -> [(Int, [Double])]
gao n v as zs = gao' (sort as) (sort zs) where
  eps = 1e-8
  g = 9.8
  gao' [] _ = []
  gao' ((a,i):a') z = (i, xy) : gao' a' z' where
    vx = v * cos(a)
    vy = v * sin(a)
    yt x = let t = x / vx in vy * t - g * t * t / 2
    z' = dropWhile (\(x, y) -> y < yt x + eps) z
    xy =
      if null z' || y < -eps
        then [v * v * sin(2 * a) / g, 0]
        else let x = fst $ head z' in [x, y]
      where
        x = fst $ head z'
        y = yt x

{- Time limit exceeded on test 7 -}
{- PS: if yt x < 0 .... WA -}

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float
import Data.Char (isDigit)

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: Num a => L.ByteString -> a
readInteger x =
  case L.readInteger x of Just (i,_) -> fromIntegral i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
                   i = if L.null integer then 0 else readInteger integer
                   f = if L.null fractional then 0 else readInteger fractional
               in  if not (L.elem '.' s)
                     then readInteger s
                     else i + 0.1 ^ L.length (L.takeWhile isDigit fractional) * f

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt . head . drop (n+1) $ l
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStrLn (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10    = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10 ^ toInteger prec :: Integer
          f n m | n < 0     = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map (dshow 6))

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

{-# LANGUAGE ForeignFunctionInterface #-}

import qualified Data.ByteString.Lazy.Char8 as LB
import qualified Data.ByteString as SB
import Data.ByteString.Internal (inlinePerformIO)
import Foreign.C.String (CString)
import Foreign.C (CDouble)

import GHC.Exts
import Data.Maybe (fromJust)
import GHC.Float (int2Double)
import Data.List (sort)
import Text.Printf (printf)

foreign import ccall unsafe "stdlib.h atof" c_atof :: CString -> IO Double
unsafeReadDouble = inlinePerformIO . flip SB.useAsCString c_atof
{-# INLINE unsafeReadDouble #-}
readDouble = unsafeReadDouble . SB.concat . LB.toChunks
readInt = fst . fromJust . LB.readInt

main = do str <- LB.getContents
          let l = LB.lines str
              [n,v] = map readInt . LB.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt $ l !! (n+1)
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = LB.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          mapM_ (uncurry (printf "%.6f %.6f\n") ) sol

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map snd res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import Text.Printf
import GHC.Exts
import GHC.Float
import Data.Char
import Data.Maybe

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt $ l !! (n+1)
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          mapM_ (uncurry (printf "%.6f %.6f\n") ) sol

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map snd res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

readInt = fst . fromJust . L.readInt
readInteger = fromIntegral . fst . fromJust . L.readInteger
readDouble s =
  let [si, sf] = L.split '.' s
      i = if L.null si then 0 else readInteger si
      f = if L.null sf then 0 else readInteger sf
  in  if not (L.elem '.' s)
        then readInteger s
        else i + 0.1 ^ L.length (L.takeWhile isDigit sf) * f

import Data.List (sort)
import Data.Maybe (fromJust)
import qualified Data.ByteString.Char8 as C

readInt = fst . fromJust . C.readInt

readDouble bs =
  if C.null bs'
    then fromIntegral a
    else fromIntegral a + fromIntegral b / fromIntegral c
  where
    Just (a, bs') = C.readInt bs
    t = C.tail bs'
    b = readInt t
    c = 10 ^ C.length t

pairs (x:y:z) = (x, y): pairs z
pairs _ = []

main = do
  (n':v':input) <- fmap C.words C.getContents
  let n = readInt n'
      v = readInt v'
      (a',(m':z')) = splitAt n input
  let a = map readDouble a'
      m = readInt m'
      z = map readDouble z'
  putStrLn $ unwords $ map show $ concatMap snd $ sort $
    gao n (fromIntegral v) (zip a [0 ..]) $ pairs z

gao :: Int -> Double -> [(Double, Int)] -> [(Double, Double)] -> [(Int, [Double])]
gao n v as zs = gao' (sort as) (sort zs) where
  eps = 1e-6
  g = 9.8
  gao' [] _ = []
  gao' ((a,i):a') z = (i, xy) : gao' a' z' where
    vx = v * cos(a)
    vy = v * sin(a)
    yt x = let t = x / vx in vy * t - g * t * t / 2
    z' = dropWhile (\(x, y) -> y < yt x + eps) z --WA
    xy = if null z' then [v * v * sin(2 * a) / g, 0] else let x = fst $ head z' in [x, yt x] --WA

{- Time limit exceeded on test 7 -}
{- PS: if yt x < 0 .... WA -}

import Data.List (sort)
import Data.Maybe (fromJust)
import qualified Data.ByteString.Char8 as C

readInt = fst . fromJust . C.readInt

readDouble bs =
  if C.null bs'
    then fromIntegral a
    else fromIntegral (a * c + b) / fromIntegral c
  where
    Just (a, bs') = C.readInt bs
    t = C.tail bs'
    b = readInt t
    c = 10 ^ C.length t

pairs (x:y:z) = (x, y): pairs z
pairs _ = []

main = do
  (n':v':input) <- fmap C.words C.getContents
  let n = readInt n'
      v = readInt v'
      (a',(m':z')) = splitAt n input
  let a = map readDouble a'
      m = readInt m'
      z = map readDouble z'
  putStrLn $ unwords $ map show $ concatMap snd $ sort $
    gao n (fromIntegral v) (zip a [0 ..]) $ pairs z

gao :: Int -> Double -> [(Double, Int)] -> [(Double, Double)] -> [(Int, [Double])]
gao n v as zs = gao' (sort as) (sort zs) where
  eps = 1e-6
  g = 9.8
  gao' [] _ = []
  gao' ((a,i):a') z = (i, xy) : gao' a' z' where
    vx = v * cos(a)
    vy = v * sin(a)
    yt x = let t = x / vx in vy * t - g * t * t / 2
    z' = dropWhile (\(x, y) -> y < yt x + eps) z --WA
    xy =
      if null z' || y < -eps
        then [v * v * sin(2 * a) / g, 0]
        else let x = fst $ head z' in [x, y] --WA
      where
        x = fst $ head z'
        y = yt x

{- Time limit exceeded on test 7 -}
{- PS: if yt x < 0 .... WA -}

{-# LANGUAGE ForeignFunctionInterface #-}

import Data.List
import Data.Maybe (catMaybes, mapMaybe)
import System.Random
import qualified Data.ByteString.Lazy.Char8 as L
import qualified Data.ByteString.Char8 as B
import Foreign.C.Types
import Foreign.C.String
import System.IO.Unsafe
import Text.Printf
import GHC.Show (intToDigit)
--import Data.ByteString.Lex.Lazy.Double (readDouble)

--import qualified Data.Text.Read as T
import GHC.Exts
import GHC.Float

foreign import ccall unsafe "stdlib.h atof" c_atof :: CString -> IO CDouble
readDouble :: B.ByteString -> Double
readDouble = realToFrac . unsafePerformIO . flip B.useAsCString c_atof

main = interact $ output . solve . parse . lines

--main = do let r = mkStdGen 146
--              lst = iterate (\(x,r) -> random r) (int2Double 1, r)
--          putStrLn . unlines . map(show . fst) $ take 200000 lst

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
           | otherwise = reverse (f n)
           where
            f n | n < 10 = [intToDigit (fromIntegral n)]
                | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x | x < 0  = '-' : dshow prec (-x)
dshow prec x = ishow first ++ ('.' : ishow second)
    where m = toInteger 10^ toInteger prec :: Integer
          n = round (x * fromIntegral m)
          (first, second) = divMod n m

--main = interact $ output' . parse' . lines

--parse' = map fst . mapMaybe ( L.readInt . L.pack )
parse' = map (readDouble . B.pack)
--output' = unlines . map (printf "%.3f")
output' = unlines . map (dshow 4)

solve' = takeWhile (/= 42)

output = unlines . map (unwords . map (dshow 9))

parse (nv:rest) = let [n,v] = map read . words $ nv
                      angles = map (readDouble . B.pack) (take n rest)
                      walls = [(x,y) | s <- drop (n+1) rest, let w = words s,
                                let x = read (head w), let y = read (last w)]
                  in (int2Double v, angles, sort walls)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: Num a => L.ByteString -> a
readInteger x =
  case L.readInteger x of Just (i,_) -> fromIntegral i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
                   i = if L.null integer then 0 else readInteger integer
                   f = if L.null fractional then 0 else readInteger fractional
               in  if not (L.elem '.' s)
                     then readInteger s
                     else i + 0.1 ^ L.length fractional * f

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt . head . drop (n+1) $ l
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStrLn (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10    = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0     = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map (dshow 6))

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

{-# LANGUAGE ForeignFunctionInterface #-}

import Data.List
import Data.Maybe (catMaybes, mapMaybe)
import System.Random
import qualified Data.ByteString.Lazy.Char8 as L
import qualified Data.ByteString.Char8 as B
import Foreign.C.Types
import Foreign.C.String
import System.IO.Unsafe
import Text.Printf
import GHC.Show (intToDigit)
--import Data.ByteString.Lex.Lazy.Double (readDouble)

--import qualified Data.Text.Read as T
import GHC.Exts
import GHC.Float

foreign import ccall unsafe "stdlib.h atof" c_atof :: CString -> IO CDouble
readDouble :: B.ByteString -> Double
readDouble = realToFrac . unsafePerformIO . flip B.useAsCString c_atof

main = interact $ output . solve . parse . lines

--main = do let r = mkStdGen 146
--              lst = iterate (\(x,r) -> random r) (int2Double 1, r)
--          putStrLn . unlines . map(show . fst) $ take 200000 lst

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
           | otherwise = reverse (f n)
           where
            f n | n < 10 = [intToDigit (fromIntegral n)]
                | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x | x < 0  = '-' : dshow prec (-x)
dshow prec x = ishow first ++ ('.' : ishow second)
    where m = toInteger 10^ toInteger prec :: Integer
          n = round (x * fromIntegral m)
          (first, second) = divMod n m

--main = interact $ output' . parse' . lines

--parse' = map fst . mapMaybe ( L.readInt . L.pack )
parse' = map (readDouble . B.pack)
--output' = unlines . map (printf "%.3f")
output' = unlines . map (dshow 4)

solve' = takeWhile (/= 42)

output = unlines . map (unwords . map (dshow 6))

parse (nv:rest) = let [n,v] = map read . words $ nv
                      angles = map (readDouble . B.pack) (take n rest)
                      walls = [(x,y) | s <- drop (n+1) rest, let w = words s,
                                let x = read (head w), let y = read (last w)]
                  in (int2Double v, angles, sort walls)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

{-# LANGUAGE ForeignFunctionInterface #-}

import qualified Data.ByteString.Lazy.Char8 as LB
import qualified Data.ByteString as SB
import Data.ByteString.Internal (inlinePerformIO)
import Foreign.C.String (CString)
import Foreign.C (CDouble)
import Data.Maybe (fromJust)

foreign import ccall unsafe "stdlib.h atof" c_atof :: CString -> IO Double
unsafeReadDouble = inlinePerformIO . flip SB.useAsCString c_atof
{-# INLINE unsafeReadDouble #-}
readDouble = unsafeReadDouble . SB.concat . LB.toChunks
readInt = fst . fromJust . LB.readInt

main = LB.getContents >>= (print . sum . map readDouble . LB.lines)

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: L.ByteString -> Integer
readInteger x =
  case L.readInteger x of Just (i,_) -> i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
               in fromIntegral (readInteger integer) +
                    0.1 ^ L.length fractional * fromIntegral (readInteger fractional)

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble (take n (tail l))
              walls = [(x,y) | s <- drop (n+2) l, let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStr (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10 = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0 = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map (dshow 6))

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: L.ByteString -> Integer
readInteger x =
  case L.readInteger x of Just (i,_) -> i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
               in fromIntegral (readInteger integer) +
                    0.1 ^ L.length fractional * fromIntegral (readInteger fractional)

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              walls = [(x,y) | s <- drop (n+2) l, let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStr (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10 = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0 = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map show)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: L.ByteString -> Integer
readInteger x =
  case L.readInteger x of Just (i,_) -> i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
               in fromIntegral (readInteger integer) +
                    0.1 ^ L.length fractional * fromIntegral (readInteger fractional)

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              walls = [(x,y) | s <- drop (n+2) l, let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStrLn (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10 = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0 = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map show)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: Num a => L.ByteString -> a
readInteger x =
  case L.readInteger x of Just (i,_) -> fromIntegral i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
                   i = if L.null integer then 0 else readInteger integer
                   f = if L.null fractional then 0 else readInteger fractional
               in i + 0.1 ^ L.length fractional * f

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt . head . drop (n+1) $ l
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStrLn (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10    = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0     = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map (dshow 6))

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: L.ByteString -> Integer
readInteger x =
  case L.readInteger x of Just (i,_) -> i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
               in fromIntegral (readInteger integer) +
                    0.1 ^ L.length fractional * fromIntegral (readInteger fractional)

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt . head . drop (n+1) $ l
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStrLn (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10 = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0 = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map show)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import GHC.Exts
import GHC.Float

main = interact $ output . solve . parse . lines

output = unlines . map (unwords . map show)

parse (nv:rest) = let [n,v] = map read . words $ nv
                      angles = map read (take n rest)
                      walls = [(x,y) | s <- drop (n+1) rest, let w = words s,
                                let x = read (head w), let y = read (last w)]
                  in (int2Double v, angles, sortWith fst walls)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0  = freeFall v (n, a) : doSolve v as walls
 | y1 > y  = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as ws
 where y1 = v * t * sin a - g * t * t / 2.0
       t = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import GHC.Exts
import GHC.Float

main = interact $ output . solve . parse . lines

output = unlines . map (unwords . map show)

parse (nv:rest) = let [n,v] = map read . words $ nv
                      angles = map read (take n rest)
                      walls = [(x,y) | s <- drop (n+1) rest, let w = words s,
                                let x = read (head w), let y = read (last w)]
                  in (int2Double v, angles, sort walls)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0  = freeFall v (n, a) : doSolve v as walls
 | y1 > y  = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as ws
 where y1 = v * t * sin a - g * t * t / 2.0
       t = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g


Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise.You are given geometric progression b defined by two integers b1 and q. Remind that a geometric progression is a sequence of integers b1, b2, b3, ..., where for each i > 1 the respective term satisfies the condition bi = bi - 1·q, where q is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both b1 and q can equal 0. Also, Dvastan gave Masha m "bad" integers a1, a2, ..., am, and an integer l.Masha writes all progression terms one by one onto the board (including repetitive) while condition |bi| ≤ l is satisfied (|x| means absolute value of x). There is an exception: if a term equals one of the "bad" integers, Masha skips it (doesn't write onto the board) and moves forward to the next term.But the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print "inf" in case she needs to write infinitely many integers.

The first line of input contains four integers b1, q, l, m (-109 ≤ b1, q ≤ 109, 1 ≤ l ≤ 109, 1 ≤ m ≤ 105) — the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of "bad" integers, respectively. The second line contains m distinct integers a1, a2, ..., am (-109 ≤ ai ≤ 109) — numbers that will never be written on the board.

Print the only integer, meaning the number of progression terms that will be written on the board if it is finite, or "inf" (without quotes) otherwise.

NoteIn the first sample case, Masha will write integers 3, 12, 24. Progression term 6 will be skipped because it is a "bad" integer. Terms bigger than 24 won't be written because they exceed l by absolute value.In the second case, Masha won't write any number because all terms are equal 123 and this is a "bad" integer.In the third case, Masha will write infinitely integers 123.

import qualified Data.ByteString.Char8 as BS
import           Data.List             (unfoldr)

main = do
  [b,q,l,_m] <- getInts
  allow <-fmap ((not.) .flip elem) getInts
  let good num = abs num <= l && allow num
      inf num | good num = "inf"
              | otherwise = "0"
      result | abs b > l = "0"
             | q == 0 && good 0 = "inf"
             | q == 0 && good b = "1"
             | q == 0           = "0"
             | q == 1  = inf b
             | q == -1 = inf b `max` inf (-b)
             | b == 0  = inf 0
             | otherwise = show $ length $ filter allow $ takeWhile ((<= l).abs) $ iterate (* q) b
  putStrLn result
  where
  getInts = fmap (unfoldr readOne) BS.getLine where
    readOne s = do
      (x , s') <- BS.readInteger s
      return (x, BS.drop 1 s')

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fst. fromJust . C.readInteger

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if abs b <= l then Just 1 else Just 0
             | not (0 `elem` as) && abs b > l = Just 0
             | otherwise = Nothing
run b 1 l as | b `elem` as || abs b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && abs b <= l    = Nothing
                | not ((-b) `elem` as) && abs b <= l = Nothing
                | otherwise = Just 0
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = filter ((<= l) . abs) (until ((> l) . abs . last) (\x -> x ++ [last x * q]) [b])

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (take (fromIntegral m) . map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

import Control.Applicative
import Data.List
import Data.Int

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int64] -> Int64
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ findPos b (sort xs))==0) then 0 else 2000000000
solve' (0:_:l:m:xs) = if ((fst $ findPos 0 (sort xs))==0) then 0 else 2000000000
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ findPos b (sort xs))==0) && ((fst $ findPos (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = findPos b xs'
                                                    (c2,_) = findPos 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0

solve' (b:q:l:m:xs) = let (xsPos,xsNeg) = separe xs
                      in solve b q l 0 (sort xsPos) (reverse $ sort xsNeg)

solve b q l c xs xs' = if (b>l || (-1)*b>l) then c else if b>=0 then do let (cm,nl) = findPos b xs
                                                                        solve (b*q) q l (c+cm) nl xs'
                                                        else do let (cm2,nls) = findNeg b xs'
                                                                solve (b*q) q l (c+cm2) xs nls

toString n = if(n==2000000000) then "inf" else show n

separe = foldl (\(p,n) e-> if(e<0) then (p,e:n) else (e:p,n)) ([],[])

findPos _ []  = (1,[])
findPos e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else findPos e xs

findNeg _ []  = (1,[])
findNeg e (x:xs) = if (x==e) then (0,xs) else if(x<e) then (1,x:xs) else findNeg e xs

import qualified Data.ByteString.Char8 as BS
import           Data.List             (unfoldr)

main = do
  [b,q,l,_m] <- getInts
  allow <-fmap ((not.) .flip elem) getInts
  let good num = abs num < l && allow num
      inf num | good num = "inf"
              | otherwise = "0"
      result | abs b >= l = "0"
             | q == 0 && good 0 = "inf"
             | q == 0 && good b = "1"
             | q == 0           = "0"
             | q == 1  = inf b
             | q == -1 = inf b `max` inf (-b)
             | b == 0  = inf 0
             | otherwise = show $ length $ filter allow $ takeWhile ((<= l).abs) $ iterate (* q) b
  putStrLn result
  where
  getInts = fmap (unfoldr readOne) BS.getLine where
    readOne s = do
      (x , s') <- BS.readInt s
      return (x, BS.drop 1 s')

import qualified Data.ByteString.Char8 as BS
import           Data.List             (unfoldr)

main = do
  [b,q,l,_m] <- getInts
  allow <-fmap ((not.) .flip elem) getInts
  let good num = abs num <= l && allow num
      inf num | good num = "inf"
              | otherwise = "0"
      result | abs b > l = "0"
             | q == 0 && good 0 = "inf"
             | q == 0 && good b = "1"
             | q == 0           = "0"
             | q == 1  = inf b
             | q == -1 = inf b `max` inf (-b)
             | b == 0  = inf 0
             | otherwise = show $ length $ filter allow $ takeWhile ((<= l).abs) $ iterate (* q) b
  putStrLn result
  where
  getInts = fmap (unfoldr readOne) BS.getLine where
    readOne s = do
      (x , s') <- BS.readInt s
      return (x, BS.drop 1 s')

import qualified Data.ByteString.Char8 as BS
import           Data.List             (unfoldr)

main = do
  [b,q,l,_m] <- getInts
  allow <-fmap ((not.) .flip elem) getInts
  let good num = abs num < l && allow num
      inf num | good num = "inf"
              | otherwise = "0"
      result | abs b >= l = "0"
             | q == 0 && good 0 = "inf"
             | q == 0 && good b = "1"
             | q == 0           = "0"
             | q == 1  = inf b
             | q == -1 = inf b `max` inf (-b)
             | b == 0  = inf 0
             | otherwise = show $ length $ filter allow $ takeWhile ((< l).abs) $ iterate (* q) b
  putStrLn result
  where
  getInts = fmap (unfoldr readOne) BS.getLine where
    readOne s = do
      (x , s') <- BS.readInt s
      return (x, BS.drop 1 s')

import qualified Data.ByteString.Char8 as BS
import           Data.List             (unfoldr)

main = do
  [b,q,l,_m] <- getInts
  allow <-fmap ((not.) .flip elem) getInts
  let good num = abs num < l && allow num
      inf num | good num = "inf"
              | otherwise = "0"
      result | q == 0 && good 0 = "inf"
             | q == 0 && good b = "1"
             | q == 0           = "0"
             | q == 1  = inf b
             | q == -1 = inf b `max` inf (-b)
             | b == 0  = inf 0
             | otherwise = show $ length $ filter allow $ takeWhile ((< l).abs) $ iterate (* q) b
  putStrLn result
  where
  getInts = fmap (unfoldr readOne) BS.getLine where
    readOne s = do
      (x , s') <- BS.readInt s
      return (x, BS.drop 1 s')

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fromIntegral . fst. fromJust . C.readInt

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if b <= l then Just 1 else Just 0
             | not (0 `elem` as) = Nothing
run b 1 l as | b `elem` as || b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && b <= l       = Nothing
                | not ((-b) `elem` as) && (-b) <= l = Nothing
                | otherwise = Just 0
run b q l as | b < 0 || q < 0 = Nothing
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = until ((> l) . last) (\x -> x ++ [last x * q]) [b]

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fst. fromJust . C.readInteger

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if abs b <= l then Just 1 else Just 0
             | not (0 `elem` as) && b > l = Just 0
             | otherwise = Nothing
run b 1 l as | b `elem` as || abs b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && abs b <= l    = Nothing
                | not ((-b) `elem` as) && abs b <= l = Nothing
                | otherwise = Just 0
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = filter ((<= l) . abs) (until ((> l) . abs . last) (\x -> x ++ [last x * q]) [b])

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (take (fromIntegral m) . map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fromIntegral . fst. fromJust . C.readInt

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if abs b <= l then Just 1 else Just 0
             | not (0 `elem` as) = Nothing
run b 1 l as | b `elem` as || abs b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && abs b <= l       = Nothing
                | not ((-b) `elem` as) && abs b <= l = Nothing
                | otherwise = Just 0
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = until ((> l) . abs . last) (\x -> x ++ [last x * q]) [b]

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fromIntegral . fst. fromJust . C.readInt

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if abs b <= l then Just 1 else Just 0
             | not (0 `elem` as) = Nothing
run b 1 l as | b `elem` as || abs b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && abs b <= l       = Nothing
                | not ((-b) `elem` as) && abs b <= l = Nothing
                | otherwise = Just 0
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = filter ((<= l) . abs) (until ((> l) . abs . last) (\x -> x ++ [last x * q]) [b])

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fst. fromJust . C.readInteger

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if abs b <= l then Just 1 else Just 0
             | not (0 `elem` as) = Nothing
run b 1 l as | b `elem` as || abs b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && abs b <= l    = Nothing
                | not ((-b) `elem` as) && abs b <= l = Nothing
                | otherwise = Just 0
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = filter ((<= l) . abs) (until ((> l) . abs . last) (\x -> x ++ [last x * q]) [b])

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (take (fromIntegral m) . map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ find' b (sort xs))==0) then 0 else 2000000000
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ find' b (sort xs))==0) && ((fst $ find' (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = find' b xs'
                                                    (c2,_) = find' 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0
solve' (b:q:l:m:xs) = solve b q l 0 (sort xs)

solve b q l c xs = if (abs b>l) then c else let (cm,nl) = find' b xs
                                            in solve (b*q) q l (c+cm) nl

toString n = if(n==2000000000) then "inf" else show n

find' _ []  = (1,[])
find' e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else find' e xs

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ find' b (sort xs))==0) then 0 else 2000000000
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ find' b (sort xs))==0) && ((fst $ find' (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = find' b xs'
                                                    (c2,_) = find' 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0
solve' (b:q:l:m:xs) = solve b q l 0 xs

solve b q l c xs = if (abs b>l) then c else let (cm,nl) = find' b xs
                                            in solve (b*q) q l (c+cm) nl

toString n = if(n==2000000000) then "inf" else show n

find' _ []  = (1,[])
find' e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else find' e xs

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if (b>l) then 0 else if ((fst $ find' b xs)==0) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (b>l) then 0 else let (c1,_) = find' b xs
                                                (c2,_) = find' 0 xs
                                            in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0
solve' (b:q:l:m:xs) = solve b q l 0 xs

solve b q l c xs = if (abs b>l) then c else let (cm,nl) = find' b xs
                                            in solve (b*q) q l (c+cm) nl

toString n = if(n==2000000000) then "inf" else show n

find' _ []  = (1,[])
find' e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else find' e xs

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ findPos b (sort xs))==0) then 0 else 2000000000
solve' (0:_:l:m:xs) = if ((fst $ findPos 0 (sort xs))==0) then 0 else 2000000000
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ findPos b (sort xs))==0) && ((fst $ findPos (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = findPos b xs'
                                                    (c2,_) = findPos 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0

solve' (b:q:l:m:xs) = let (xsPos,xsNeg) = separe xs
                      in solve b q l 0 (sort xsPos) (reverse $ sort xsNeg)

solve b q l c xs xs' = if (b>l || (-1)*b>l) then c else if b>=0 then let (cm,nl) = findPos b xs
                                                             in solve (b*q) q l (c+cm) nl xs'
                                                else let (cm,nl) = findNeg b xs'
                                                     in solve (b*q) q l (c+cm) xs nl

toString n = if(n==2000000000) then "inf" else show n

separe = foldl (\(p,n) e-> if(e<0) then (p,e:n) else (e:p,n)) ([],[])

findPos _ []  = (1,[])
findPos e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else findPos e xs

findNeg _ []  = (1,[])
findNeg e (x:xs) = if (x==e) then (0,xs) else if(x<e) then (1,x:xs) else findNeg e xs

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ findPos b (sort xs))==0) then 0 else 2000000000
solve' (0:_:l:m:xs) = if ((fst $ findPos 0 (sort xs))==0) then 0 else 2000000000
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ findPos b (sort xs))==0) && ((fst $ findPos (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = findPos b xs'
                                                    (c2,_) = findPos 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0

solve' (b:q:l:m:xs) = let (xsPos,xsNeg) = separe xs
                      in solve b q l 0 (sort xsPos) (reverse $ sort xsNeg)

solve b q l c xs xs' = if (abs b>l) then c else if b>=0 then let (cm,nl) = findPos b xs
                                                             in solve (b*q) q l (c+cm) nl xs'
                                                else let (cm,nl) = findNeg b xs'
                                                     in solve (b*q) q l (c+cm) xs nl

toString n = if(n==2000000000) then "inf" else show n

separe = foldl (\(p,n) e-> if(e<0) then (p,e:n) else (e:p,n)) ([],[])

findPos _ []  = (1,[])
findPos e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else findPos e xs

findNeg _ []  = (1,[])
findNeg e (x:xs) = if (x==e) then (0,xs) else if(x<e) then (1,x:xs) else findNeg e xs

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if ((fst $ find' b xs)==0) then 0 else 2000000000
solve' (b:0:xs) = 2000000000
solve' (b:q:l:m:xs) = solve b q l 0 xs

solve b q l c xs = if (abs b>l) then c else let (cm,nl) = find' b xs
                                            in solve (b*q) q l (c+cm) nl

toString n = if(n==2000000000) then "inf" else show n

find' _ []  = (1,[])
find' e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else find' e xs

import Control.Applicative
import Data.List
import Data.Int

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int64] -> Int64
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ find' b (sort xs))==0) then 0 else (-1)
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ find' b (sort xs))==0) && ((fst $ find' (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = find' b xs'
                                                    (c2,_) = find' 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> (-1)
                                                (1,0) -> 1
                                                (0,0) -> 0
solve' (b:q:l:m:xs) = solve b q l 0 (sort xs)

solve b q l c xs = if (abs b>l) then c else let (cm,nl) = find' b xs
                                            in solve (b*q) q l (c+cm) nl

toString n = if(n==(-1)) then "inf" else show n

find' _ []  = (1,[])
find' e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else find' e xs


Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has $$$a$$$ sticks and $$$b$$$ diamonds?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$0 \le a, b \le 10^9$$$) — the number of sticks and the number of diamonds, respectively.

For each test case print one integer — the maximum number of emeralds Polycarp can earn.

NoteIn the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.In the second test case Polycarp does not have any diamonds, so he cannot craft anything.

import Control.Monad

main :: IO ()
main = do
  n <- readLn
  replicateM_ n routine

routine :: IO ()
routine = do
  [a,b] <- (map read.words) <$> getLine
  print $ solve a b

solve :: Int -> Int -> Int
solve a b = minimum [a,b,(a+b) `div` 3]

import Control.Monad
main :: IO ()
main = do
    n <- readLn
    replicateM_ n routine

routine :: IO ()
routine = do
    [a, b] <- (map read.words) <$> getLine
    putStrLn $ show (solve a b)

solve :: Int -> Int -> Int
solve a b = min (min a b) (div (a+b) 3)

module Main where

    solve :: IO ()
    solve = do
        s <- getLine
        let [a, b] = map (\x -> read x :: Integer) $ words s
        print $ minimum [a, b, (a + b) `div` 3]

    iter :: Integer -> IO ()
    iter 1 = solve
    iter n = do
        solve
        iter (n - 1)

    main :: IO ()
    main = do
        s <- getLine
        let q = read s :: Integer
        iter q

import Control.Monad

readInts :: IO [Int]
readInts = map read.words <$> getLine

main :: IO()
main = do
  [t] <- readInts
  replicateM_ t solve

solve :: IO()
solve = do
  [a,b] <- readInts
  print $ minimum [a,b, div (a+b) 3]

module Main where

    solve :: IO ()
    solve = do
        s <- getLine
        let [a, b] = map (\x -> read x :: Integer) $ words s
        print $ maximum [a, b, (a + b) `div` 3]

    iter :: Integer -> IO ()
    iter 1 = solve
    iter n = do
        solve
        iter (n - 1)

    main :: IO ()
    main = do
        s <- getLine
        let q = read s :: Integer
        iter q