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 math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,d = LI()
    xy = [LI() for _ in range(n)]
    t = collections.defaultdict(int)
    for x,y in xy:
        t[(x%d,y%d)] += 1

    m = 0
    ma = []
    mb = []
    mc = []
    for k,v in t.items():
        s = int(v**0.5)
        u = -1
        for i in range(max(0,s-2),s+2):
            if i*i >= v:
                u = i
                break

        if m < u:
            m = u
            ma = []
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)
        elif m == u:
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)

    if d <= 50:
        mr = inf
        for x,y in mb:
            mx = x
            my = y
            for nx,ny in mb:
                if x > nx:
                    nx += d
                if y > ny:
                    ny += d
                if mx < nx:
                    mx = nx
                if my < ny:
                    my = ny

            mm = max(mx-x, my-y)
            for tm in range(mm,mm+d+100):
                f = 1
                for nx,ny in mc:
                    if x > nx:
                        nx += d
                    if y > ny:
                        ny += d
                    if nx - x > tm and ny - y > tm:
                        f = 0
                        break
                if f == 1:
                    if mr > tm:
                        mr = tm
                        break

        for x,y in mc:
            mx = 0
            my = 0
            for nx,ny in mb:
                if x > nx:
                    nx += d
                if y > ny:
                    ny += d
                if mx < nx:
                    mx = nx
                if my < ny:
                    my = ny

            mm = max(0, max(mx-x, my-y))
            for tm in range(mm,mm+d+100):
                f = 1
                for nx,ny in mc:
                    if x > nx:
                        nx += d
                    if y > ny:
                        ny += d
                    if nx - x > tm and ny - y > tm:
                        f = 0
                        break
                if f == 1:
                    if mr > tm:
                        mr = tm
                        break

        return mr + (m-1) * d

    return -m


print(main())

Submission status: Wrong Answer

Solution code:
import math

n, d = map(int, raw_input().split())

print int(math.ceil(n ** 0.5 - 1))

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,d = LI()
    xy = [LI() for _ in range(n)]
    t = collections.defaultdict(int)
    for x,y in xy:
        t[(x%d,y%d)] += 1

    m = 0
    ma = []
    mb = []
    mc = []
    for k,v in t.items():
        s = int(v**0.5)
        u = -1
        for i in range(max(0,s-2),s+2):
            if i*i >= v:
                u = i
                break

        if m < u:
            m = u
            ma = []
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)
        elif m == u:
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)

    if d <= 50:
        mr = inf
        for x,y in mb:
            mx = x
            my = y
            for nx,ny in mb:
                if x > nx:
                    nx += d
                if y > ny:
                    ny += d
                if mx < nx:
                    mx = nx
                if my < ny:
                    my = ny

            mm = max(mx-x, my-y)
            for tm in range(mm,mm+d+100):
                f = 1
                for nx,ny in mc:
                    if x > nx:
                        nx += d
                    if y > ny:
                        ny += d
                    if nx - x > tm and ny - y > tm:
                        f = 0
                        break
                if f == 1:
                    if mr > tm:
                        mr = tm
                        break

        for x,y in mc:
            mx = x
            my = 0
            for nx,ny in mb:
                if x > nx:
                    nx += d
                if y > ny:
                    ny += d
                if mx < nx:
                    mx = nx
                if my < ny:
                    my = ny

            mm = max(mx-x, my-y)
            for tm in range(mm,mm+d+100):
                f = 1
                for nx,ny in mc:
                    if x > nx:
                        nx += d
                    if y > ny:
                        ny += d
                    if nx - x > tm and ny - y > tm:
                        f = 0
                        break
                if f == 1:
                    if mr > tm:
                        mr = tm
                        break

        for x,y in mc:
            mx = 0
            my = y
            for nx,ny in mb:
                if x > nx:
                    nx += d
                if y > ny:
                    ny += d
                if mx < nx:
                    mx = nx
                if my < ny:
                    my = ny

            mm = max(mx-x, my-y)
            for tm in range(mm,mm+d+100):
                f = 1
                for nx,ny in mc:
                    if x > nx:
                        nx += d
                    if y > ny:
                        ny += d
                    if nx - x > tm and ny - y > tm:
                        f = 0
                        break
                if f == 1:
                    if mr > tm:
                        mr = tm
                        break

        return mr + (m-1) * d

    return -m


