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:
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[b+1-i][0] -= 1
                    cum[0][b+1-j] -= 1
                    cum[b+1-i][b+1-j] += 1
                    cum[0][D-j] += 1
                    cum[b+1-i][D-j] -= 1
                    cum[D-i][0] += 1
                    cum[D-i][b+1-j] -= 1
                    cum[D-i][D-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)

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-i][0] += 1
                    cum[D-i][0] -= 1
                else:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[D+b-i][0] += 1
                if j<=b:
                    cum[0][b-j] += 1
                    cum[0][D-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[0][D-j] -= 1
                    cum[0][D+b-j]
            elif a**2<r[i][j]<=a**2+a:
                if i<=b and j<=b:
                    cum[b-i][b-j] += 1
                    cum[D-i][b-j] -= 1
                    cum[b-i][D-j] -= 1
                    cum[D-i][D-j] += 1
                elif b+1<=i and j<=b:
                    cum[0][b-j] += 1
                    cum[D-i][b-j] -= 1
                    cum[0][D-j] -= 1
                    cum[D-i][D-j] += 1
                    cum[D+b-i][b-j] += 1
                    cum[D+b-i][D-j] -= 1
                elif i<=b and b+1<=j:
                    cum[b-i][0] += 1
                    cum[b-i][D-j] -= 1
                    cum[D-i][0] -= 1
                    cum[D-i][D-j] += 1
                    cum[b-i][D+b-j] += 1
                    cum[D-i][D+b-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[b-i][0] -= 1
                    cum[0][b-j] -= 1
                    cum[b-i][b-j] += 1
                    cum[0][D-j] += 1
                    cum[b-i][D-j] -= 1
                    cum[D-i][0] += 1
                    cum[D-i][b-j] -= 1
                    cum[D-i][D-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)

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-i][0] += 1
                    cum[D-i][0] -= 1
                else:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[D+b-i][0] += 1
                if j<=b:
                    cum[0][b-j] += 1
                    cum[0][D-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[0][D-j] -= 1
                    cum[0][D+b-j]
            elif a**2<r[i][j]<=a**2+a:
                if b+1<=i and b+1<=j:
                    cum[b-i][b-j] += 1
                    cum[D-i][b-j] -= 1
                    cum[b-i][D-j] -= 1
                    cum[D-i][D-j] += 1
                elif b+1<=i and j<=b:
                    cum[0][b-j] += 1
                    cum[D-i][b-j] -= 1
                    cum[0][D-j] -= 1
                    cum[D-i][D-j] += 1
                    cum[D+b-i][b-j] += 1
                    cum[D+b-i][D-j] -= 1
                elif i<=b and b+1<=j:
                    cum[b-i][0] += 1
                    cum[b-i][D-j] -= 1
                    cum[D-i][0] -= 1
                    cum[D-i][D-j] += 1
                    cum[b-i][D+b-j] += 1
                    cum[D-i][D+b-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[b-i][0] -= 1
                    cum[0][b-j] -= 1
                    cum[b-i][b-j] += 1
                    cum[0][D-j] += 1
                    cum[b-i][D-j] -= 1
                    cum[D-i][0] += 1
                    cum[D-i][b-j] -= 1
                    cum[D-i][D-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)

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
mid = (l+b)//2

def possible(b):
    if b == 0:
        return False
    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 0<=i<=b-1:
                    cum[0][0] += 1
                    cum[b-i][0] -= 1
                    cum[D-i][0] += 1
                else:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                if 0<=j<=b-1:
                    cum[0][0] += 1
                    cum[0][b-j] -= 1
                    cum[0][D-j] += 1
                else:
                    cum[0][0] += 1
                    cum[0][D-j] -= 1
            elif a**2<r[i][j]<=a**2+a:
                if b <= i and b <= j:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[0][D-j] -= 1
                    cum[D-i][D-j] += 1
                elif b<=i and j<=b-1:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[0][b-j] -= 1
                    cum[D-i][b-j] += 1
                    cum[0][D-j] += 1
                    cum[D-i][D-j] -= 1
                elif i<=b-1 and b <= j:
                    cum[0][0] += 1
                    cum[b-i][0] -= 1
                    cum[0][D-j] -= 1
                    cum[b-i][D-j] += 1
                    cum[D-i][0] += 1
                    cum[D-i][D-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[b-i][0] -= 1
                    cum[0][b-j] -= 1
                    cum[b-i][b-j] += 1
                    cum[0][D-j] += 1
                    cum[b-i][D-j] -= 1
                    cum[D-i][0] += 1
                    cum[D-i][b-j] -= 1
                    cum[D-i][D-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,cum]

for i in range(10):
    if possible(mid):
        b = mid
    else:
        l = mid
    mid = (l+b)//2
if b >= 2:
    print(a*D+b)
else:
    a -= 1
    if possible(D):
        print((a+1)*D)
    else:
        print((a+1)*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
mid = (l+b)//2

def possible(b):
    if b == 0:
        return False
    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 0<=i<=b-1:
                    cum[0][0] += 1
                    cum[b-i][0] -= 1
                    cum[D-i][0] += 1
                else:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                if 0<=j<=b-1:
                    cum[0][0] += 1
                    cum[0][b-j] -= 1
                    cum[0][D-j] += 1
                else:
                    cum[0][0] += 1
                    cum[0][D-j] -= 1
            elif a**2<r[i][j]<=a**2+a:
                if b <= i and b <= j:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[0][D-j] -= 1
                    cum[D-i][D-j] += 1
                elif b<=i and j<=b-1:
                    cum[0][0] += 1
                    cum[D-i][0] -= 1
                    cum[0][b-j] -= 1
                    cum[D-i][b-j] += 1
                    cum[0][D-j] += 1
                    cum[D-i][D-j] -= 1
                elif i<=b-1 and b <= j:
                    cum[0][0] += 1
                    cum[b-i][0] -= 1
                    cum[0][D-j] -= 1
                    cum[b-i][D-j] += 1
                    cum[D-i][0] += 1
                    cum[D-i][D-j] -= 1
                else:
                    cum[0][0] += 1
                    cum[b-i][0] -= 1
                    cum[0][b-j] -= 1
                    cum[b-i][b-j] += 1
                    cum[0][D-j] += 1
                    cum[b-i][D-j] -= 1
                    cum[D-i][0] += 1
                    cum[D-i][b-j] -= 1
                    cum[D-i][D-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)

Submission status: Wrong Answer

Solution code:
n, d = [int(i) for i in input().split()]
X, Y = zip(*[[int(i) for i in input().split()] for j in range(n)])

ans = int(1e9)

for bx in range(d):
    for by in range(d):
        S = set()

        for x, y in zip(X, Y):
            x %= d
            x += d if x < bx else 0
            y %= d
            y += d if y < by else 0
            dy = 0
            try:
                if x > y:
                    while True:
                        for dx in range(dy+1):
                            if not (x+dx*d, y+dy*d) in S:
                                S.add((x+dx*d, y+dy*d))
                                raise()
                        dx = dy
                        for dy in range(dx+1):
                            if not (x+dx*d, y+dy*d) in S:
                                S.add((x+dx*d, y+dy*d))
                                raise()
                        dy += 1
                else:
                    while True:
                        for ddy in range(dy+1):
                            if not (x+dy*d, y+ddy*d) in S:
                                S.add((x+dy*d, y+ddy*d))
                                raise()
                        for dx in range(dy+1):
                            if not (x+dx*d, y+dy*d) in S:
                                S.add((x+dx*d, y+dy*d))
                                raise()
                        dy += 1
            except:
                    pass

        X, Y = zip(*tuple(S))
        ans = min(ans, max(abs(max(X) - min(X)), (max(Y) - min(Y))))

print(ans)

Submission status: Wrong Answer

Solution code:
n, d = [int(i) for i in input().split()]
X, Y = zip(*[[int(i) for i in input().split()] for j in range(n)])

S = set()

for x, y in zip(X, Y):
    x %= d
    y %= d
    dy = 0
    try:
        if x > y:
            while True:
                for dx in range(dy+1):
                    if not (x+dx*d, y+dy*d) in S:
                        S.add((x+dx*d, y+dy*d))
                        raise()
                dx = dy
                for dy in range(dx+1):
                    if not (x+dx*d, y+dy*d) in S:
                        S.add((x+dx*d, y+dy*d))
                        raise()
                dy += 1
        else:
            while True:
                for ddy in range(dy+1):
                    if not (x+dy*d, y+ddy*d) in S:
                        S.add((x+dy*d, y+ddy*d))
                        raise()
                for dx in range(dy+1):
                    if not (x+dx*d, y+dy*d) in S:
                        S.add((x+dx*d, y+dy*d))
                        raise()
                dy += 1
    except:
            pass

X, Y = zip(*tuple(S))

print(max(abs(max(X) - min(X)), (max(Y) - min(Y))))

Submission status: Wrong Answer

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 = int(sqrt(c))
        if z*z < c:
            z += 1
        if sz < z:
            sz = z
            P = [(j, i, c)]
        elif sz == z:
            P.append((j, i, c))
ans = 10**18
for j, i, _ in P:
    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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

N, D = map(int, input().split())

diff_dict  = {}

for n in range(N):
	point_x, point_y = map(int, input().split())
	if point_x >= 0 :
		diff_x = point_x % D
	else:
		if abs(point_x) % D == 0:
			diff_x = 0
		else:
			diff_x = abs(point_x) % D
	if point_y >= 0 :
		diff_y = point_y % D
	else:
		if abs(point_y) % D == 0:
			diff_y = 0
		else:
			diff_y = abs(point_y) % D

	if (diff_x, diff_y) in diff_dict.keys():
		diff_dict[(diff_x, diff_y)] += 1
	else:
		diff_dict[(diff_x, diff_y)] = 1

n = 1
n_2 = (n+1) * (n+1)

while max(diff_dict.values()) > n_2:

	n += 1
	n_2 = (n+1) * (n+1)

diff_a = 0

max_k = max(diff_dict, key=diff_dict.get)

for i in diff_dict.keys():
	if diff_dict[i] > n*n:
		diff_a = max(min(abs(i[0]-max_k[0]), abs(i[0]-D)), min(abs(i[1]-max_k[1]), abs(i[1]-D)))

answer = n * D + diff_a

print(answer)

Submission status: Wrong Answer

Solution code:
n,d = map(int,input().split())

m = [[0] *d for i in range(d)]
for i in range(n):
    bnm,jkl = map(int,input().split())
    i = bnm%d
    j = jkl%d
    m[i][j]+=1



for i in range(d):
    for j in range(d):
        if  m[i][j] != 0:
            l = 1
            if i <= d-i:
                p = i
            p = min(i,abs(d-i))
            q = min(j,abs(d-j))
            
            g = 10**5
            h = 0
            for qqq in range(100):
                a = (g+h)//2
                c = ((a-p)//d+(a+p)//d+1)*((a-q)//d+(a-q)//d+1)
                if c > m[i][j]:
                    g = a
                else:
                    h = a
            a = h
            if ((a-p)//d+(a+p)//d+1)*((a-q)//d+(a-q)//d+1)>=m[i][j]:
                m[i][j] = a
            else:
                m[i][j] = g

res = m[0][0]
for i in range(d):
    for j in range(d):
        res = max(res,m[i][j])
print(res)

Submission status: Wrong Answer

Solution code:
import math
def intsqrt(k):
    tmp=int(math.sqrt(k))
    if tmp**2==k:
        return tmp
    else:
        return tmp+1
    
N,D=map(int,input().split())
x=[0 for i in range(N)]
y=[0 for i in range(N)]
for i in range(N):
    x[i],y[i]=map(int,input().split())
A=[[0 for i in range(D)] for i in range(D)]
for i in range(N):
    A[x[i]%D][y[i]%D]+=1
B=[[0 for i in range(D)] for i in range(D)]
for i in range(D):
    for j in range(D):
        if A[i][j]==0:
            continue
        B[i][j]=D*(intsqrt(A[i][j])-1)
X=[0,B[0][0]]
Y=[0,B[0][0]]
for i in range(D):
    for j in range(D):
        if (i,j)==(0,0):
            continue
        L=[(0,0),(-D,0),(0,-D),(-D,-D)]
        dX=X[1]-X[0]
        dY=Y[1]-Y[0]
        dR=max(dX,dY)
        for r in L:
            Xtmp0=[i+r[0] for i in X]
            Ytmp0=[i+r[1] for i in Y]
            Xtmp=[min(Xtmp0[0],X[0]),max(Xtmp0[1],X[1])]
            Ytmp=[min(Ytmp0[0],Y[0]),max(Ytmp0[1],Y[1])]
            dR=min(dR,max(Xtmp[1]-Xtmp[0],Ytmp[1]-Ytmp[0]))
        for r in L:
            Xtmp0=[i+r[0] for i in X]
            Ytmp0=[i+r[1] for i in Y]
            Xtmp=[min(Xtmp0[0],X[0]),max(Xtmp0[1],X[1])]
            Ytmp=[min(Ytmp0[0],Y[0]),max(Ytmp0[1],Y[1])]
            if max(Xtmp[1]-Xtmp[0],Ytmp[1]-Ytmp[0])==dR:
                X=[Xtmp[0],Xtmp[1]]
                Y=[Ytmp[0],Ytmp[1]]
print(max(X[1]-X[0],Y[1]-Y[0])+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
        xs = set()
        ys = set()
        for x,y in ma:
            xs.add(x)
            ys.add(y)
        txy = []
        for x in xs:
            for y in ys:
                txy.append((x,y))

        for x,y in txy:
            for tm in range(d+1000):
                f = 1
                for nx,ny in mb:
                    if x > nx:
                        nx += d
                    if y > ny:
                        ny += d
                    if nx - x > tm or ny - y > tm:
                        f = 0
                        break
                if f == 0:
                    continue

                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 <= 50 or True:
        mr = inf
        xs = set()
        ys = set()
        for x,y in ma:
            xs.add(x)
            ys.add(y)
        txy = []
        for x in xs:
            for y in ys:
                txy.append((x,y))

        for x,y in txy:
            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+1000):
                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 <= 50:
        mr = inf
        xs = set()
        ys = set()
        for x,y in ma:
            xs.add(x)
            ys.add(y)
        txy = []
        for x in xs:
            for y in ys:
                txy.append((x,y))

        for x,y in txy:
            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,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 = 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())