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

Solution (Python):
from collections import deque


def cal_distance(a_list, b_list):
    total = 0
    for i in range(len(a_list)):
        total += (a_list[i] - b_list[i]) ** 2
    
    return total**0.5


if __name__ == "__main__":
    N, D = map(int, input().split())

    X = deque()
    for _ in range(N):
        temp = list(map(int, input().split()))
        X.append(temp)

    total = 0
    while X:
        x = X.popleft()
        for temp in X:
            distance = cal_distance(x, temp)
            if distance.is_integer():
                total += 1
                
    print(total)

Solution (Python):
import numpy as np
def dist(y, z):
    y = np.array(y)
    z = np.array(z)
    return (sum((y - z) **2)) ** (0.5)

def is_integer(n):
    try:
        float(n)
    except ValueError:
        return False
    else:
        return float(n).is_integer()

def resolve():
    N, D = list(map(int, input().split()))
    X = []
    for _ 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 is_integer(dist(X[i], X[j])):
                ans+=1
    print(ans)
    return
resolve()

Solution (Python):






import math





N,D = map(int, input().split())
X = []
for _ 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):
        dist = sum((X[i][k]-X[j][k])**2 for k in range(D))
        if math.sqrt(dist)%1 == 0:
            ans += 1
print(ans)

Solution (Python):
P=0
L=[]
N,D = map(int,input().split())


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


for a in range(0,N-1):  
    
    for b in range(a+1,N): 
       
        f = 0
        s = 0
        for i,j in zip(L[a],L[b]): 
            f = f + (i-j)**2  
            
        f = f **0.5 
        
        P = P + f.is_integer()  
print(P)

Solution (Python):
import math

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(1, N):
    for j in range(i):
        d = 0
        for k in range(D):
            d += (X[i][k] - X[j][k])**2
        for l in range(d+1):
            if l**2 == d:
                cnt += 1
                break
print(cnt)

Solution (Python):
n, d=map(int, input().split())
l=[]
cnt=0
for _ in range(n):
  l.append(list(map(int, input().split())))
for i in range(n-1):
  for j in range(i+1, n):
    calc=0
    for h in range(d):
      calc+=(l[i][h]-l[j][h])**2
    if int(calc**0.5)==calc**0.5:
      cnt+=1
print(cnt)

Solution (Python):
import itertools

n,d = map(int,input().split())
x = [ list(map(int,input().split(" "))) for i in range(n)]
ns = [i for i in range(n)]
ans = 0
for v in itertools.combinations(ns, 2):
    k = 0
    for i in range(d):
        k += (x[v[0]][i] - x[v[1]][i]) ** 2
    if k ** 0.5 % 1 == 0:
        ans += 1
print(ans)

Solution (Python):
import math
n, d = map(int, input().split())
p = []
cnt = 0
for i1 in range(n):
    p.append(list(map(int, input().split())))

def check(l1, l2, d1):
    v = 0
    for k in range(d1):
        v += (l1[k] - l2[k]) ** 2
    if float.is_integer(math.sqrt(v)):
        return True
    else:
        return False

for j1, j2 in enumerate(p[:n]):
    for j3 in p[j1+1:]:
        if check(j2, j3, d):
            cnt += 1
print(cnt)

Solution (Python):
import math
n, d = map(int, input().split())
p = []
cnt = 0
for i1 in range(n):
    p.append(list(map(int, input().split())))

def check(l1, l2, d1):
    v = 0
    for k in range(d1):
        v += (l1[k] - l2[k]) ** 2
    if float.is_integer(math.sqrt(v)):
        return True
    else:
        return False

for j1, j2 in enumerate(p[:n]):
    for j3 in p[j1+1:]:
        if check(j2, j3, d):
            cnt += 1
print(cnt)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)



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



readline = sys.stdin.readline

x = [[int(i) for i in readline().split()] for _ in range(n)]

import itertools
import math

ans = 0

for v in itertools.combinations(x, 2):
    dist = math.sqrt(sum([(i-j)**2 for i,j in zip(v[0], v[1])]))
    if dist == int(dist):
        ans += 1
        