print(main())

Submission status: Wrong Answer

Solution code:
N,D = map(int,input().split())
pos = [[0 for _ in range(D)] for i in range(D)]
flg = [[0 for _ in range(D)] for i in range(D)]
for i in range(N):
    x,y = map(int,input().split())
    pos[x%D][y%D] += 1
maxn = -1

for i in range(D):
    for j in range(D):
        maxn = max(maxn,pos[i][j])
sqs = int((maxn-1)**0.5)+1

lx = [0 for _ in range(D)]
ly = [0 for _ in range(D)]
for i in range(D):
    for j in range(D):
        if pos[i][j]<=(sqs-1)**2:
            pass
        elif pos[i][j]<=(sqs-1)*sqs:
            flg[i][j] = 1
            lx[i] = max(lx[i],1)
            ly[j] = max(ly[i],1)
        else:
            flg[i][j] = 2
            lx[i] = 2
            ly[j] = 2
flaggg = False            
for sq in range(D):
    for i in range(D):
        for j in range(D):
            flagg = True
            for x in range(D):
                for y in range(D):
                    if (i<=x<=i+sq or i<=x+D<=i+sq)and (j<=y<=j+sq  or j<=y+D<=j+sq):
                        pass
                    elif (i<=x<=i+sq or i<=x+D<=i+sq) or (j<=y<=j+sq  or j<=y+D<=j+sq):
                        if flg[x][y]==2:
                            flagg = False
                            break
                    else:
                        if flg[x][y]==1:
                            flagg = False
                            break
                if not flagg:
                    break
            if flagg:
                flaggg = True
                break
        if flaggg:
            break
    if flaggg:
        break
print((int((maxn-1)**0.5))*D+sq)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,d = LI()
    xy = [LI() for _ in range(n)]
    t = collections.defaultdict(int)
    for x,y in xy:
        t[(x%d,y%d)] += 1

    m = 0
    ma = []
    mb = []
    mc = []
    for k,v in t.items():
        s = int(v**0.5)
        u = -1
        for i in range(max(0,s-2),s+2):
            if i*i >= v:
                u = i
                break

        if m < u:
            m = u
            ma = []
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)
        elif m == u:
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)

    if d <= 50:
        mr = inf
        for x,y in ma:
            mx = x
            my = y
            for nx,ny in mb:
                if x > nx:
                    nx += d
                if y > ny:
                    ny += d
                if mx < nx:
                    mx = nx
                if my < ny:
                    my = ny

            mm = max(mx-x, my-y)
            for tm in range(mm,mm+d+100):
                f = 1
                for nx,ny in mc:
                    if x > nx:
                        nx += d
                    if y > ny:
                        ny += d
                    if nx - x > tm and ny - y > tm:
                        f = 0
                        break
                if f == 1:
                    if mr > tm:
                        mr = tm
                        break

        return mr + (m-1) * d

    return -m


