-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path106.hs
More file actions
77 lines (62 loc) · 2.12 KB
/
Copy path106.hs
File metadata and controls
77 lines (62 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE BangPatterns #-}
import Control.Applicative
import Control.Monad
import Data.Function
import qualified Data.ByteString.Char8 as B
import Data.Array
import Data.Bits
-- s 0 0 = 1
-- s n 0 | n > 0 = 0
-- s 0 m | m > 0 = 0
-- s n m | n > 0
-- && m > 0 = m * s (n-1) m + s(n-1) (m-1)
-- specification n m = s n m `mod` 2
-- m * s (n-1) m + s(n-1) (m-1) ; m is odd
-- m * (m * s (n-2) m + s(n-2) (m-1)) + ((m-1) * s (n-2) (m-1) + s(n-2) (m-2))
-- m * (m * s (n-2) m + s(n-2) (m-1)) + s(n-2) (m-2)
-- s (n-2) m + s(n-2)(m-1) + s(n-2) (m-2)
-- s (n-3) m + s(n-3)(m-1) + s(n-3)(m-1) + s(n-3)(m-2) + s(n-3)(m-2) + s(n-3)(m-3)
-- s (n-3) m + + s(n-3)(m-3)
-- op x y = if x == y then 0 else 1
-- arr = listArray ((0,0), (max',max')) values
-- where
-- max' = 500
-- values = do
-- n <- [0..max']
-- m <- [0..max']
-- if n == m
-- then return 1
-- else if m>n || m==0 || n==0 then return 0
-- else if even m
-- then return $ arr ! (n-1,m-1)
-- else return $ (arr ! ((n-1), m)) + (arr ! (n-1,m-1))
-- -- -- return $ (arr ! ((n-1), m)) `op` (arr ! (n-1,m-1))
-- memoizing_sb =
-- sb n m = s' n m `mod` 2
-- where s' 0 0 = 1
-- s' n 0 | n > 0 = 0
-- s' 0 m | m > 0 = 0
-- s' n m
-- | m == n = 1
-- | n > 0
-- && m > 0 =
-- if even m then s' (n-1) (m-1)
-- else s'(n-1) m + s'(n-1) (m-1)
-- tests = do
-- x <- [1..15]
-- y <- [1..x]
-- return (x,y)
sb :: Int -> Int -> Int
sb !n !m = if (n-m) .&. ((m-1) `div` 2) == 0 then 1 else 0
readInts :: B.ByteString -> Maybe (Int, Int)
readInts x = do
(n,rest) <- B.readInt x
(m,_) <- B.readInt $ B.tail rest
return (n,m)
getInts = maybe (0,0) id . readInts
main = do
l:ls <- B.lines <$> B.getContents
mapM_ (print . solve' . getInts) $ take (read . B.unpack $ l) ls
return ()
solve' = uncurry sb