print(ans)

Solution (Python):
import math
N,D=map(int,input().split())
L=[]
for i in range(N):
    A=list(map(int,input().split()))
    L.append(A)

ans=0
for i in range(1,N):
    for j in range(i+1,N+1):
        dis=0
        for k in range(D):
            dis+=(L[i-1][k]-L[j-1][k])**2
        if math.sqrt(dis).is_integer():
            ans+=1
            
print(ans)

Solution (Python):
def d2(d,p1,p2):
  return sum([(p2[i]-p1[i])**2 for i in range(d)])
def judge(x):
  return int(x) == int(x**(1/2))**2

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

l = [list(map(int,input().split())) for _ in range(N)]
s = 0
for i in range(N):
  for j in range(i+1,N):
    s += judge(d2(D,l[i],l[j]))
    
print(s)

Solution (Python):
import itertools
N, D = map(int, input().split())
count = 0

X = []
for i in range(N):
    X.append(list(map(int, input().split())))
for i in list(itertools.combinations(X, 2)):
    f = 0
    for j in range(D):
        f += (i[0][j]-i[1][j])**2
    f **= 0.5
    if f.is_integer():
        count += 1
print(count)

Solution (Python):
def main():
    n, d = map(int, input().split())
    x = [list(map(int, input().split())) for i in range(n)]
    res = 0
    cnt = 0
    for i in range(n):
        for j in range(n):
            if i < j:
                for k in range(d):
                    res += abs(x[i][k] - x[j][k])**2
                ans = res ** (1/2)
                if ans.is_integer():
                    cnt += 1
                res = 0
    print(cnt)
    
if __name__ == '__main__':
    main()

Solution (Python):
import math
from collections import deque
from collections import defaultdict

import itertools as it


def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def combmod(n,r,mod):
	return (fact(n)/fact(n-r)*pow(fact(r),mod-2,mod))%mod
def printQueue(q):
	r=copyQueue(q)
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): 
		if self.parents[x]<0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	def union(self,x,y):
		x = self.find(x)
		y = self.find(y)

		if x==y:
			return

		if self.parents[x]>self.parents[y]:
			x,y = y,x

		self.parents[x]+=self.parents[y]
		self.parents[y]=x

	def size(self,x):
		return -1*self.parents[self.find(x)]

	def same(self,x,y):
		return self.find(x)==self.find(y)

	def members(self,x): 
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i)==root]

	def roots(self):
		return [i for i,x in enumerate(self.parents) if x<0]

	def group_count(self):
		return len(self.roots())

	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()} 
def bitArr(n):
	x = 1
	zero = "0"*n
	ans = []
	ans.append([0]*n)
	for i in range(2**n-1):
		ans.append(list(map(lambda x:int(x),list((zero+bin(x)[2:])[-1*n:]))))
		x+=1
	return ans;
def arrsSum(a1,a2):
	for i in range(len(a1)):
		a1[i]+=a2[i]
	return a1