print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,d = LI()
    xy = [LI() for _ in range(n)]
    t = collections.defaultdict(int)
    for x,y in xy:
        t[(x%d,y%d)] += 1

    m = 0
    ma = []
    mb = []
    mc = []
    for k,v in t.items():
        s = int(v**0.5)
        u = -1
        for i in range(max(0,s-2),s+2):
            if i*i >= v:
                u = i
                break

        if m < u:
            m = u
            ma = []
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)
        elif m == u:
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)

    if d <= 33:
        mr = inf
        for x,y in ma:
            mx = x
            my = y
            for nx,ny in mb:
                if x > nx:
                    nx += d
                if y > ny:
                    ny += d
                if mx < nx:
                    mx = nx
                if my < ny:
                    my = ny

            mm = max(mx-x, my-y)
            for tm in range(mm,mm+d+10):
                f = 1
                for nx,ny in mc:
                    if x > nx:
                        nx += d
                    if y > ny:
                        ny += d
                    if nx - x > tm and ny - y > tm:
                        f = 0
                        break
                if f == 1:
                    if mr > tm:
                        mr = tm
                        break

        return mr + (m-1) * d

    return -m


print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,d = LI()
    xy = [LI() for _ in range(n)]
    t = collections.defaultdict(int)
    for x,y in xy:
        t[(x%d,y%d)] += 1

    m = 0
    ma = []
    mb = []
    mc = []
    for k,v in t.items():
        s = int(v**0.5)
        u = -1
        for i in range(max(0,s-2),s+2):
            if i*i >= v:
                u = i
                break

        if m < u:
            m = u
            ma = []
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)
        elif m == u:
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)

    if len(ma) == 1:
        return (m-1) * d

    if d <= 33:
        mr = inf
        for x,y in ma:
            mx = x
            my = y
            for nx,ny in mb:
                if x > nx:
                    nx += d
                if y > nx:
                    ny += d
                if mx < nx:
                    mx = nx
                if my < ny:
                    my = ny

            mm = max(mx-x, my-y)
            for tm in range(mm,mm+d+10):
                f = 1
                for nx,ny in mc:
                    if x > nx:
                        nx += d
                    if y > nx:
                        ny += d
                    if nx - x > tm and ny - y > tm:
                        f = 0
                        break
                if f == 1:
                    if mr > tm:
                        mr = tm
                        break

        return mr + (m-1) * d

    return -m


print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,d = LI()
    xy = [LI() for _ in range(n)]
    t = collections.defaultdict(int)
    for x,y in xy:
        t[(x%d,y%d)] += 1

    m = 0
    ma = []
    mb = []
    mc = []
    for k,v in t.items():
        s = int(v**0.5)
        u = -1
        for i in range(max(0,s-2),s+2):
            if i*i >= v:
                u = i
                break

        if m < u:
            m = u
            ma = []
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)
        elif m == u:
            ma.append(k)
            if m*(m-1) >= v:
                mc.append(k)
            else:
                mb.append(k)

    if len(ma) == 1:
        return (m-1) * d

    if d <= 30:
        mr = inf
        for x,y in ma:
            mx = x
            my = y
            for nx,ny in mb:
                if x > nx:
                    nx += d
                if y > nx:
                    ny += d
                if mx < nx:
                    mx = nx
                if my < ny:
                    my = ny

            mm = max(mx-x, my-y)
            for tm in range(mm,mm+d+1):
                f = 1
                for nx,ny in mc:
                    if x > nx:
                        nx += d
                    if y > nx:
                        ny += d
                    if nx - x > tm and ny - y > tm:
                        f = 0
                        break
                if f == 1:
                    if mr > tm:
                        mr = tm
                        break
        return mr + (m-1) * d

    return -m


print(main())

Submission status: Wrong Answer

Solution code:
N,D = map(int,input().split())
pos = [[0 for _ in range(D)] for i in range(D)]
for i in range(N):
    x,y = map(int,input().split())
    pos[x%D][y%D] += 1
maxn = -1
for i in range(D):
    for j in range(D):
        if maxn < pos[i][j]:
            maxposx = set([i])
            maxposy = set([j])
            maxn = pos[i][j]
        elif maxn == pos[i][j]:
            maxposx.add(i)
            maxposy.add(j)
maxposx.add(min(maxposx)+D)
maxposy.add(min(maxposy)+D)
maxx = list(maxposx)
maxy = list(maxposy)
maxx.sort()
maxy.sort()
sukx = 0
for i in range(len(maxx)-1):
    sukx = max(sukx,maxx[i+1]-maxx[i])
