Problem:


Score : 200 points

Problem Statement

There are N points in a D-dimensional space.

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

Constraints

All values in input are integers.

2 \leq N \leq 10

1 \leq D \leq 10

-20 \leq X_{ij} \leq 20

No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}.

Input

Input is given from Standard Input in the following format:

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.

Sample Input 1

3 2
1 2
5 5
-2 8

Sample Output 1

1

The number of pairs with an integer distance is one, as follows:

The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.

The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer.

The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer.

Sample Input 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

Sample Output 2

2

Sample Input 3

5 1
1
2
3
4
5

Sample Output 3

10



Sample input:
3 2
1 2
5 5
-2 8


Sample output:
1

Submission status: Accepted

Solution code:
import numpy as np
N, D = [int(n) for n in input().split()]
X = [[int(n) for n in input().split()] for _ in range(N)]
cnt = 0
for i in range(N-1):
    for j in range(i+1, N):
        d2 = 0
        for k in range(D):
            d2 += (X[i][k] - X[j][k]) * (X[i][k] - X[j][k])
        d = np.sqrt(d2)
        if d == int(d):
            cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n,d = map(int,input().split())
x = [0 for _ in range(d)]*n
for i in range(n):
    x[i] = [int(_) for _ in input().split()]
res = 0
sq = [i*i for i in range(10000)] 
for i in range(n):
    for j in range(n):
        if i != j:
            if sum([(x[i][k]-x[j][k])**2 for k in range(d)]) in sq:
                res += 1
