Problem:


Score : 800 points

Problem Statement

Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor.

Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them.

In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume).

He may perform the following operation arbitrarily many times:

Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point.

Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square.

Find the minimum scatteredness after he performs arbitrarily many operations.

Constraints

2 \leq N \leq 10^5

1 \leq D \leq 1000

0 \leq x_i, y_i \leq 10^9

Given coordinates are pairwise distinct

All numbers given in input are integers

Partial Scores

500 points will be awarded for passing the test set satisfying 1 \leq D \leq 30.

Input

Input is given from Standard Input in the following format:

N D
x_1 y_1
:
x_N y_N

Output

Print the answer.

Sample Input 1

3 1
0 0
1 0
2 0

Sample Output 1

1

Sample Input 2

19 2
1 3
2 3
0 1
1 1
2 1
3 1
4 4
5 4
6 4
7 4
8 4
8 3
8 2
8 1
8 0
7 0
6 0
5 0
4 0

Sample Output 2

4

Sample Input 3

8 3
0 0
0 3
3 0
3 3
2 2
2 5
5 2
5 5

Sample Output 3

4



Sample input:
3 1
0 0
1 0
2 0


Sample output:
1

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,D=map(int,input().split())
l=[[0]*D for i in range(D)]
for i in range(N):
    x,y=map(lambda x: int(x)%D,input().split())
    l[x][y]+=1
M=0
for i in l:
    M=max(M,max(i))
for i in range(1,318):
    if i**2>=M:
        m=i
        break
s0=[]
s1=[]
for i in range(D):
    for j in range(D):
        if l[i][j]<=(m-1)**2:
            continue
        if l[i][j]<=(m-1)*m:
            s0.append((i,j))
        else:
            s1.append((i,j))
a=D
for i in s0:
    c=0
    for j in s0:
        t=min((j[0]-i[0])%D,(j[1]-i[1])%D)
        if c<t:
            c=t
    for j in s1:
        t=max((j[0]-i[0])%D,(j[1]-i[1])%D)
        if c<t:
            c=t
    if a>c:
        a=c
for i in s1:
    c=0
    for j in s0:
        t=min((j[0]-i[0])%D,(j[1]-i[1])%D)
        if c<t:
            c=t
    for j in s1:
        t=max((j[0]-i[0])%D,(j[1]-i[1])%D)
        if c<t:
            c=t
    if a>c:
        a=c
print(D*(m-1)+a)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,D=map(int,input().split())
l=[[0]*D for i in range(D)]
for i in range(N):
    x,y=map(lambda x: int(x)%D,input().split())
    l[x][y]+=1
M=0
for i in l:
    M=max(M,max(i))
for i in range(1,318):
    if i**2>=M:
        m=i
        break
s0=[]
s1=[]
for i in range(D):
    for j in range(D):
        if l[i][j]<=(m-1)**2:
            continue
        if l[i][j]<=(m-1)*m:
            s0.append((i,j))
        else:
            s1.append((i,j))
a=m
for i in s0:
    c=0
    for j in s0:
        t=min((i[0]-j[0])%D,(i[1]-j[1])%D)
        if c<t:
            c=t
    for j in s1:
        t=max((i[0]-j[0])%D,(i[1]-j[1])%D)
        if c<t:
            c=t
    if a>c:
        a=c
for i in s1:
    c=0
    for j in s0:
        t=min((i[0]-j[0])%D,(i[1]-j[1])%D)
        if c<t:
            c=t
    for j in s1:
        t=max((i[0]-j[0])%D,(i[1]-j[1])%D)
        if c<t:
            c=t
    if a>c:
        a=c