def maxValue(a,b,v):
	v2 = v
	for i in range(v2,-1,-1):
		for j in range(v2//a+1): 
			k = i-a*j
			if k%b==0:
				return i
	return -1
def copyQueue(q):
	nq = queue.Queue()
	n = q.qsize()
	for i in range(n):
		x = q.get()
		q.put(x)
		nq.put(x)
	return nq
def get_sieve_of_eratosthenes(n):
	
	data = [0,0,0]
	for i in range(3,n+1,2):
		data.append(i)
		data.append(0)
	for i in range(len(data)):
		interval = data[i]
		if interval!=0:
			for j in range(i+interval,n-1,interval):
				data[j] = 0
	
	ans = data[:]
	return ans

ans = 0

n,d = readInts()
x = []
for i in range(n):
	x.append(readInts())

for i in list(it.combinations(range(0,n),2)):
	tmp = 0
	for j in range(d):
		tmp+=(x[i[0]][j]-x[i[1]][j])**2
	if tmp==(int(tmp**0.5))**2:
		ans+=1

print(ans)

Solution (Python):
import math
n,d=map(int,input().split())
x=[]
for i in range(n):
  x_=list(map(int,input().split()))
  x.append(x_)
cnt=0
for i in range(n):
  for j in range(i+1,n):
    sumx=0
    for k in range(d):
      sumx+=abs(x[i][k]-x[j][k])**2

    s=float(math.sqrt(sumx))
    
    if s.is_integer():
      cnt+=1

    else:
      continue
print(cnt)

Solution (Python):
from math import sqrt
n, d = map(int, input().split())
x = [list(map(int, input().split())) for i in range(n)]
ans = 0
for j in range(n):
    for i in range(j):
        xi = x[i]
        xj = x[j]
        m = 0
        for k in range(d):
            m += abs(xi[k] - xj[k])**2
        ans += sqrt(m).is_integer()
print(ans)

Solution (Python):
import math

n,d=map(int,input().split())
x=[list(map(int,input().split())) for i in range(n)]
cnt=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
        if math.sqrt(dis)%1==0:
            cnt+=1
print(cnt)

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

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

P = []
for i in range(N):
    for j in range(i+1, N):
        S = 0
        for k in range(D):
            S += (A[i][k] - A[j][k])**2
        P.append(S)

M = int(max(P)**0.5)
ans = 0

for i in range(1, M+1):
    for j in P:
        if i == j**0.5:
            ans += 1
            
print(ans)

Solution (Python):
import math
def dist(a,b):
  ret=0
  for (x,y)in zip(a,b):
    ret+=pow(x-y,2)
  return math.sqrt(ret)
point=[]
n,d=map(int,input().split())
for i in range(n):
  point.append(list(map(int,input().split())))
ans=0
for i in point:
  for j in point:
    if i==j:
      continue
    if dist(i,j)%1==0:
      ans+=1
print(int(ans/2))

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

Solution (Python):
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,1):
    r2=0
    for k in range(D):
      r2+=(X[i][k]-X[j][k])*(X[i][k]-X[j][k])
    r=math.sqrt(r2)
    if r.is_integer()==True:
      count+=1
print(count)

Solution (Python):
import sys
import itertools
import math
input = lambda: sys.stdin.readline().rstrip()


def dist(D, x, y):

    ret = 0
    for i in range(D):
        ret += pow(x[i] - y[i], 2)

    return (ret ** 0.5).is_integer()


def solve():
    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
    comb = list(itertools.combinations(range(N), 2))
    for c in comb:
        i1, i2 = c[0], c[1]
        if dist(D, x[i1], x[i2]):
            ans += 1

    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
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):
        norm = 0
        for k in range(d):
            diff = abs(X[i][k] - X[j][k]) ** 2
            norm += diff
        for k in range(1, norm + 1):
            if k ** 2 == norm:
                ans += 1
                continue
print(ans)

Solution (Python):
import math
def f(x,y):
  s = 0
  for i in range(len(x)):
    s += (x[i]-y[i]) ** 2
  return math.sqrt(s)

n,d = map(int, input().split())
X = [list(map(int, input().split())) for i in range(n)]
ans = 0
for i in range(n-1):
  for j in range(i+1,n):
    if f(X[i],X[j]) == math.floor(f(X[i],X[j])):
      ans += 1
 
print(ans)

Solution (Python):
import itertools

def issquare(x):
    a = 1
    while x >= a*a:
        if x == a*a:
            return True
        a += 1
    return False

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

ans = 0
for pi,pj in itertools.combinations(ps, 2):
    d = sum([(x-y)*(x-y) for x,y in zip(pi,pj)])
    if issquare(d):
        ans += 1
print(ans)

Solution (Python):
import math

def main():
    n,d=map(int,input().split())
    x=[]
    for _ in range(n):
        x.append(list(map(int,input().split())))
    count = 0
    for ai in range(len(x)-1):
        for bi in range(ai+1, len(x)):
            a = x[ai]
            b = x[bi]
            dist = 0
            for i, v in enumerate(a):
                dist += (v - b[i])**2
            dist = math.sqrt(dist)
            
            if dist.is_integer():
                
                count += 1
    print(count)
    
