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=map(int,input().split())
X=[np.array(input().split(),dtype=np.int64) for _ in range(N)]
ans=0
for i in range(N):
    for j in range(i+1,N):
        d=((X[i]-X[j])*(X[i]-X[j])).sum()
        dd=int(round(d**.5))
        if d==dd*dd:
            ans +=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from math import fabs
def main(inp1, inp2, X):
    FinalValue = 0
    for all in range(inp1+1):
        for iterater in range(all + 1, inp1):
            var = 0.0
            for rangeinp2 in range(inp2):
                var += fabs( (int(X[all][rangeinp2]) - int(X[iterater][rangeinp2]))**2 )
            var = var ** 0.5
            varint=int(var)
            if var==varint:
                FinalValue += 1
    return FinalValue
inp1, inp2 = map(int, input().split())
X = [input().split() for _ in range(inp1)]
print(main(inp1, inp2, X))

Submission status: Accepted

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

res = 0
for i in range(N - 1):
    for k in range(i + 1, N):
        ans = 0.0
        for j in range(D):
            ans += abs( (int(X[i][j]) - int(X[k][j]))**2 )
    
        ans = ans ** (1/2)
        if ans.is_integer():
            res += 1

print(res)

Submission status: Accepted

Solution code:

url = "https://atcoder.jp//contests/abc133/tasks/abc133_b"
import itertools
def main():
    n,d = list(map(int, input().split()))
    lis = []
    for i in range(n):
        t = list(map(int, input().split()))
        lis.append(t)
    count = 0
    for i in range(len(lis)):
        for j in range(i+1, len(lis)):
            if i == j:
                continue
            if float.is_integer(ret(lis[i], lis[j])):
                count += 1
    print(count)


import math
def ret(a,b):
    retval = 0
    idx = 0
    while True:
        if idx == len(a):
            break
        retval += abs(a[idx]-b[idx])**2
        idx += 1
    return math.sqrt(retval)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def distance2_nd(p, q):
    return sum((px - qx) ** 2 for px, qx in zip(p, q))


def isqrt(n):
    if n > 0:
        x = 1 << (n.bit_length() + 1) // 2
        y = (x + n // x) // 2
        while y < x:
            x = y
            y = (x + n // x) // 2
        return x
    elif n == 0:
        return 0
    else:
        raise ValueError("isqrt() argument must be nonnegative")


def main():
    N, D = map(int, readline().split())
    X = [list(map(int, readline().split())) for _ in range(N)]

    ans = 0
    for i in range(N):
        for j in range(i + 1, N):
            d2 = distance2_nd(X[i], X[j])
            di = isqrt(d2)
            if di * di == d2:
                ans += 1

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
def gooddistance(l1, l2):
    calc = []
    for i in range(len(l1)):
        calc.append((l1[i] - l2[i])**2)

    if math.sqrt(sum(calc)).is_integer():
        return 1
    else:
        return 0

N, D = map(int, input().split())
lis = [list(map(int, input().split())) for _ in range(N)]
cnt = 0

for i in range(N):
    for j in range(i+1,N):
       cnt += gooddistance(lis[i],lis[j])

print(cnt)

Submission status: Accepted

Solution code:
import math
n,d=map(int,input().split())
plot=[list(map(int,input().split())) for i in range(n)]
res=0
for i in range(n-1):
  for j in range(i+1,n):
    dist=0
    for k in range(d):
      dist += (plot[j][k]-plot[i][k])**2
    temp = int(math.sqrt(dist))
    res += dist == temp**2
    
print(res)

Submission status: Accepted

Solution code:
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(n):
        if i == j: continue
        counter = 0
        for k in range(d):
            diff = abs(x[i][k] - x[j][k])
            counter += diff ** 2
        ok = False
        for k in range(10000):
            if k * k == counter: ok = True
        if ok: ans += 1
print(ans // 2)

Submission status: Accepted

Solution code:
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(i+1,n):
    sum=0
    for k in range(d):
      sum+=(x[i][k]-x[j][k])**2
    z=pow(sum,0.5)
    if math.ceil(z)==math.floor(z):
      cnt+=1
print(cnt)

Submission status: Accepted

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

def dstnc(X, Y):
    res = 0
    for l, x in enumerate(X):
        res += (x - Y[l])**2
    return res


ans = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        su = dstnc(A[i], A[j])**0.5
        if str(su)[-1] == "0" and str(su)[-2] == ".":
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
n, d = map(int, input().split())
l = []
for i in range(n):
    l.append(list(map(int, input().split())))
count = 0
import math
for i in range(n):
    for j in range(i + 1, n):
        sum  = 0
        if i != j:
            for k in range(d):
                sum += (l[i][k] -l[j][k]) ** 2
            dis = math.sqrt(sum)
            if dis % 1 == 0:
                count += 1
print(count)

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
cal = 0
for i in range(n):
  for j in range(i+1, n):
    for k in range(d):
      de = abs(x[i][k] - x[j][k])
      cal += de**2
    if math.sqrt(cal)%1==0:
      ans += 1
    cal = 0
print(ans)

Submission status: Accepted

Solution code:
import math
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):
        total = 0
        for k in range(D):
            total += (X[i][k]-X[j][k])**2
        total = math.sqrt(total)
        if total%1 == 0:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
N,D=map(int,input().split())
X=[[0]*D for i in range(N)]
for i in range(N):
  X[i]=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])
      Norm+=Diff*Diff
    flag=False
    for k in range(Norm+1):
      if k*k==Norm:
        flag=True
    if flag==True:
      ans+=1
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N, D = map(int,input().split())
LX = [list(map(int, input().split())) for i in range(N)]

