Problem:


タイル (Tile)

問題

JOI 高校では， 1 &times; 1 の正方形のタイルを使って N &times; N の正方形の壁画を作り，文化祭で展示することになった．タイルの色は，赤，青，黄の 3 種類である．壁画のデザインは次の通りである．まず，最も外側の周に赤のタイルを貼り，その内側の周に青のタイルを貼る．さらにその内側の周に黄色のタイルを貼る．これを N &times; N の正方形が埋め尽くされるまで繰り返す．用いるタイルの色は，一番外側の周から順番に赤，青，黄，赤，青，黄，…である．

文化祭が近づいてきたある日，壁画のうち K 枚のタイルがはがれていることが判明した．そこで，新しいタイルを購入して，はがれた箇所に新しいタイルを貼ることにした．

入力として壁画の一辺の長さ N と，はがれたタイルの枚数 K， K 枚のはがれたタイルの位置が与えられたとき，はがれたタイルの色を求めるプログラムを作成せよ．

例えば，N = 11 の場合，11 &times; 11 の壁画のデザインは下図の通りである． 

また，N = 16 の場合，16 &times; 16 の壁画のデザインは下図の通りである． 

入力

入力は全部で 2+K 行からなる． 1 行目には，壁画の一辺の長さ N （1 ≤ N ≤ 1000000000 = 109）が， 2 行目には，はがれたタイルの枚数 K （1 ≤ K ≤ 1000）が書かれている． 2+i 行目（1 ≤ i ≤ K）には，2 つの整数 ai と bi （1 ≤ ai ≤ N, 1 ≤ bi ≤ N）が空白区切りで書かれており， i 枚目のはがれたタイルが，左から ai 列目，上から bi 行目のタイルであることを表す．

入力の 3 行目から 2+K 行目には同じタイルを表す行が重複して現れることはない．また，与えられる入力データ 40% では， N ≤ 1000 をみたしている． 

出力

出力は K 行からなる．各行は 1 つの整数からなり， i 行目（1 ≤ i ≤K）の整数は，i 枚目のはがれたタイルが赤のときは 1 を，青のときは 2 を，黄色のときは 3 を表す．

入出力例

入力例 1
  

11
4
5 2
9 7
4 4
3 9

出力例 1

2
3
1
3

入力例 2
  

16
7
3 7
5 2
11 6
15 2
9 7
8 12
15 16

出力例 2

3
2
3
2
1
2
1

問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
11
4
5 2
9 7
4 4
3 9


Sample output:
2
3
1
3

Submission status: Wrong Answer

Solution code:
import sys


def find_color(n, ai, bi):
    color = (3, 1, 2)
    if n % 2 != 0:
        if ai > int((n+1)/2):
            ai = int((n+1)/2) - (ai - int((n+1)/2))
        if bi > int((n+1)/2):
            bi = int((n+1)/2) - (bi - int((n+1)/2))
    else:
        if ai > int(n/2):
            ai = int(n/2) - (ai - int(n/2)) + 1
        if bi > int(n/2):
            bi = int(n/2) - (bi - int(n/2)) + 1

    print(ai, bi)

    if bi >= ai and bi <= n - ai:
        return color[ai % 3]
    else:
        return color[bi % 3]


n = int(sys.stdin.readline())
k = int(sys.stdin.readline())

for i in range(0, k):
    ai, bi = (int(x) for x in sys.stdin.readline().split())
    print(find_color(n, ai, bi))

Submission status: Wrong Answer

Solution code:
N=(input()+1)/2
for i in [1]*input():
	a,b=map(int,raw_input().split())
	print (max(a%N,b%N)-1)%3+1

Submission status: Accepted

Solution code:
d = int(input())
n = int(input())

for _ in range(n):
    a,b = map(int, input().split())
    if a > d/2:
        a = d-a+1
    if b > d/2:
        b = d-b+1
    c = min(a,b) % 3
    if c == 0:
        c = 3
    print(c)

Submission status: Accepted