if __name__ == "__main__":
    main()

Solution (Python):
n,d=[int(x) for x in input().rstrip().split()]
l=[]
for i in range(n):
  x=[int(i) for i in input().rstrip().split()]
  l.append(x)

ans=0
for i in range(n):
  ni=l[i]
  for j in range(i+1,n):
    nj=l[j]
    now=0
    for k in range(d):
      now+=(ni[k]-nj[k])**2
    now=now**0.5
    if now.is_integer():
      ans+=1
print(ans)

Solution (Python):
import math
import itertools
k=0
s=0
R=list()
N,M=map(int,input().split())
for i in range(N):
  L=list(map(int,input().split()))
  R.append(L)
list1 = list(itertools.permutations(R, 2))
for i in range(len(list1)):
  o=list1[i]
  A=list(o)[0]
  B=list(o)[1]
  for j in range(M):
    s+=(A[j]-B[j])**2
  d = int(math.sqrt(s))
  if pow(d,2)==s:
    k+=1
  s=0
print(k//2)

Solution (Python):
n,d = map(int,input().split())
l = []
for i in range(n):
    l.append([int(x) for x in input().split()])

import math
def calc(x1,x2):
    
    l1 = l[x1]
    l2 = l[x2]
    s = 0
    for i in range(d):
        s += (l1[i]-l2[i])**2
    s = math.sqrt(s)
    if(s.is_integer()):
        return 1
    return 0


ans = 0
for i in range(n):
    for j in range(i,n):
        if(i==j):
            continue
        ans += calc(i,j)
print(ans)

Solution (Python):
N, D = map(int, input().split())
X = []
count = 0
for n in range(N):
  temp = list(map(int, input().split()))
  X.append(temp)
for i in range(N):
  for j in range(i + 1, N):
    sums = 0
    for d in range(D):
      sums += (X[i][d] - X[j][d]) ** 2
    for n in range(sums):
      if n * n == sums or sums == 1:
        count += 1
print(count)

Solution (Python):
import math
N,D=map(int,input().split())
X=[list(map(int,input().split()))for i in range(N)]
ans=0
dist=0.0
for i in range(N):
    for j in range(i+1,N):
        dist=0.0
        for k in range(D):
            dist+=(X[i][k]-X[j][k])**2
        dist=math.sqrt(dist)
        if dist.is_integer() is True:
            ans+=1
print(ans)

Solution (Python):
import numpy as np

_N, _D, *_X = open(0).read().split()
N = int(_N)
D = int(_D)
X = np.array(list(zip(*[map(int, iter(_X))]*D)))

import itertools

ans = 0

for x, y in itertools.combinations(X, 2):
    d = np.linalg.norm(x - y)

    if d%1 == 0:
        ans += 1

print(ans)

Solution (Python):
from math import sqrt 

def is_integer(n):
    try:
        float(n)
    except ValueError:
        return False
    else:
        return float(n).is_integer()

n,d =map(int,input().split())
x=[list(map(int,input().split())) for i in range(n)]
cnt=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
    if is_integer(sqrt(dis)):
      cnt+=1
print(cnt)

Solution (Python):
import math
n, d = map(int,input().split())
x = [list(map(int,input().split())) for _ in range(n)]
cnt = 0
for i in range(n):
    for j in range(n):
        if i <= j:
            continue
        temp = 0
        for k  in range(d):
            temp += abs(x[i][k]-x[j][k]) ** 2
        dist = math.sqrt(temp)
        if dist == int(dist):
            cnt += 1
print(cnt)

Solution (Python):
import math

N,D=map(int,input().split())
S = [[0 for j in range(D)] for i in range(N)]

for i in range(N):
    Z=list(map(int,input().split()))
    for j in range(D):
        S[i][j]=Z[j]
DC=0
goukei=0
root=0
count=0
for i in range(N):
    for j in range(i+1,N):
        goukei=0
        DC=0
        while DC<D:
            goukei+=abs(S[i][DC]-S[j][DC])**2
            DC+=1
            
        root=math.sqrt(goukei)
        if root.is_integer() ==True:
            count+=1
print(count)

Solution (Python):
import itertools

from math import sqrt


def actual(n, d, X):
    count = 0

    for p1, p2 in itertools.combinations(X, 2):
        d_float = sqrt(sum([(y - z) ** 2 for (y, z) in zip(p1, p2)]))

        if d_float % 1 == 0.0:
            count += 1

    return count

n, d = map(int, input().split())
X = []
for _ in range(n):
	X.append(list(map(int, input().split())))

print(actual(n, d, X))

Solution (Python):
import math
N,D=map(int,input().split())
X=[]
for i in range(N):
    x=list(map(int,input().split()))
    X.append(x)
res=0
for i in range(N-1):
    for j in range(i+1,N):
        dist=0
        for d in range(D):
            dist+=(X[i][d]-X[j][d])**2
        dist=math.sqrt(dist)
        if dist==int(dist):
            res+=1
print(res)

Solution (Python):
import math
n, d = map(int, input().split())
arr = []
ans = 0
for i in range(n):
  x = list(map(int, input().split()))
  arr.append(x)

p = []
for i in range(n):
  for j in range(n):
    if i < j:
      p.append([i, j])

for ij in p:
  y = arr[ij[0]]
  z = arr[ij[1]]
  d = 0
  for i, j in zip(y, z):
    d += (i - j) ** 2 
  d = math.sqrt(d)
  if d == int(d):
    ans += 1
print(ans)

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

Solution (Python):
import math
N, D = map(int, input().split())
xd = [list(map(int, input().split())) for _ in range(N)]
ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        t = 0
        for k in range(D):
            t += pow(xd[i][k]-xd[j][k], 2)
        t = math.sqrt(t)
        if t == float(int(t)):
            ans += 1
print(ans)

Solution (Python):
def resolve():
    import math
    N, D = map(int, input().split())
    A = []
    count = 0
    for i in range(N):
        A.append(list(map(int, input().split())))
    for j in range(N):
        for g in range(N):
            ans = 0
            if j != g:
                for k in range(D):
                    ans += (A[j][k]-A[g][k])**2
                if math.sqrt(ans)%1== 0:
                    count += 1
    print(count//2)
resolve()

Solution (Python):
def resolve():
    import math
    N, D = map(int, input().split())
    A = []
    count = 0
    for _ in range(N):
        A.append(list(map(int, input().split())))
    for i in range(N):
        for j in range(i+1, N):
            if j != i:
                ans=0
                for g in range(D):
                    ans += (A[i][g]-A[j][g])**2
                if math.sqrt(ans)%1 == 0:
                    count += 1
    print(count)
resolve()

Solution (Python):
import itertools
import math

n, d = map(int, input().split())
l = []
for _ in range(n):
    l.append(list(map(int, input().split())))

ll = [i for i in range(n)]
count = 0
for a1, a2 in itertools.combinations(ll, 2):
    
    
    
    sum_v = 0
    for i in range(d):
        sum_v += (l[a1][i] - l[a2][i]) ** 2
        
    dist = math.sqrt(sum_v)
    if dist == int(dist):
        
        count += 1
        
print(count)

Solution (Python):
import math
def inp():return map(int,input().split())
def linp():return list(map(int,input().split()))

n,d=inp()
distance=[]
coordinates=[]
for i in range(n):
    coordinates.append(linp())

ans=0
for i in range(n-1):
    for k in range(i+1,n):
        s=0
        for j in range(d):
            s+=abs((coordinates[i][j]-coordinates[k][j])**2)
        if math.sqrt(s)==int(math.sqrt(s)):ans+=1
print(ans)

Solution (Python):
n,d = map(int, raw_input().split(' '))
elems = [map(int,raw_input().split(' ')) for _ in range(n)]

def f(x):
	q = 1
	while(q * q <= x):
		if q **2 == x: return True
		q +=1
	return False
count = 0
for i in range(len(elems)):
	for j in range(i+1, len(elems)):
		if f(sum([(abs(a-b) ** 2) for a, b in zip(elems[i],elems[j])])):
			count +=1
print count

Solution (Python):
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):
        dis=0
        for k in range(d):
            dis+=(x[i][k]-x[j][k])**2
        r=math.sqrt(dis)
        if r.is_integer():
            ans+=1
print(ans)

Solution (Python):
n,d=map(int, input().split())
xd = [list(map(int, input().split())) for _ in range(n)]
import math
ans = 0
for i in range(len(xd)):
    for j in range(len(xd)):
        d = 0
        if i == j:
            continue
        for k in range(len(xd[i])):
            d += abs(xd[i][k]-xd[j][k])**2
        if math.sqrt(d).is_integer():
            ans += 1
print(int(ans/2))

Solution (Python):
import math
import itertools



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

X = list()

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

ans_count = int()

if d > 1:
    for v in itertools.combinations(X, 2):
        dist = int()
        for num in range(d):
            dist += (v[0][num] - v[1][num])**2
        ans_dist = math.sqrt(dist)
        if ans_dist.is_integer():
            ans_count += 1
    print(ans_count)
    
else:
    for v in itertools.combinations(X, 2):
        dist = int()
        dist = (v[0][0]- v[1][0])**2
        ans_dist = math.sqrt(dist)
        if ans_dist.is_integer():
            ans_count += 1
    print(ans_count)

Solution (Python):
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 + 1, N):
        dis = 0
        for k in range(D):
            dis += (X[i][k] - X[j][k]) ** 2
        r = sqrt(dis)
        if r == int(r):
            ans += 1