print(D*(m-1)+a)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,d = LI()
    p = LIR(n)
    for i in range(n):
        x,y = p[i][0],p[i][1]
        x %= d
        y %= d
        p[i][0],p[i][1] = x,y
    g = defaultdict(lambda : 0)
    for x,y in p:
        g[(x,y)] += 1
    p = [i**2 for i in range(1,10**3)]
    m = 0
    bx,by = 0,0
    f = defaultdict(lambda : 0)
    for i,j in g.items():
        k = bisect.bisect_left(p,j)
        f[i] = k
        if m < k:
            m = k
            bx,by = i
    a = [[0,d*m] for i in range(4)]
    for i,j in g.items():
        na = [[-float("inf"),float("inf")] for i in range(4)]
        for l,r in a:
            if f[i] < m or (bx,by) == i:
                na = [[a[i][j] for j in range(2)] for i in range(4)]
                continue
            x,y = i
            x -= bx
            x %= d
            y -= by
            y %= d
            nl = x-d
            nr = x+d
            nb = y-d
            nu = y+d
            res = max(1,j-p[f[i]-1])
            if res <= f[i]:
                if l <= nl or nr <= r or l <= nb or nu <= r:
                    na = [[a[i][j] for j in range(2)] for i in range(4)]
                    continue
                na[0][0] = max(na[0][0],nl)
                na[1][0] = max(na[1][0],nb)
                na[2][1] = min(na[2][1],nr)
                na[3][1] = min(na[3][1],nu)
            else:
                if (l <= nl or nr <= r) and (l <= nb or nu <= r):
                    na = [[a[i][j] for j in range(2)] for i in range(4)]
                    continue
                dl = l-nl
                db = l-nb
                dr = nr-r
                du = nu-r
                if max(dl,db) < du:
                    na[0][0] = max(na[0][0],max(nl,nb))
                    na[0][1] = a[0][1]
                else:
                    na[0][0] = max(na[0][0],nl)
                    na[0][1] = min(na[0][1],nu)
                if max(dl,db) < dr:
                    na[1][0] = max(na[1][0],max(nl,nb))
                    na[1][1] = a[1][1]
                else:
                    na[1][0] = max(na[1][0],nb)
                    na[1][1] = min(na[1][1],nr)
                if max(dr,du) < db:
                    na[2][0] = a[0][0]
                    na[2][1] = min(na[2][1],min(nr,nu))
                else:
                    na[2][0] = max(na[2][0],nb)
                    na[2][1] = min(na[2][1],nr)
                if max(dr,du) < dl:
                    na[3][0] = a[3][0]
                    na[3][1] = min(na[3][1],min(nr,nu))
                else:
                    na[3][0] = max(na[3][0],nl)
                    na[3][1] = min(na[3][1],nu)
        a = [[na[i][j] for j in range(2)] for i in range(4)]
    ans = [a[i][1]-a[i][0] for i in range(4)]
    print(min(ans))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def S_():
    return input()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()
import fractions
#gcd=fractions.gcd
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a
class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo
show_flg=False
show_flg=True

ans=0

n,d=LI()
p=[]
mp=[[0]*d for i in range(d)]
for i in range(n):
    x,y=LI()
    p.append((x,y))
    mp[x%d][y%d]+=1

sq=[[int(mp[i][j]**0.5) for i in range(d)] for j in range(d)]