suky = 0
for i in range(len(maxy)-1):
    suky = max(suky,maxy[i+1]-maxy[i])
print((int((maxn-1)**0.5))*D+max(D-sukx,D-suky))

Submission status: Time Limit Exceeded

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 range(D):
    for j in range(D):
        c=0
        for k in s0:
            t=min((k[0]-i)%D,(k[1]-j)%D)
            if c<t:
                c=t
        for k in s1:
            t=max((k[0]-i)%D,(k[1]-j)%D)
            if c<t:
                c=t
        if a>c:
            a=c
print(D*(m-1)+a)

Submission status: Time Limit Exceeded

Solution code:
import sys,heapq,math
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

n,d = inm()
x = []
y = []
for i in range(n):
    xx,yy = inm()
    x.append(xx)
    y.append(yy)

ddprint("n {} d {}".format(n,d))
ddprint(x)
ddprint(y)

rdsq = [ [0] * d for i in range(d) ]
for i in range(n):
    rdsq[x[i]%d][y[i]%d] += 1
mx = max([ max(z) for z in rdsq ])
rt = int(math.sqrt(mx))
if rt*rt < mx:
    rt += 1

ddprint("mx {} rt {} rdsq:".format(mx,rt))
ddprint(rdsq)

g1 = []
g2 = []
for i in range(d):
  for j in range(d):
      if rdsq[i][j] > rt*(rt-1):
          g2.append((i,j))
      elif rdsq[i][j] > (rt-1)*(rt-1):
          g1.append((i,j))

mn = 99999999
for i in range(d):
    for j in range(d):
        x2 = max([max([(z[0]+i)%d, (z[1]+j)%d]) for z in g2]) if len(g2)>0 else 0
        x1 = max([min([(z[0]+i)%d, (z[1]+j)%d]) for z in g1]) if len(g1)>0 else 0
        xx = max(x1,x2)
        mn = min(mn, xx)

print((rt-1)*d + mn)

Submission status: Time Limit Exceeded

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[1][i+d-1][j+d-1] - rui[1][i+kon+1 - 1][j+d-1] - rui[1][i+d-1][j+kon+1 - 1] + rui[1][i+kon+1 - 1][j+kon+1 - 1]:
        continue
      if rui[0][i+d-1][j+kon] - rui[0][i+kon+1 - 1][j+kon] - rui[0][i+d-1][j - 1] + rui[0][i+kon+1 - 1][j - 1]:
        continue
      if rui[0][i+kon][j+d-1] - rui[0][i - 1][j+d-1] - rui[0][i+kon][j+kon+1 - 1] + rui[0][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: Time Limit Exceeded

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(kon):
  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 k<= kon and l<= kon:
            if num[i2][j2] > (a+1)**2:
              flag = 0
          elif kon < k and kon < l:
            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)
  if c == 1:
    dai = kon
  else:
    syo = kon
print(dai+r)

Submission status: Time Limit Exceeded

Solution code:
# D
N, D = map(int, input().split())
xyd_list = [[0]*D for _ in range(D)]

for _ in range(N):
    x, y = map(int, input().split())
    xyd_list[x%D][y%D] += 1
    
# how many copy we need
mmm = max([max(x) for x in xyd_list])
bound = 1
while bound**2 < mmm:
    bound += 1
    
res = D*bound - 1

# bruce for 500
for i in range(D):
    for j in range(D):
        res_t = D*(bound - 1)
        for k in range(D):
            i_ = (i+k)%D
            for l in range(D):
                j_ = (j+l)%D
                if xyd_list[i_][j_] <= (bound - 1)**2:
                    pass
                elif xyd_list[i_][j_] <= (bound - 1)*bound:
                    res_t = max(res_t, D*(bound - 1) + min(k, l))
                else:
                    res_t = max(res_t, D*(bound - 1) + max(k, l))
        if res_t < res:
            res = res_t