print(ans)

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

cnt = 0
for i in range(n-1):
    for j in range(i+1,n):
        t = 0
        for k in range(d):
            t += (abs(lis[i][k] - lis[j][k]))**2
            
        if (t**0.5).is_integer():
            cnt += 1
            
print(cnt)

Solution (Python):
n,d=map(int,input().split())
a=[]
for i in range(n):
    a.append([int(i) for i in input().split()])
ans=0

for i in range(n-1):
    for j in range(i+1,n):
        x=a[i]
        y=a[j]
        kyori=[(x[k]-y[k])**2 for k in range(d)]
        if sum(kyori)**(0.5)==int(sum(kyori)**(0.5)):
            ans+=1
print(ans)

Solution (Python):
import math
n, d  = map(int, input().split())
ll = []
count = 0
for i in range(n):
    ll.append(list(map(int, input().split())))
for i in range(n):
    for j in range(i+1,n):
        dd = 0
        for k in range(d):
            dd += (ll[i][k]-ll[j][k])**2
            s = math.sqrt(float(dd))
        if s == int(s):
            count+=1
print(count)

Solution (Python):
def main():
    N, D = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(N)]    
    cnt = 0
    for i in range(len(A)-1):
        for j in range(i+1, len(A)):
            a1 = A[i]
            a2 = A[j]
            dist = 0
            for x, y in zip(a1, a2):
                dist += (x-y)**2
            dist_sqr = dist**0.5
            if dist_sqr == int(dist_sqr):
                cnt += 1
    print(cnt)