cnt=0
for i in range(N-1):
    for j in range(i+1,N):
        LXP = np.array(LX[i])
        LXQ = np.array(LX[j])
        ans = (float(np.sqrt(sum((LXP - LXQ) ** 2))))
        if ans.is_integer():
            cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
from itertools import combinations
N,D=map(int,input().split())
X=[list(map(int,input().split())) for i in range(N)]
count=0
for x,y in combinations(X,2):
    r = np.sqrt(sum(abs(np.array(x)-np.array(y))**2))
    if r.is_integer() == True:
        count += 1
print(count)

Submission status: Accepted

Solution code:
import numpy as np
from itertools import combinations
 
n, d = map(int, input().split())
x = np.empty((n, d), dtype=np.int64)
for i in range(n):
    x[i] = [int(i) for i in input().split()] 
res = 0
for i, j in combinations(range(n), 2):
    if (np.sum((x[i]-x[j])**2)**0.5).is_integer():
        res += 1
print(res)

Submission status: Accepted

Solution code:
import numpy as np
from itertools import combinations

n, d = map(int, input().split())
x = np.empty((n, d), dtype=np.int64)
for i in range(n):
    x[i] = [int(i) for i in input().split()]

res = 0
for i, j in combinations(range(n), 2):
    if (np.sum((x[i]-x[j])**2)**0.5).is_integer():
        res += 1
print(res)

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):
    tmp = 0.0
    for k in range(d):
      tmp += (X[j][k] - X[i][k]) ** 2
    tmp = math.sqrt(tmp)
    if tmp.is_integer():
      count += 1
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    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(n):
            dis2 = 0
            if i != j:
                for k in range(d):
                    dis2 += (x[i][k] - x[j][k]) ** 2
                if (dis2 ** 0.5).is_integer():
                    count += 1
    print(count//2)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

def f(A,B):
  D = 0
  for i in range(d):
    D += (A[i]-B[i])**2
  if math.sqrt(D) == math.floor(math.sqrt(D)):
    return(1)
  else:
    return(0)
ans = 0  
for i in range(n-1):
  for j in range(i+1,n):
    ans += f(X[i],X[j])
 
print(ans)

Submission status: Accepted

Solution code:
import math
n,d=map(int,input().split())
l=[]
x=[]
c=0
for i in range(n):
    l.append(list(map(int,input().split())))
    
for i in range(n-1):
    for j in range(i+1,n):
        for k in range(d):
            x.append(abs(l[i][k]-l[j][k])**2)
        if math.sqrt(sum(x))%1==0:
            c+=1
        x=[]
print(c)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[7]:


import math


# In[13]:


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


# In[14]:


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)


# In[ ]:

Submission status: Accepted

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

# 座標リストLを作成
for i in range(N):
    X = list(map(int,input().split()))
    L.append(X)

# 座標リストを1から順々に最後の座標までaに入れつつループ 
for a in range(0,N-1):  
    #print(L[a])
    for b in range(a+1,N): #a+1番目から最後までの座標を順にループ
       # print(" " + str(L[b]))
        f = 0
        s = 0
        for i,j in zip(L[a],L[b]): #対応要素毎にループ
            f = f + (i-j)**2  
            #print("  " +str(i)+ ";"+str(j)+";;"+str(f))   
        f = f **0.5 #それぞれの２乗を足したものをルートをとる
        #print(f)
        P = P + f.is_integer()  #fが整数ならばPを１増やす　違えば増やさない
print(P)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

#a = int(input())
#b = list(map(int, input().split()))
n, d = map(int, input().split())
#s = input()
#s,t = input().split()
#
readline = sys.stdin.readline
#n,m = [int(i) for i in readline().split()]
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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): # root
		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): # much time
		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()} # 1~n
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): #j:aの個数
			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 = [2]
	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 = [x for x in data if x!=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)

Submission status: Accepted

Solution code:
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))
    #print(s)
    if s.is_integer():
      cnt+=1

    else:
      continue
print(cnt)

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

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

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

Submission status: Accepted

Solution code:
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()

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)
            #print("d", dist)
            if dist.is_integer():
                #print(a, b)
                count += 1
    print(count)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)