Solution code:
N = int(input())
K = int(input())
tile = [tuple(map(int, input().split())) for i in range(K)] 
if N % 2 != 0:
    c = int(N / 2 + 0.5)
    if c % 3 == 0:
        red, blue, yellow = 2, 1, 0
    elif c % 3 == 2:
        yellow, red, blue = 2, 1, 0
    elif c % 1 == 1:
        blue, yellow, red = 2, 1, 0
    for i in range(K):
        x, y = tile[i]
        d = max(abs(x-c), abs(y-c))
        if d % 3 == red:
            print(1)
        elif d % 3 == blue:
            print(2)
        elif d % 3 == yellow:
            print(3)
else:
    c = N / 2
    if c % 3 == 0:
        red, blue, yellow = 2, 1, 0
    elif c % 3 == 2:
        yellow, red, blue = 2, 1, 0
    elif c % 1 == 0:
        blue, yellow, red = 2, 1, 0
    c += 0.5
    for i in range(K):
        x, y = tile[i]
        d = max(int(abs(x-c)-0.5), int(abs(y-c)-0.5))
        if d % 3 == red:
            print(1)
        elif d % 3 == blue:
            print(2)
        elif d % 3 == yellow:
            print(3)

Submission status: Accepted

Solution code:
n = input()
for i in range(input()):
    s = map(int, raw_input().split())
    x = min(s[0], s[1], n-s[0]+1, n-s[1]+1) 
    print (x-1)%3+1

Submission status: Accepted

Solution code:
n=int(input())
n+=1
for _ in range(int(input())):
    a,b=map(int,input().split())
    print((min(min(a,n-a),min(b,n-b))-1)%3+1)

Submission status: Accepted

Solution code:
import sys


def find_color(n, ai, bi):
    color = (3, 1, 2)
    if n % 2 != 0:
        if ai > int((n+1)/2):
            ai = int((n+1)/2) - (ai - int((n+1)/2))
        if bi > int((n+1)/2):
            bi = int((n+1)/2) - (bi - int((n+1)/2))
    else:
        if ai > int(n/2):
            ai = int(n/2) - (ai - int(n/2)) + 1
        if bi > int(n/2):
            bi = int(n/2) - (bi - int(n/2)) + 1

    if bi >= ai and bi <= n - ai:
        return color[ai % 3]
    else:
        return color[bi % 3]


n = int(sys.stdin.readline())
k = int(sys.stdin.readline())

for i in range(0, k):
    ai, bi = (int(x) for x in sys.stdin.readline().split())
    print(find_color(n, ai, bi))

Submission status: Accepted

Solution code:
n,k=[int(input())for i in range(2)]
tile=[[int(s)for s in input().split(" ")]for i in range(k)]
c=1+n
for t in tile:
    d=max((abs(2*t[0]-c),abs(2*t[1]-c)))
    if d%3==(c-2)%3:print('1')
    elif d%3==(c-4)%3:print('2')
    elif d%3==c%3:print('3')

Submission status: Accepted

Solution code:
N=int(input())
for _ in[0]*int(input()):
 a,b=map(int,input().split())
 print(min(a-1,N-a,b-1,N-b)%3+1)

Submission status: Accepted

Solution code:
import sys
N=int(input())
input()
for e in sys.stdin:
 a,b=map(int,e.split())
 print(min(a-1,N-a,b-1,N-b)%3+1)

Submission status: Accepted

Solution code:
n = int(input())
k = int(input())
for i in range(k):
  x, y = map(int,input().split())
  if n % 2:
    c = n // 2 + 1
    if x > c:
      x = c * 2 - x
    if y > c:
      y = c * 2 - y
  else:
    c = n // 2
    if x > c:
      x = c * 2 - x + 1
    if y > c:
      y = c * 2 - y + 1
  print((min(x, y) - 1) % 3 + 1)

Submission status: Accepted

Solution code:
# AOJ 0556: Tile
# Python3 2018.6.30 bal4u