if __name__ == '__main__':
    main()

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

def main():
    n, d = map(int, input().split())
    X=[]
    count = 0
    sum = 0
    for i in range(n):
        x = list(map(int, input().split()))
        X.append(x)
    for i in range(n-1):
        for v in range(i+1,n):
            for u in range(d):
                sum += (X[i][u] - X[v][u])**2
            len = math.sqrt(sum)
            sum = 0
            if len == int(len):
                count += 1
    print(count)


main()

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

ans = 0

for i, x in enumerate(X):
    for y in X[i+1:]:
        d = 0
        for i in range(D):
            d += (x[i] - y[i]) ** 2
        
        j = 0
        while True:
            if j ** 2 == d:
                ans += 1
                break
            elif j ** 2 > d:
                break
            j += 1
print(ans)

Solution (Python):
n,d=map(int,input().split())
x = []
nijou = set([i**2 for i in range(10000)])
for i in range(n):
	x.append([int(i) for i in input().split()])
ans = 0
for i in range(n-1):
	for j in range(i+1, n):
		if sum([(x[i][k] - x[j][k])**2 for k in range(d)]) in nijou:
			ans += 1
print(ans)

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, d = map(int, input().split())
    x = [list(map(int, input().split())) for _ in range(n)]
    ans = 0
    for i in range(n - 1):
        for j in range(i + 1, n):
            dd = sum([(x[i][k] - x[j][k]) ** 2 for k in range(d)])
            if dd == int(dd ** 0.5) ** 2:
                ans += 1
    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

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