ans=inf
for i in range(d):
    for j in range(d):
        l=sq[i][j]
        tmp=-1
        for i2 in range(d):
            for j2 in range(d):
                if (i,j)==(i2,j2):continue
                if sq[i2][j2]==l:
                    tmp=max(tmp,max(abs(i-i2),abs(j-j2))+l)
                elif sq[i2][j2]>l:
                    tmp=l
                else:
                    tmp=max(tmp,l)
        ans=min(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

import numpy as np

N,D = map(int,readline().split())
XY = np.array(list(map(int,read().split())))

A = np.zeros((D,D),np.int64)
X = XY[::2]; Y = XY[1::2]
np.add.at(A,(X%D,Y%D),1)

D = 4
A = np.array([[1,0,0,1],[1,0,1,0],[0,0,0,0],[1,0,1,1]])

maxA = A.max()
n = int((maxA-1)**.5)

# 2周分用意する
AA = np.tile(A,(2,2))

gr1 = (AA > n*n).cumsum(axis=0).cumsum(axis=1)
gr2 = (AA > n*(n+1)).cumsum(axis=0).cumsum(axis=1)

def test(x):
    gr1_rd = gr1[D:D+D,D:D+D] + gr1[x:x+D,x:x+D] - gr1[x:x+D,D:D+D] - gr1[D:D+D,x:x+D]
    condition_rd = (gr1_rd == 0)
    gr2_ru = gr2[x:x+D,D:D+D] + gr2[0:D,x:x+D] - gr2[x:x+D,x:x+D] - gr2[0:D,D:D+D]
    gr2_ld = gr2[D:D+D,x:x+D] + gr2[x:x+D,0:D] - gr2[x:x+D,x:x+D] - gr2[D:D+D,0:D]
    condition_ru = (gr2_ru == 0)
    condition_ld = (gr2_ld == 0)
    return (condition_rd&condition_ru&condition_ld).any()

left = 0 # 足りてない
right = D # 足りてる
while left+1 < right:
    mid = (left+right)//2
    if test(mid):
        right = mid
    else:
        left = mid

answer = n*D + right - 1
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

import numpy as np

N,D = map(int,readline().split())
XY = np.array(list(map(int,read().split())))

A = np.zeros((D,D),np.int64)
X = XY[::2]; Y = XY[1::2]
np.add.at(A,(X%D,Y%D),1)

maxA = A.max()
n = int((maxA-1)**.5)

# 2周分用意する
AA = np.tile(A,(2,2))

gr1 = (AA > n*n).cumsum(axis=0).cumsum(axis=1)
gr2 = (AA > n*(n+1)).cumsum(axis=0).cumsum(axis=1)

def test(x):
    gr1_rd = gr1[D:D+D,D:D+D] + gr1[x:x+D,x:x+D] - gr1[x:x+D,D:D+D] - gr1[D:D+D,x:x+D]
    condition_rd = (gr1_rd == 0)
    gr2_ru = gr2[x:x+D,D:D+D] + gr2[0:D,x:x+D] - gr2[x:x+D,x:x+D] - gr2[0:D,D:D+D]
    gr2_ld = gr2[D:D+D,x:x+D] + gr2[x:x+D,0:D] - gr2[x:x+D,x:x+D] - gr2[D:D+D,0:D]
    condition_ru = (gr2_ru == 0)
    condition_ld = (gr2_ru == 0)
    return (condition_rd&condition_ru&condition_ld).any()

left = 0 # 足りてない
right = D # 足りてる
while left+1 < right:
    mid = (left+right)//2
    if test(mid):
        right = mid
    else:
        left = mid

answer = n*D + right - 1
print(answer)

Submission status: Wrong Answer

Solution code:
N,D=map(int,input().split())
x=[[] for i in range(N)]
for i in range(N):
    a,b=map(int,input().split())
    x[i].append(a)
    x[i].append(b)
k=0
l=0
for i in range(N):
    for j in range(2):
        x[i][j]=x[i][j]%D
        if x[i][j]>D-x[i][j]:
            x[i][j]=D-x[i][j]
        if k<x[i][j] and j==0:
                k=x[i][j]
        if l<x[i][j] and j==1:
                l=x[i][j]
if k<l:
    k=l
while (D+k+1)*(D+k+1)<N:
        k=k+1
print(D+k)

Submission status: Wrong Answer

Solution code:
N,D=map(int,input().split())
x=[[] for i in range(N)]
for i in range(N):
    a,b=map(int,input().split())
    x[i].append(a)
    x[i].append(b)
k=0
l=0
for i in range(N):
    for j in range(2):
        x[i][j]=x[i][j]%D
        if x[i][j]>D-x[i][j]:
            x[i][j]=D-x[i][j]
        if k<x[i][j] and j==0:
                k=x[i][j]
        if l<x[i][j] and j==1:
                l=x[i][j]
if k<l:
    k=l
while (k+1)*(k+1)<N:
        k=k+1
print(k)

Submission status: Wrong Answer

Solution code:
n,d = map(int,input().split())
num = [[0]*d for i in range(d)]
a = 0
for i in range(n):
  x,y = map(int,input().split())
  x %=d
  y%=d
  num[x][y] += 1
  a = max(a,num[x][y])
x=1
while x*x<a:
  x += 1
r = (x-1)*d
a = x-1
dai = d-1
syo = 0
anum = [[[0]*d for i in range(d)]for i in range(2)]
rui = [[[0]*(2*d+1) for i in range(2*d+1)]for i in range(2)]
for i in range(d):
  for j in range(d):
    z = num[i][j]
    if z> a*(a+1):
      anum[0][i][j]=1
      anum[1][i][j]=1
    elif z > a*a:
      anum[1][i][j]=1
for x in range(2):
  for i in range(1,2*d+1):
    for j in range(1,2*d+1):
      rui[x][i][j]+=rui[x][i-1][j]+rui[x][i][j-1]-rui[x][i-1][j-1]+anum[x][(i-1)%d][(j-1)%d]
def cheak(kon):
  for i in range(1,d+1):
    for j in range(1,d+1):
      if rui[0][i+d-1][j+d-1] - rui[0][i+kon+1 - 1][j+d-1] - rui[0][i+d-1][j+kon+1 - 1] + rui[0][i+kon+1 - 1][j+kon+1 - 1]:
        continue
      if rui[1][i+d-1][j+kon] - rui[1][i+kon+1 - 1][j+kon] - rui[1][i+d-1][j - 1] + rui[1][i+kon+1 - 1][j - 1]:
        continue
      if rui[1][i+kon][j+d-1] - rui[1][i - 1][j+d-1] - rui[1][i+kon][j+kon+1 - 1] + rui[1][i - 1][j+kon+1 - 1]:
        continue
      return 1
  return 0 
kon = (dai+syo)//2
while True:
  if dai-syo <= 1:
    if cheak(syo) == 1:
       dai = syo
    break
  kon = (dai+syo)//2
  c = cheak(kon)
  if c == 1:
    dai = kon
  else:
    syo = kon
print(dai+r)

Submission status: Wrong Answer

Solution code:
n,d = map(int,input().split())
num = [[0]*d for i in range(d)]
a = 0
for i in range(n):
  x,y = map(int,input().split())
  x %=d
  y%=d
  num[x][y] += 1
  a = max(a,num[x][y])
x=1
while x*x<a:
  x += 1
r = (x-1)*d
a = x-1
dai = d-1
syo = 0
anum = [[[0]*d for i in range(d)]for i in range(2)]
rui = [[[0]*d for i in range(d)]for i in range(2)]
for i in range(d):
  for j in range(d):
    if num[i][j]<= a*a:
      continue
    elif num[i][j]<= a*(a+1):
      anum[1][i][j]=1
    else:
      anum[0][i][j]=1
for x in range(2):
  for i in range(d):
    for j in range(d):
      if i:
        rui[x][i][j]+=rui[x][i-1][j]
      if j:
        rui[x][i][j]+=rui[x][i][j-1]
      if i and j:
        rui[x][i][j]-=rui[x][i-1][j-1]
        rui[x][i][j]+=anum[x][(i-1)%d][(j-1)%d]
def cheak(kon):
  for i in range(d):
    for j in range(d):
      if (rui[0][d-1][d-1]) - abs(rui[0][(i+kon)%d][(j+kon)%d]- rui[0][i][j]):
        continue
      if rui[1][(i+d-1)%d][(j+d-1)%d] + rui[1][(i+kon+1)%d][(j+kon+1)%d] - rui[1][(i+d-1)%d][(j+kon+1)%d] - rui[1][(i+kon+1)%d][(j+d-1)%d]:
        continue
      return 1
  return 0 
kon = (dai+syo)//2
while True:
  if dai-syo <= 1:
    if cheak(syo) == 1:
       dai = syo
    break
  kon = (dai+syo)//2
  c = cheak(kon)
  if c == 1:
    dai = kon
  else:
    syo = kon
print(dai+r)

Submission status: Wrong Answer

Solution code:
n,d = map(int,input().split())
num = [[0]*d for i in range(d)]
a = 0
for i in range(n):
  x,y = map(int,input().split())
  x %=d
  y%=d
  num[x][y] += 1
  a = max(a,num[x][y])
x=1
while x*x<a:
  x += 1
r = (x-1)*d
a = x-1
dai = d-1
syo = 0
anum = [[[0]*d for i in range(d)]for i in range(2)]
rui = [[[0]*d for i in range(d)]for i in range(2)]
for i in range(d):
  for j in range(d):
    if num[i][j]<= a*a:
      continue
    elif num[i][j]<= a*(a+1):
      anum[1][i][j]=1
      anum[0][i][j]=1
    else:
      anum[0][i][j]=1
for x in range(2):
  for i in range(d):
    for j in range(d):
      if i:
        rui[x][i][j]+=rui[x][i-1][j]
      if j:
        rui[x][i][j]+=rui[x][i][j-1]
      if i and j:
        rui[x][i][j]-=rui[x][i-1][j-1]
        rui[x][i][j]+=anum[x][(i-1)%d][(j-1)%d]
def cheak(kon):
  for i in range(d):
    for j in range(d):
      if rui[0][(i+d-1)%d][(j+d-1)%d] - rui[0][(i+kon+1)%d][(j+kon+1)%d]:
        return 0
      if rui[1][(i+d-1)%d][(j+d-1)%d] + rui[1][(i+kon+1)%d][(j+kon+1)%d] - rui[1][(i+d-1)%d][(j+kon+1)%d] - rui[1][(i+kon+1)%d][(j+d-1)%d]:
        return 0
  return 1
kon = (dai+syo)//2
while True:
  if dai-syo <= 1:
    if cheak(syo) == 1:
       dai = syo
    break
  kon = (dai+syo)//2
  c = cheak(kon)
  if c == 1:
    dai = kon
  else:
    syo = kon
print(dai+r)

Submission status: Wrong Answer

Solution code:
n,d = map(int,input().split())
num = [[0]*d for i in range(d)]
a = 0
for i in range(n):
  x,y = map(int,input().split())
  x %=d
  y%=d
  num[x][y] += 1
  a = max(a,num[x][y])
x=1
while x*x<a:
  x += 1
r = (x-1)*d
a = x-1
dai = d-1
syo = 0
anum = [[[0]*d for i in range(d)]for i in range(2)]
rui = [[[0]*d for i in range(d)]for i in range(2)]
for i in range(d):
  for j in range(d):
    if num[i][j]<= a*a:
      continue
    elif num[i][j]<= a*(a+1):
      anum[1][i][j]=1
    else:
      anum[0][i][j]=1
for x in range(2):
  for i in range(d):
    for j in range(d):
      if i:
        rui[x][i][j]+=rui[x][i-1][j]
      if j:
        rui[x][i][j]+=rui[x][i][j-1]
      if i and j:
        rui[x][i][j]-=rui[x][i-1][j-1]
        rui[x][i][j]+=anum[x][(i-1)%d][(j-1)%d]
def cheak(kon):
  for i in range(d):
    for j in range(d):
      if rui[0][(i+d-1)%d][(j+d-1)%d] - rui[0][(i+kon+1)%d][(j+kon+1)%d]:
        return 0
      if rui[1][(i+d-1)%d][(j+d-1)%d] + rui[1][(i+kon+1)%d][(j+kon+1)%d] - rui[1][(i+d-1)%d][(j+kon+1)%d] - rui[1][(i+kon+1)%d][(j+d-1)%d]:
        return 0
  return 1
kon = (dai+syo)//2
while True:
  if dai-syo <= 1:
    if cheak(syo) == 1:
       dai = syo
    break
  kon = (dai+syo)//2
  c = cheak(kon)
  if c == 1:
    dai = kon
  else:
    syo = kon
print(dai+r)

Submission status: Wrong Answer

Solution code:
n,d = map(int,input().split())
num = [[0]*d for i in range(d)]
a = 0
for i in range(n):
  x,y = map(int,input().split())
  x %=d
  y%=d
  num[x][y] += 1
  a = max(a,num[x][y])
x=1
while x*x<a:
  x += 1
r = (x-1)*d
a = x-1
dai = d-1
syo = 0
def cheak(x):
  for i in range(d):
    for j in range(d):
      flag = 1
      for k in range(d):
        i2 = (i+k)%d
        for l in range(d):
          j2 = (j+l)%d
          if i2<= kon and j2<= kon:
            if num[i2][j2] > (a+1)**2:
              flag = 0
          elif kon < i2 and kon < j2:
            if num[i2][j2] > a**2:
              flag = 0
          else:
            if num[i2][j2] > a*(a+1):
              flag = 0            
      if flag:
        return 1
  return 0
kon = (dai+syo)//2
while True:
  if dai-syo <= 1:
    if cheak(syo) == 1:
       dai = syo
    break
  kon = (dai+syo)//2
  c = cheak(kon+r)
  if c == 1:
    dai = kon
  else:
    syo = kon
print(dai+r)

Submission status: Wrong Answer

Solution code:
n,d = map(int,input().split())
num = [[0]*d for i in range(d)]
a = 0
for i in range(n):
  x,y = map(int,input().split())
  x %=d
  y%=d
  num[x][y] += 1
  a = max(a,num[x][y])
x=1
while x*x<a:
  x += 1
r = (x-1)*d
a = x-1
dai = d-1
syo = 0
def cheak(x):
  for i in range(d):
    for j in range(d):
      flag = 1
      for k in range(d):
        i2 = (i+k)%d
        for l in range(d):
          j2 = (j+l)%d
          if i2<= kon and j2<= kon:
            if num[i2][j2] > (a+1)**2:
              flag = 0
          elif kon < i2 and kon < j2:
            if num[i2][j2] > a**2:
              flag = 0
          else:
            if num[i2][j2] > a*(a+1):
              flag = 0            
      if flag:
        return 1
  return 0
kon = (dai+syo)//2
while True:
  if dai-syo <= 1:
    if cheak(dai) != 1:
          dai = syo
    break
  kon = (dai+syo)//2
  c = cheak(kon+r)
  if c == 1:
    dai = kon
  else:
    syo = kon
print(dai+r)

Submission status: Wrong Answer

Solution code:
n,d = map(int,input().split())
num = [[0]*d for i in range(d)]
a = 0
for i in range(n):
  x,y = map(int,input().split())
  x %=d
  y%=d
  num[x][y] += 1
  a = max(a,num[x][y])
x=1
while x*x<a:
  x += 1
a = (x-1)*d
dai = d-1
syo = 0
def cheak(x):
  for i in range(d):
    for j in range(d):
      flag = 1
      for k in range(d):
        i2 = (i+k)%d
        for l in range(d):
          j2 = (j+l)%d
          if i2<= kon and j2<= kon:
            if num[i2][j2] > (x+1)**2:
              flag = 0
          elif kon < i2 and kon < j2:
            if num[i2][j2] > x**2:
              flag = 0
          else:
            if num[i2][j2] > x*(x+1):
              flag = 0            
      if flag:
        return 1
  return 0
kon = (dai+syo)//2
while True:
  if dai-syo <= 1:
    if cheak(dai) != 1:
          dai = syo
    break
  kon = (dai+syo)//2
  c = cheak(kon+a)
  if c == 1:
    dai = kon
  else:
    syo = kon
print(dai+a)

Submission status: Wrong Answer

Solution code:
from math import ceil
N,D = map(int,input().split())
r = [[0]*D for i in range(D)]
for i in range(N):
    x,y = map(int,input().split())
    r[x%D][y%D] += 1

M = 0
for i in range(D):
    for j in range(D):
        M = max(M,r[i][j])

a = ceil(M**0.5-1)

l = 0
b = D-1
mid = (l+b)//2

def possible(b):
    cum = [[0]*(D+1) for i in range(D+1)]
    for i in range(D):
        for j in range(D):
            if a**2+a<r[i][j]:
                if i<=b:
                    cum[b+1-i][0] += 1
                    cum[D-i][0] -= 1
                else:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[D+b+1-i][0] += 1
                if j<=b:
                    cum[0][b+1-j] += 1
                    cum[0][D-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[0][D-j] -= 1
                    cum[0][D+b+1-j]
            elif a**2<r[i][j]<=a**2+a:
                if i<=b and j<=b:
                    cum[b+1-i][b+1-j] += 1
                    cum[D-i][b+1-j] -= 1
                    cum[b+1-i][D-j] -= 1
                    cum[D-i][D-j] += 1
                elif b+1<=i and j<=b:
                    cum[0][b+1-j] += 1
                    cum[D-i][b+1-j] -= 1
                    cum[0][D-j] -= 1
                    cum[D-i][D-j] += 1
                    cum[D+b+1-i][b+1-j] += 1
                    cum[D+b+1-i][D-j] -= 1
                elif i<=b and b+1<=j:
                    cum[b+1-i][0] += 1
                    cum[b+1-i][D-j] -= 1
                    cum[D-i][0] -= 1
                    cum[D-i][D-j] += 1
                    cum[b+1-i][D+b+1-j] += 1
                    cum[D-i][D+b+1-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[0][D-j] -= 1
                    cum[D-i][D-j] += 1
                    cum[0][D+b+1-j] += 1
                    cum[D-i][D+b+1-j] -= 1
                    cum[D+b+1-i][0] += 1
                    cum[D+b+1-i][D-j] -= 1
                    cum[D+b+1-i][D+b+1-j] += 1
    for i in range(1,D):
        for j in range(D):
            cum[i][j] += cum[i-1][j]
    for i in range(D):
        for j in range(1,D):
            cum[i][j] += cum[i][j-1]
    for i in range(D):
        for j in range(D):
            if cum[i][j] == 0:
                return True
    return False

for i in range(10):
    if possible(mid):
        b = mid
    else:
        l = mid
    mid = (l+b)//2

print(a*D+b)

Submission status: Wrong Answer

Solution code:
from math import ceil
N,D = map(int,input().split())
r = [[0]*D for i in range(D)]
for i in range(N):
    x,y = map(int,input().split())
    r[x%D][y%D] += 1

M = 0
for i in range(D):
    for j in range(D):
        M = max(M,r[i][j])

a = ceil(M**0.5-1)

l = 0
b = D-1
mid = (l+b)//2

def possible(b):
    cum = [[0]*(D+1) for i in range(D+1)]
    for i in range(D):
        for j in range(D):
            if a**2+a<r[i][j]:
                if i<=b:
                    cum[b+1-i][0] += 1
                    cum[D-i][0] -= 1
                else:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[D+b+1-i][0] += 1
                if j<=b:
                    cum[0][b+1-j] += 1
                    cum[0][D-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[0][D-j] -= 1
                    cum[0][D+b+1-j]
            elif a**2<r[i][j]<=a**2+a:
                if i<=b and j<=b:
                    cum[b+1-i][b+1-j] += 1
                    cum[D-i][b+1-j] -= 1
                    cum[b+1-i][D-j] -= 1
                    cum[D-i][D-j] += 1
                elif b+1<=i and j<=b:
                    cum[0][b+1-j] += 1
                    cum[D-i][b+1-j] -= 1
                    cum[0][D-j] -= 1
                    cum[D-i][D-j] += 1
                    cum[D+b+1-i][b+1-j] += 1
                    cum[D+b+1-i][D-j] -= 1
                elif i<=b and b+1<=j:
                    cum[b+1-i][0] += 1
                    cum[b+1-i][D-j] -= 1
                    cum[D-i][0] -= 1
                    cum[D-i][D-j] += 1
                    cum[b+1-i][D+b+1-j] += 1
                    cum[D-i][D+b+1-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[0][D-j] -= 1
                    cum[D-i][D-j] += 1
                    cum[0][D+b+1-j] += 1
                    cum[D-i][D+b+1-j] -= 1
                    cum[D+b+1-i][0] += 1
                    cum[D+b+1-i][D-j] -= 1
                    cum[D+b+1-i][D+b+1-j] += 1
    for i in range(1,D):
        for j in range(D):
            cum[i][j] += cum[i-1][j]
    for i in range(D):
        for j in range(1,D):
            cum[i][j] += cum[i][j-1]
    res = False
    for i in range(D):
        for j in range(D):
            if cum[i][j] == 0:
                res = True
                break
    return res

for i in range(20):
    if possible(mid):
        b = mid
    else:
        l = mid
    mid = (l+b)//2

print(a*D+b)

Submission status: Wrong Answer

Solution code:
from math import ceil
N,D = map(int,input().split())
r = [[0]*D for i in range(D)]
for i in range(N):
    x,y = map(int,input().split())
    r[x%D][y%D] += 1

M = 0
for i in range(D):
    for j in range(D):
        M = max(M,r[i][j])

a = ceil(M**0.5-1)

l = 0
b = D-1
mid = (l+b)//2

def possible(b):
    cum = [[0]*(D+1) for i in range(D+1)]
    for i in range(D):
        for j in range(D):
            if a**2+a<r[i][j]:
                if i<=b:
                    cum[b+1-i][0] += 1
                    cum[D-i][0] -= 1
                else:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[D+b+1-i][0] += 1
                if j<=b:
                    cum[0][b+1-j] += 1
                    cum[0][D-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[0][D-j] -= 1
                    cum[0][D+b+1-j]
            elif a**2<r[i][j]<=a**2+a:
                if i<=b and j<=b:
                    cum[b+1-i][b+1-j] += 1
                    cum[D-i][b+1-j] -= 1
                    cum[b+1-i][D-j] -= 1
                    cum[D-i][D-j] += 1
                elif b+1<=i and j<=b:
                    cum[0][b+1-j] += 1
                    cum[D-i][b+1-j] -= 1
                    cum[0][D-j] -= 1
                    cum[D-i][D-j] += 1
                    cum[D+b+1-i][b+1-j] += 1
                    cum[D+b+1-i][D-j] -= 1
                elif i<=b and b+1<=j:
                    cum[b+1-i][0] += 1
                    cum[b+1-i][D-j] -= 1
                    cum[D-i][0] -= 1
                    cum[D-i][D-j] += 1
                    cum[b+1-i][D+b+1-j] += 1
                    cum[D-i][D+b+1-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[0][D-j] -= 1
                    cum[D-i][D-j] += 1
                    cum[0][D+b+1-j] += 1
                    cum[D-i][D+b+1-j] -= 1
                    cum[D+b+1-i][0] += 1
                    cum[D+b+1-i][D-j] -= 1
                    cum[D+b+1-i][D+b+1-j] += 1
    for i in range(1,D):
        for j in range(D):
            cum[i][j] += cum[i-1][j]
    for i in range(D):
        for j in range(1,D):
            cum[i][j] += cum[i][j-1]
    res = False
    for i in range(D):
        for j in range(D):
            if cum[i][j] == 0:
                res = True
                break
    return res

for i in range(10):
    if possible(mid):
        b = mid
    else:
        l = mid
    mid = (l+b)//2

print(a*D+b)