N, K = int(input())-1, int(input())
for i in range(K):
	a, b = map(int, input().split())
	a, b = a-1, b-1
	if a > N>>1: a = N-a
	if b > N>>1: b = N-b
	if a > b: a = b
	print(a%3+1)

Submission status: Accepted

Solution code:
n = input()
k = input()
for i in range(k):
	x, y = map(int, raw_input().split())
	if x <= (n + 1) / 2:
		x -= 1
	else:
		x = n - x
	if y <= (n + 1) / 2:
		y -= 1
	else:
		y = n - y
	if x <= y < n - x:
		print 1 + x % 3
	else:
		print 1 + y % 3

Submission status: Accepted

Solution code:

s = input()
n = input()
for i in range(n):
  x, y = map(int, raw_input().split(" "));
  x -= 1;
  y -= 1;
  p = min(x, y, s - x - 1, s - y - 1)
  print p % 3 + 1

Submission status: Accepted

Solution code:
N=input()
for i in range(input()):
    a,b=map(int,raw_input().split())
    print min(a-1,N-a,b-1,N-b)%3+1

Submission status: Accepted

Solution code:
n = int(raw_input())
for i in range(int(raw_input())):
    x,y = map(int, raw_input().split())
    w = min(x, n - x + 1)
    h = min(y, n - y + 1)
    if h > w:
        print (w - 1) % 3 + 1
    else:
        print (h - 1) % 3 + 1

Submission status: Accepted

Solution code:
n = input(); k = input();
p = (n+1)/2 if n%2==1 else n/2
for i in range(k):
    a,b = map(int, raw_input().split())
    if p<a: a = n+1-a
    if p<b: b = n+1-b
    print (min(a,b)-1)%3+1

Submission status: Accepted

Solution code:
n = int(raw_input())
for i in range(int(raw_input())):
    a, b = [int(i) for i in raw_input().split(" ")]
    min_a = min(a, n+1-a)
    min_b = min(b, n+1-b)
    print (min(min_a, min_b)-1)%3+1

Submission status: Accepted

Solution code:
N=input()
for i in [1]*input():
    a,b=map(int,raw_input().split())
    print min(a-1,N-a,b-1,N-b)%3+1

Submission status: Accepted

Solution code:
n = int(input())
m = int(input())
for _ in range(m):
	a, b = map(int, input().split())
	c = (n+1)/2
	tmp = c-max(abs(c-a), abs(c-b))
	if tmp%3==0:
		r = 3
	elif tmp%3==1:
		r = 1
	else:
		r = 2
	print(r)

Submission status: Accepted

Solution code:
n = int(input())
k = int(input())
for i in range(k):
    a, b = map(int, input().split())
    if min(a, b, n-a+1, n-b+1)%3 == 0:
        print(3)
    elif min(a, b, n-a+1, n-b+1)%3 == 1:
        print(1)
    else:
        print(2)

Submission status: Accepted

Solution code:
n=int(input())
k=int(input())
ab=[]
for _ in range(k):
    ab1=list(map(int,input().split()))
    ab.append([ab1[1]-1,ab1[0]-1])
c=[]
if n%2==0:
    nn=n//2
    for i in range(k):
        y=ab[i][0]
        x=ab[i][1]
        if ab[i][0]>=nn:
            y=n-ab[i][0]-1
            if ab[i][1]>=nn:
                x=n-ab[i][1]-1
        else:
            if ab[i][1]>=nn:
                x=n-ab[i][1]-1
        c.append((min(x,y)+1)%3)
else:
    nn=(n+1)//2
    for i in range(k):
        y=ab[i][0]
        x=ab[i][1]
        if ab[i][0]>=nn:
            y=n-ab[i][0]-1
            if ab[i][1]>=nn:
                x=n-ab[i][1]-1
        else:
            if ab[i][1]>=nn:
                x=n-ab[i][1]-1
        c.append((min(x,y)+1)%3)
for i in c:
    if i!=0:
        print(i)
    else:
        print(3)