print(res)

Submission status: Time Limit Exceeded

Solution code:
from math import sqrt
N, D = map(int, input().split())
C = [[0]*(D) for i in range(D)]
for i in range(N):
    x, y = map(int, input().split())
    C[x % D][y % D] += 1
sz = 0
P = []
for i in range(D):
    for j in range(D):
        c = C[i][j]
        if c == 0:
            continue
        z = 1
        while z**2 < c:
            z += 1
        if sz < z:
            sz = z
            P = [(j, i, c)]
        elif sz == z:
            P.append((j, i, c))
ans = 10**18
for i in range(D):
    for j in range(D):
        left = 0; right = 10**9
        while left+1 < right:
            mid = (left + right) // 2
            ok = 1
            for x, y, c in P:
                if x < j:
                    x += D
                if y < i:
                    y += D
                if c > (1+(mid+j-x)//D)*(1+(mid+i-y)//D):
                    ok = 0
                    break
            if ok:
                right = mid
            else:
                left = mid
        ans = min(ans, right)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,D = map(int,input().split())
pos = [[0 for _ in range(D)] for i in range(D)]
flg = [[0 for _ in range(D)] for i in range(D)]
for i in range(N):
    x,y = map(int,input().split())
    pos[x%D][y%D] += 1
maxn = -1

for i in range(D):
    for j in range(D):
        maxn = max(maxn,pos[i][j])
sqs = int((maxn-1)**0.5)+1

for i in range(D):
    for j in range(D):
        if pos[i][j]<=(sqs-1)**2:
            pass
        elif pos[i][j]<=(sqs-1)*sqs:
            flg[i][j] = 1
        else:
            flg[i][j] = 2
flaggg = False            
for sq in range(D):
    for i in range(D):
        for j in range(D):
            flagg = True
            for x in range(D):
                for y in range(D):
                    if (i<=x<=i+sq or i<=x+D<=i+sq)and (j<=y<=j+sq  or j<=y+D<=j+sq):
                        pass
                    elif (i<=x<=i+sq or i<=x+D<=i+sq) or (j<=y<=j+sq  or j<=y+D<=j+sq):
                        if flg[x][y]==2:
                            flagg = False
                            break
                    else:
                        if flg[x][y]==1 or flg[x][y]==2:
                            flagg = False
                            break
                if not flagg:
                    break
            if flagg:
                flaggg = True
                break
        if flaggg:
            break
    if flaggg:
        break
print((int((maxn-1)**0.5))*D+sq)

Submission status: Accepted

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(3)]
rui = [[[0]*(2*d+1) for i in range(2*d+1)]for i in range(3)]
for i in range(d):
  for j in range(d):
    z = num[i][j]
    if z > (a+1)**2:
      anum[0][i][j]=1
      anum[1][i][j]=1
      anum[2][i][j]=1
    elif z> a*(a+1):
      anum[1][i][j]=1
      anum[2][i][j]=1
    elif z > a*a:
      anum[1][i][j]=1
for x in range(3):
  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+kon][j+kon] - rui[0][i - 1][j+kon] - rui[0][i+kon][j - 1] + rui[0][i - 1][j - 1]:
        continue
      if rui[1][i+d-1][j+d-1] - rui[1][i+kon+1 - 1][j+d-1] - rui[1][i+d-1][j+kon+1 - 1] + rui[1][i+kon+1 - 1][j+kon+1 - 1]:
        continue
      if rui[2][i+d-1][j+kon] - rui[2][i+kon+1 - 1][j+kon] - rui[2][i+d-1][j - 1] + rui[2][i+kon+1 - 1][j - 1]:
        continue
      if rui[2][i+kon][j+d-1] - rui[2][i - 1][j+d-1] - rui[2][i+kon][j+kon+1 - 1] + rui[2][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: Accepted

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_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)