print(res//2)

Submission status: Accepted

Solution code:
from math import sqrt

N, D = list(map(int, input().split()))
X = [[] for i in range(N)]
for i in range(N):
    X[i] = list(map(int,input().split()))

num = 0
for i in range(N):
    for j in range(i+1, N):
        diff = [(X[i][k] - X[j][k])**2 for k in range(D)]
        dist = sum(diff)**.5
        if dist == int(dist):
            num += 1

print(num)

Submission status: Accepted

Solution code:
N,D = map(int,input().split())
X = [ list(map(int,input().split())) for i in range(N)]

ans = 0
l = [ i for i in range(N)]
import itertools
for a in itertools.combinations(l,2):
    dis = 0
    for k in range(D):
        dis += (X[a[0]][k] - X[a[1]][k])**2
    dis = dis**(1/2)
    if float.is_integer(dis):
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import math
N,D = [int(i) for i in input().split()]
X = [[int(i) for i in input().split()] for _ in range(N)]
n = 0 
for i in range(N-1):
    for j in range(i+1, N):
        dis = math.sqrt(sum([(xi-xj)**2 for xi,xj in zip(X[i], X[j])]))
        if dis == math.floor(dis):
            n+=1
print(n)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

mi = lambda: map(int, input().split())
li = lambda: list(map(int, input().split()))

import math

N, D = mi()

X = [li() for _ in range(N)]

count = 0
for i in range(N):
  for j in range(N):
    if i == j:
      continue
    distance = math.sqrt(sum([(X[i][index] - X[j][index]) ** 2 for index in range(D)]))
    if int(distance) == distance:
      count += 1

print(count // 2)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
c = []
e = 0
for i in range(a):
    c.append(list(map(int, input().split())))
for i in range(a):
    for j in range(i + 1, a):
        d = 0
        for k in range(b):
            d += (c[i][k] - c[j][k]) ** 2
        d = d ** 0.5
        if d == int(d):
            e += 1
print(e)

Submission status: Accepted

Solution code:
n, d = map(int, list(input().split()))
zahyou = []
for i in range(n):
    zahyou.append(list(map(int, list(input().split()))))

count = 0

for i in range(n):
    for j in range(n):
        nijou_dist = 0
        for k in range(d):
            nijou_dist = nijou_dist + (zahyou[i][k] - zahyou[j][k]) ** 2
        if (nijou_dist ** (1 / 2)).is_integer():
            count = count + 1

print((count - n) // 2)

Submission status: Accepted

Solution code:
N,D = map(int,input().split())
X = []
for i in range(N):
    X.append([int(x) for x in input().split()])
    
ans = 0
for i in range(N-1):
    for j in range(i+1,N):
        sqr = 0   
        for d in range(D):
            sqr += (X[i][d]-X[j][d])**2
        if (sqr ** (1/2)).is_integer():
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
n,d=map(int,input().split())
l=[[] for i in range(n)]
for i in range(n):
    l[i]=list(map(int,input().split()))
ans=0
for i in range(n):
    for j in range(i+1,n):
        cnt=0
        for k in range(d):
            cnt+=(l[i][k]-l[j][k])**2
        #print(cnt)
        for x in range(1000):
            if x**2==cnt:
                ans+=1
print(ans)

Submission status: Accepted

Solution code:
n, d = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
ans = 0
for i in range(n):
    for j in range(i+1, n):
        dis = 0
        for k in range(d):
            dis += (x[i][k]-x[j][k]) ** 2
        dis = dis ** (1/2)
        if dis.is_integer():
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import math

def distance(D, X, Y):
    ans = 0.0
    for i in range(D):
        ans += (X[i]-Y[i])*(X[i]-Y[i])
    return math.sqrt(ans)

def isInteger(n):
    return (n // 1) == n

N, D = map(int,input().split())
X = []
cnt = 0
for i in range(N):
    X.append(list(map(int,input().split())))
for i in range(N):
    for j in range(i+1, N):
        temp = distance(D, X[i], X[j])
        if isInteger(temp):
            cnt += 1
print(str(cnt))

Submission status: Accepted

Solution code:
import math

def checkDisInt(y, z):
    disSum = 0
    for i in range(D):
        disSum += (y[i] - z[i])**2
    if int(math.sqrt(disSum))**2 == disSum:
        return True
    return False

N, D = map(int, input().split())
X = []
for i in range(N):
    X.append(list(map(int, input().split())))

ans = 0
for i in range(N):
    for j in range(i+1, N):
        if checkDisInt(X[i], X[j]):
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right; from collections import Counter, defaultdict, deque, OrderedDict; from copy import deepcopy; from fractions import gcd; from functools import lru_cache, reduce; from math import ceil, floor; from sys import stdin, setrecursionlimit
import heapq as hq; import itertools as its; import operator as op


# globals
inf = float('inf')
input = stdin.readline


def bisearch(f, n):
    l = 0
    r = n
    fl = f(l)
    fr = f(r)
    while l <= r:
        m = (l + r) // 2
        fm = f(m)
        #print l,m,r,fl,fm,fr,n
        if fm == n:
            return m
        elif fm > n:
            r = m - 1
            fr = f(r)
        else:
            l = m + 1
            fl = f(l)


def try_square_root(n2):
    return bisearch(lambda n:n*n, n2)


def main():
    n, d = get_li()
    points = []
    for _ in range(n):
        points.append(get_li())
    ans = 0
    for i in range(n):
        for j in range(i+1, n):
            sq_distance = 0
            for k in range(d):
                sq_distance += (points[i][k] - points[j][k]) ** 2
            if try_square_root(sq_distance):
                ans += 1
    print(ans)
    return


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


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


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


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


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


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


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


if __name__ == '__main__':
    setrecursionlimit(4100000)
    main()

Submission status: Accepted

Solution code:
N,D = map(int,input().split())
X = []
for i in range(N):
  X.append(list(map(int,input().split())))
  
ans = 0
for i in range(N - 1):
  for j in range(i + 1,N):
    a = 0
    for k in range(D):
      a += (X[i][k] - X[j][k]) ** 2
    if int(a ** 0.5) == a ** 0.5:
      ans += 1
print(ans)

Submission status: Accepted

Solution code:
N, D = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N)]
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        t = 0
        for y, z in zip(X[i], X[j]):
            t += abs(y - z) ** 2
        t = t ** 0.5
        if t % 1 == 0:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import math
n, d = map(int, input().split())
x = [[int(i) for i in input().split()] for i in range(n)]
count = 0

def dist(i,j,x, d):
    distance = 0
    for k in range(d):
        distance += (x[i][k] - x[j][k])**2
    distance = math.sqrt(distance)
    return distance
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        else:
            distance = dist(i,j,x, d)
            if distance.is_integer() == True:
                count += 1
print(count//2)

Submission status: Accepted

Solution code:
import math
N, D = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N)]
ans = 0
for i in range(N):
  for j in range(i+1, N):
    d2 = sum([(a-b)**2 for a, b in zip(X[i],X[j])])
    if float(int(math.sqrt(d2))) == math.sqrt(d2):
      ans += 1
print(ans)

Submission status: Accepted

Solution code:
N, D = [int(i) for i in input().split()]
xs = [[int(i) for i in input().split()] for _ in range(N)]

res = 0

for i in range(len(xs)):
    for j in range(i):
        d = (sum((xs[i][k] - xs[j][k])**2 for k in range(D)))**0.5
        if d.is_integer(): res+=1

print(res)

Submission status: Accepted

Solution code:
import math
d, n = map(int, input().split())
listT = []
index=0
while index < d:
    listX = list(map(int, input().split()))
    listT.append(listX)
    index+=1

index2=0
count=0
while index2 < len(listT):
    index3=index2+1
    while index3 < len(listT):
        sumA=0
        index4=0
        while index4 < len(listT[index2]):
            sumA+=(listT[index2][index4]-listT[index3][index4])**2
            index4+=1
        if math.sqrt(sumA).is_integer():
            count+=1
        index3+=1
    index2+=1
print(count)

Submission status: Accepted

Solution code:
n, d = [ int(v) for v in input().split() ]
point_list = [  [ int(v) for v in input().split() ] for i in range(n) ]
square_list = [ i**2 for i in range(10**4+1) ]
ans = 0

for i in range(n):
    for j in range(i+1,n):
        d2 = 0
        for k in range(d):
            d2 += (point_list[i][k] - point_list[j][k])**2
        if d2 in square_list:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import math

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

x_list = []
c = 0
for i in range(N):
    x_list.append(list(map(int, input().split())))
    if i != 0:
        for j in range(i):
            S = 0
            for k in range(D):
                s = (x_list[j][k]-x_list[i][k])**2
                S += s
            a = math.sqrt(S)
            if float.is_integer(a) == True:
                c += 1

print(c)

Submission status: Accepted

Solution code:
N,D = map(int,input().split())

Xs = []

targets = []
count = 0
for i in range(0, 3000):
    targets.append(i**2)

for i in range(0,N):
    Xs.append(list(map(int,input().split())))

for i in range(0,N):
    for h in range(i,N):
        if i == h:
            continue

        dist = 0
        for index in range(len(Xs[0])):
            dist += (Xs[i][index] - Xs[h][index])**2

        if dist in targets:
            count += 1


print(count)

Submission status: Accepted

Solution code:
from math import sqrt
N,D = map(int,input().split())
X = []
for i in range(N):
    X.append(list(map(int,input().split())))

ans = 0
for i in range(N):
    for j in range(i+1,N):
        tmp = 0
        for k in range(D):
            tmp += (X[i][k]-X[j][k])**2
        if int(sqrt(tmp))**2  == tmp:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import math

N, D = map(int, input().split())
X = [0]*N
for i in range(N):
    x = list(map(int, input().split()))
    X[i] = x

ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        dis = 0
        for x1, x2 in zip(X[i], X[j]):
            dis += (x1 - x2)**2
        if math.sqrt(dis) == int(math.sqrt(dis)):
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
import math
N, D = list(map(int,input().split()))

x = [list(map(int, input().split())) for _ in range(N)]


ans = 0

for i in range(N):
    for j in range(i+1, N):
        s = 0
        for k in range(D):
            s += (x[i][k] - x[j][k]) ** 2

        if math.sqrt(s) == int(math.sqrt(s)):
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import numpy as np


N, D = map(int, input().split())
points = np.array([list(map(int, input().split())) for i in range(N)])

answer = 0
for i in range(N):
  for j in range(i+1, N):
    distance = np.linalg.norm(points[i]-points[j])
    if distance == int(distance):
      answer += 1

print(answer)

Submission status: Accepted

Solution code:
import math
N, D = [int(x) for x in input().split(' ')]

X = []
for n in range(N):
 X.append([int(x) for x in input().split(' ')])

count = 0
for i in range(N):
 for j in range(i+1, N):
  if math.sqrt(sum([(X[i][d] - X[j][d])**2 for d in range(D)])) % 1 == 0.:
          count +=1

print(count)

Submission status: Accepted

Solution code:
import sys

fin = sys.stdin.readline

n, d = map(int, fin().split())
x = [tuple(map(int, fin().split())) for i in range(n)]

count = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        d = (sum([(y - z) ** 2 for y, z in zip(x[i], x[j])]) ** 0.5)

        if abs(d - round(d)) < 1e-9:
            count += 1

print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from itertools import combinations
from math import sqrt

def solve():
    N, D = map(int, input().split())
    X = [list(map(int, input().split())) for _ in range(N)]
    res = 0
    for x,y in combinations(X,r=2):
        z = sum(map(pow, map(int.__sub__,x,y), [2]*D))
        r = sqrt(z)
        if round(r)**2 == z:
            res += 1
    return str(res)

if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
import math

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

X = []

def distance(a, b):
    x = 0
    for i in range(len(a)):
        x += (a[i] - b[i]) * (a[i] - b[i])
    y = math.sqrt(x)
    return y.is_integer()

for i in range(N):
    x = list(map(int, input().split()))
    X.append(x)

ans = 0

for i in range(N):
    for j in range(i+1, N):
        if distance(X[i], X[j]):
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N,D = map(int, input().split())
P = []
for i in range(N):
  *X, = map(int, input().split())
  P.append(np.array(X))

ans = 0
for j in range(1,N):
  for i in range(j):
    tmp = (P[i] - P[j])
    tmp2 = np.sqrt(np.dot(tmp,tmp))
    #print(tmp2, tmp2.is_integer())
    if (tmp2.is_integer()):
      ans += 1
print(ans)

Submission status: Accepted

Solution code:

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

dat = []

for i in range(n):
    dat.append(list(map(int,input().split())))

def isSquare(x):
    for i in range(x+1):
        if i**2 == x:
            return True
    return False
cnt = 0
for i in range(n):
    for j in range(i+1,n):
        s = 0
        for k in range(d):
            s+= (dat[i][k]-dat[j][k])**2
        if isSquare(s):
            cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
import math
n,d = map(int,input().split())
x = [list(map(int,input().split())) for _ in range(n)]

count = 0

for i in range(n):
    for j in range(i+1,n):
        a = 0
        for k in range(d):
            a += (x[i][k] - x[j][k])**2
        if int(int(math.sqrt(a))**2) == a:
            count += 1

print(count)

Submission status: Accepted

Solution code:
import numpy as np

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

X = [[] for _ in range(N)]

for i in range(N):
    X[i] = list(map(int,input().split()))
    
def dist(x,y):
    temp = 0
    for i in range(D):
        temp += (x[i]-y[i])**2
    
    return np.sqrt(temp)

ans = 0    

for i in range(N):
    for j in range(i+1,N):
        dic = dist(X[i],X[j])
        
        if dic%1 == 0:
            ans += 1
        
print(ans)

Submission status: Accepted

Solution code:
from math import sqrt
n,d = map(int, input().split())
X = [list(map(int, input().split())) for i in range(n)]

ans = 0
for i in range(n):
    for j in range(i,n):
        if i == j: continue
        tem = 0
        for k in range(d):
            tem += (X[i][k] - X[j][k])**2
        #print(int(sqrt(tem))**2)
        if int(sqrt(tem))**2 == tem:
            #print(i,j,tem)
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import math
N, D = map(int, input().split())

X = [[] for _ in range(N)]
for i in range(N):
    X[i] = list(map(int, input().split()))

ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        dist = 0
        for d in range(D):
            dist = dist + (X[i][d] - X[j][d]) ** 2
        dist = math.sqrt(dist)
        if dist.is_integer():
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
import math
n,d = map(int,input().split())
a=[]
for i in range(n):
    arr=list(map(int,input().split()))
    a.append(arr)
ans=0
for i in range(n-1):
    for j in range(i+1,n):
        tmp=0
        for k in range(d):
            tmp+=(a[i][k]-a[j][k])**2
        if math.sqrt(tmp)==int(math.sqrt(tmp)):
            ans+=1
print(ans)

Submission status: Accepted

Solution code:
def main():

    """ convenient functions
    # for i, a in enumerate(iterable)
    # q, mod = divmod(a, b)
    # divmod(x, y) returns the tuple (x//y, x%y)
    # Higher-order function: reduce(operator.mul, xyz_count, 1)
    # manage median(s) using two heapq https://atcoder.jp/contests/abc127/tasks/abc127_f
    """

    """convenient decorator
    # @functools.lru_cache():
    # to facilitate use of recursive function
        # ex:
        # from functools import lru_cache
        # import sys
        # sys.setrecursionlimit(10**9)
        # @lru_cache(maxsize=None)
        # def fib(n):
        #     if n < 2:
        #         return n
        #     return fib(n-1) + fib(n-2)
        # print(fib(1000))
    """
    
    # import numpy as np
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product # https://docs.python.org/ja/3/library/itertools.html
    # accumulate() returns iterator! to get list: list(accumulate())
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  # prime factor
        b, e = 2, 0  # base, exponent
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        # TODO: How should I do when n - r is negative?
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict # https://docs.python.org/ja/3/library/collections.html#collections.deque
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest # https://docs.python.org/ja/3/library/heapq.html
    from copy import deepcopy, copy # https://docs.python.org/ja/3/library/copy.html
    import operator
    from operator import itemgetter
    # ex1: List.sort(key=itemgetter(1))
    # ex2: sorted(tuples, key=itemgetter(1,2))
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd # Deprecated since version 3.5: Use math.gcd() instead.
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    # first create factorial_list
    # fac_list = mod_factorial_list(n)
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) # Recursive function in python is slow!
    def modinv(a, p = MOD):
        # evaluate reciprocal using Fermat's little theorem:
        # a**(p-1) is identical to 1 (mod p) when a and p is coprime
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        # fac_list = modfactorial_list(100)
        # print(modcomb(100, 5, modfactorial_list(100)))
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    """ initialize variables and set inputs
    # initialize variables
        # to initialize list, use [0] * n
        # to initialize two dimentional array, use [[0] * N for _ in range(N)]
    # set inputs
        # open(0).read() is a convenient method:
        # ex) n, m, *x = map(int, open(0).read().split())
        #     min(x[::2]) - max(x[1::2])
        # ex2) *x, = map(int, open(0).read().split())
        #     don't forget to add comma after *x if only one variable is used
    # preprocessing
        # transpose = [x for x in zip(*data)]
        # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
        # flat = [flatten for inner in data for flatten in inner]
        # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [1, 2, 3, 4, 5, 6, 7, 8, 9]
    # calculate and output
        # output pattern
        # ex1) print(*l) => when l = [2, 5, 6], printed 2 5 6
    """

    # functions used
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    # single int: int(r())
    # single string: r()
    # single float: float(r())
    # line int: R()
    # line string: r().split()
    # line (str, int, int): [j if i == 0 else int(j) for i, j in enumerate(r().split())]
    # lines int: [R() for _ in range(n)]
    """

    if __name__ != '__main__':
        # for test
        sys.stdin = open('sample.txt')

    # ----------------------------------
    # main
    N, D = R()
    X = [R() for _ in range(N)]
    ans = 0
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            else:
                tmp = 0
                for k in range(D):
                    tmp += (X[i][k] - X[j][k]) ** 2
                if sqrt(tmp) == floor(sqrt(tmp)):
                    ans += 1
    print(ans//2)
    
    # end of main
    # ----------------------------------

    """memo: how to use defaultdict of list
    # initialize
    Dic = defaultdict(list)
    # append / extend
    Dic[x].append(y)
    # for
    for k, v in Dic.items():
    """

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N, D = map(int, input().split())
    X = []
    for _ in range(N):
        A = tuple(map(int, input().split()))
        X.append(A)

    ans = 0
    for i in range(N-1):
        for j in range(i+1, N):
            s = 0
            for d in range(D):
                s += (X[i][d] - X[j][d])**2

            t = int(s**(0.5))
            if s == t**2:
                ans += 1

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
import numpy as np

N,D  = list(map(lambda x: int(x),input().split()))
Xs=[]
for i in range(N):
    X = list(map(lambda x: int(x),input().split()))
    Xs.append(X)
count=0
for i in range(N):
    for j in range(i+1,N):
        s = np.sum((np.array(Xs[i])-np.array(Xs[j]))**2)**0.5
        #print(s)
        if s==int(s):
            count += 1

print(count)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

X = [list(map(int, input().split())) for _ in range(N)]
ans = 0
for i in range(N):
    for j in range(N):
        if i >= j:
            pass
        else:
            distance = 0
            for x in range(D):
                distance += (X[i][x] - X[j][x]) ** 2
            distance = distance ** 0.5
            if int(distance) == distance:
                ans += 1

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N,D = map(int,input().split(' '))
X = np.array([list(map(int,input().split(' '))) for i in [0]*N])

M = int(np.sqrt(20000))
squares = [i**2 for i in range(0,M)]
ans = 0
for i in range(N):
	for j in range(i+1,N):
		a = np.sum(np.square(X[i]-X[j]))
		if a in squares:
			ans += 1

print(ans)

Submission status: Accepted

Solution code:
import math

n, d = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
ret = 0
for i in range(n):
    for j in range(i + 1, n):
        distance = sum((x[i][k] - x[j][k]) * (x[i][k] - x[j][k]) for k in range(d))
        sq = int(math.sqrt(distance))
        if distance == sq * sq:
            ret += 1
print(ret)

Submission status: Accepted

Solution code:
import itertools
n,d=map(int,input().split())
ans=0
def dist(A,B):
  ret=0
  for i in range(d):
    ret+=(A[i]-B[i])**2
  return int(ret**0.5)**2==ret
XX=[]
for i in range(n):
  X=[int(i) for i in input().split()]
  XX.append(X)

for c1,c2 in itertools.combinations(range(n),2):
  A,B=XX[c1],XX[c2]
  if dist(A,B):
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
n, D = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(n)]
res = 0
for i in range(n):
    for j in range(i + 1, n):
        d = sum((l[i][k] - l[j][k]) ** 2 for k in range(D))
        if int(d ** 0.5) ** 2 == d:
            res += 1
print(res)

Submission status: Accepted

Solution code:
import math

n,d = map(int,input().split())
x = [list(map(int,input().split())) for i in range(n)]

ans = 0
for i in range(n):
    for l in range(i+1,n):
        k = 0
        for j in range(d):
            k += (x[i][j]-x[l%n][j])**2
    #    print(math.sqrt(k))
        if math.sqrt(k)%1 == 0:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
[N,D] = list(map(int,input().split()))
listX =[]
for i in range(N):
    listX.append(list(map(int,input().split())))
output = 0

def dist(x,y):
    d2 = 0
    for i in range(D):
        d2 += (x[i]-y[i])**2
    d = d2**0.5
    return d

for i in range(N):
    for j in range(i,N):
        if i==j:
            continue
        else:
            d = dist(listX[i],listX[j])
            if d == int(d):
                output += 1

print(output)