Submission status: Accepted

Solution code:
N, K = int(input()), int(input())
p = N // 2 if N & 1 == 0 else N // 2 + 1
for i in range(K):
    a, b = map(int, input().split())
    x = N + 1 - a if a > p else a
    y = N + 1 - b if b > p else b
    mod = x % 3 if x <= y else y % 3
    print((mod + 2) % 3 + 1)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


# globals
inf = float('inf')
N = 0
K = 0
A, B = [], []
C, D = [], []

def set_inputs():
    global N, K, A, B, C, D
    N = get_int()
    K = get_int()
    A, B = get_data(K, [int, int])
    C = [min(a, N - a + 1) for a in A]
    D = [min(b, N - b + 1) for b in B]
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    # ----------MAIN----------
    for c, d in zip(C, D):
        ans = min(c, d) % 3
        if ans == 0:
            ans = 3
        print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def getN():
    return int(input())

def getlist():
    return list(map(int, input().split()))

def solve(n, pos):
    x,y = pos
    if x > n // 2:
        x = n - x + 1
    if y > n // 2:
        y = n - y + 1

    if x == y:
        color = ( (x-1) % 3 ) + 1
    else:
        definitive = min(x,y)
        color = ((definitive - 1) % 3) + 1
    return color

n = getN()
k = getN()

for i in range(k):
    pos = getlist()
    print(solve(n, pos))

Submission status: Accepted

Solution code:
n=int(input())
k=int(input())
for i in range(k):
    a,b=map(int,input().split())
    half=(n+1)//2
    if a>half:a=n+1-a
    if b>half:b=n+1-b
    print((min(a,b)-1)%3+1)

Submission status: Runtime Error

Solution code:
N = int(raw_input())
k = int(raw_input())

while k:
	k -= 1
	x, y = map(int, raw_input().split())
	
	two = 0
	
	while 1:
		a_y = two + 1
		a_x = two + 1
		f = 0
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_y += 1
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_x += 1
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_y -= 1
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_x -= 1
	
		if f == 1:
			print two % 3 + 1
			break
		
		two += 1

Submission status: Runtime Error

Solution code:
N = int(raw_input())
k = int(raw_input())
lis = []

while k:
	k -= 1
	x, y = map(int, raw_input().split())
	
	two = 0
	
	while 1:
		a_y = two + 1
		a_x = two + 1
		f = 0
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_y += 1
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_x += 1
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_y -= 1
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_x -= 1
	
		if f == 1:
			lis.append(two % 3 + 1)
			break
		
		two += 1

for i in lis:
	print i

Submission status: Runtime Error

Solution code:
N = int(raw_input())
k = int(raw_input())
lis = []

while k:
	k -= 1
	x, y = map(int, raw_input().split())
	
	two = 0
	f = 0
	
	while f != 1:
		a_y = two + 1
		a_x = two + 1
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_y += 1
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_x += 1
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_y -= 1
	
		for i in range(N - two * 2 - 1):
			if x == a_x and y == a_y:
				f = 1
				break
			a_x -= 1
			
		if N % 2 == 1 and two >= N / 2 + 1:
			break
		
		two += 1
		
	lis.append((two - 1) % 3 + 1)

for i in lis:
	print i

Submission status: Runtime Error

Solution code:
s = input()
n = input()
for i in range(n):
  x, y = input()-1, input()-1
  p = min(x, y, s - x - 1, s - y - 1)
  print p % 3 + 1

Submission status: Runtime Error

Solution code:
N=input()
L=[[0]*N for i in range(N)]
R,B,Y=1,2,3
C=R
for y in range(N/2+1):
    for x in range(y,N-y):
        L[y][x]=C
        L[N-y-1][x]=C
    C=C%3+1
C=R
for x in range(N/2+1):
    for y in range(x,N-x):
        L[y][x]=C
        L[y][N-x-1]=C
    C=C%3+1

for i in range(input()):
    a,b=map(int,raw_input().split())
    print L[b-1][a-1]