lis=[False for i in range(16001)]
i=0
while(i*i<16000):
  lis[i*i]=True
  i+=1
  
ans=0
for i in range(n):
  for j in range(i):
    dis=sum([(x[i][k]-x[j][k])*(x[i][k]-x[j][k]) for k in range(d)])
 
    if lis[dis]:
      ans += 1
print(ans)

Solution (Python):
from math import sqrt
from itertools import combinations
n,d=map(int,input().split())
ct=0
list1=[[0 for i in range(d)]for j in range(n)]
list2=list(range(n))
pair=list(combinations(list2,2))
for i in range(n):
    list1[i]=list(map(int,input().split()))
for i in range(len(pair)):
    ans=0
    for j in range(d):
        ans+=(list1[pair[i][0]][j]-list1[pair[i][1]][j])**2
    if int(sqrt(ans))-sqrt(ans)==0:
        ct+=1
print(ct)

Solution (Python):
from itertools import combinations
import math
a,b=map(int,input().split())
li=[list(map(int,input().split())) for i in range(a) ]
ln=list(combinations(range(a),2))
ans=[]
l=0
for i in range(len(ln)):
    for j in range(b):
        l+=(li[ln[i][0]][j]-li[ln[i][1]][j])**2
    l3=math.sqrt(l)
    ans.append(l3)
    l=0

answe=0
for i in ans:
    if i.is_integer():
       answe+=1 
print(answe)

Solution (Python):
N, D = list(map(int, input().split()))
point = []
for _ in range(N):
    point += [list(map(int, input().split()))]

count = 0
for i in range(N):
    for j in range(i + 1, N):
        distance = sum([(y - z) ** 2 for y, z in zip(point[i], point[j])]) ** .5
        
        if abs(distance - int(distance)) < 0.0001:
            count += 1
print(count)

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


X = [list(map(int,input().split(" "))) for i in range(N)]
import math


def diff(lis1,lis2):
	l = 0
	
	for i in range(len(lis1)):
		l += abs(lis1[i] - lis2[i])**2
	return math.sqrt(l)


cnt = 0
for i in range(N):
	for j in range(i+1,N):
		if diff(X[i],X[j]).is_integer():
			cnt += 1
		

print(cnt)

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

import itertools,math
a = [i for i in range(n)]
e = itertools.combinations(a,2)

ans = 0
for i in e:
  c = 0
  for j in range(d):
    c += (x[i[0]][j] - x[i[1]][j]) ** 2
  if math.sqrt(c).is_integer():
    ans += 1

print(ans)

Solution (Python):
N, D = map(int,input().split())
X = [list(map(int,input().split())) for _ in range(N)]
a = 0
for i in range(1,N):
    for j in X[i:]:
        W = 0
        for d in range(D):
            W +=(X[i-1][d]-j[d])**2
        if W**(1/2)== int(W**(1/2)):
            a +=1

print(a)

Solution (Python):
import math

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

count = 0

for i in range(N-1):
    for j in range(i+1, N):

        sum = 0
        for d in range(D):
            sum += (X[i][d] - X[j][d]) ** 2

        if int(math.sqrt(sum)) ** 2 == sum:
            count += 1
            
print(count)

Solution (Python):
import math
X= []

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

f = 0
leng = 0

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