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:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from math import sqrt

def rl(proc=None):
    if proc is not None:
        return proc(sys.stdin.readline())
    else:
        return sys.stdin.readline().rstrip()

def srl(proc=None):
    if proc is not None:
        return list(map(proc, rl().split()))
    else:
        return rl().split()

def dist(a, b):
    r = 0
    for i in range(len(a)):
        d = a[i] - b[i]
        r += d * d
    return r

def issq(d):
    c = int(sqrt(d))
    return c * c == d or (c+1) * (c+1) == d

def main():
    N, D = srl(int)
    A = []
    for _ in range(N):
        A.append(srl(int))
    c = 0
    for i in range(N):
        for j in range(i+1, N):
            if issq(dist(A[i], A[j])):
                c += 1
    print(c)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
R = lambda: map(int, raw_input().split())

n, d = R()
a = [R() for _ in xrange(n)]
ans = 0
for i in xrange(n):
    for j in xrange(i):
        dis = sum((a[i][k] - a[j][k]) ** 2 for k in xrange(d))
        ans += int(dis ** 0.5 + 0.1) ** 2 == dis


print ans

Submission status: Accepted

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


def len(i,j):
    foo = 0
    for n in range(D):
        foo += (X[i][n]-X[j][n])**2
    return foo
ans = 0
for i in range(N):
    for j in range(i+1,N):
        len2 = len(i,j)
        if int(len2**0.5)**2 == len2:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

# N, A, B = list(map(int, sys.stdin.readline().split()))
# print(min(A*N, B))


N, D = list(map(int, sys.stdin.readline().split()))
X = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]


ans = 0
for i, j in itertools.combinations(range(N), r=2):
    a = X[i]
    b = X[j]
    r = 0
    for aa, ba in zip(a, b):
        r += (aa - ba) ** 2
    ans += int(math.sqrt(r)) ** 2 == r
print(ans)

Submission status: Accepted

Solution code:
n,d=[int(i) for i in input().split()]
x=[[int(i) for i in input().split()] for _ in range(n)]
ans=0
for i in range(n):
    for j in range(n):
        if i>j:
            dis=0
            for h in range(d):
                dis+=(x[j][h]-x[i][h])**2
            if (dis**0.5).is_integer():
                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)]
import math
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]) * (X[i][k] - X[j][k])
        tmp = math.sqrt(tmp)
        if float.is_integer(tmp):
            ans += 1
            
print(ans)

Submission status: Accepted

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

ans = 0
for i,a in enumerate(X[:-1]):
    for b in X[i+1:]:
        d = 0
        for x,y in zip(a,b):
            d += (x-y)**2
        if d**0.5 - int(d**0.5) < 10e-10:
            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)]
def dist(a,b):
    return (sum([(a[i]-b[i])**2 for i in range(D)]))**0.5
cnt=0
for i in range(N):
    for j in range(i+1,N):
        if dist(X[i],X[j])==int(dist(X[i],X[j])):
            cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
N,D=list(map(int,input().strip().split(' ')))
points=[]
for _ in range(N):
    P=list(map(int,input().strip().split(' ')))
    points+=[P]

count=0
for i in range(len(points)):
    for j in range(i+1,len(points)):
        temp=sum([(points[i][k]-points[j][k])**2 for k in range(D)])
        root=int(temp**0.5)
        if (root-1)**2==temp or root**2==temp or (root+1)**2==temp:
            count+=1
print(count)

Submission status: Accepted

Solution code:
from functools import reduce
import math

MIN = -10 ** 9
MAX = 10 ** 9


def main():
    N, D = [int(a) for a in input().split()]
    X = [
        [int(a) for a in input().split()]
        for _ in range(N)
    ]

    c = 0

    for i in range(N):
        for j in range(i+1,N):
            t = 0
            for k in range(D):
                aaa = X[i][k] - X[j][k]
                t += aaa * aaa
            t = math.sqrt(t)
            if t - int(t) == 0:
                c += 1
    print(c)


if __name__ == '__main__':
    main()

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

Submission status: Accepted

Solution code:
n,d=map(int,input().split())
a=[list(map(int,input().split()))for i in range(n)]
t=0
for i in range(n-1):
    for j in range(i+1,n):
        s=0
        for h in range(d):
            s+=(a[i][h]-a[j][h])**2
        else:
            if s==int(s**0.5)**2:
                t+=1
print(t)

Submission status: Accepted

Solution code:
from operator import sub


def calc(a, b):
    r = sum(c ** 2 for c in map(sub, a, b)) ** (1 / 2)
    return r % 1 == 0


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

cnt = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        if calc(x[i], x[j]):
            cnt += 1
print(cnt)

Submission status: Accepted

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

count = 0
for i, xi in enumerate(X):
    for j, xj in enumerate(X):
        if i >= j:
            continue
        t = 0
        for k in range(D):
            t += (xi[k] - xj[k]) ** 2
        if int(t ** 0.5) ** 2 == t:
            count += 1

print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math


def judge_dist(X, Y):
    total = 0
    for x, y in zip(X, Y):
        total += (x - y) ** 2
    return int(math.sqrt(total)) ** 2 == total


def solve(N: int, D: int, X: "List[List[int]]"):
    count = 0
    for i in range(1, N):
        for j in range(i):
            if judge_dist(X[i], X[j]):
                count += 1
    print(count)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    X = [ [ int(next(tokens)) for _ in range(D) ] for _ in range(N) ]  # type: "List[List[int]]"
    solve(N, D, X)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import itertools

N,D = (int(x) for x in input().split())

X_arr = []
for _ in range(N):
    X_arr.append([int(x) for x in input().split()])

ans = 0
for Y,Z in itertools.combinations(X_arr, 2):
    r2 = sum([(y-z)**2 for y,z in zip(Y,Z)])
    if round(math.sqrt(r2))**2 == r2:
        ans += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import fractions
import copy
import numpy as np
def j(q):
    if q==1: print("Yes")
    elif q == 0:print("No")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
x,y = ips()
a = []
for i in range(x):
    a.append(iprow())
ans=0
for i in range(x):
    p = a[i]
    for k in range(i+1,x):
        q = a[k]
        s = 0
        for l in range(y):
            s+=pow(p[l]-q[l],2)
        if math.sqrt(s) == int(math.sqrt(s)):
            ans+=1
print(ans)

Submission status: Accepted

Solution code:
from math import sqrt


def dist2(aa, bb):
    return sum((a - b) ** 2 for a, b in zip(aa, bb))


def main():
    n, d = map(int, raw_input().split())
    xx = []
    for _ in xrange(n):
        xx.append(map(int, raw_input().split()))
    count = 0
    for i in xrange(n - 1):
        for j in xrange(i + 1, n):
            d = dist2(xx[i], xx[j])
            if d == int(sqrt(d)) ** 2:
                count += 1
    print count


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,D = [int(x) for x in input().split()]
dots = [[int(x) for x in input().split()]for _ in range(N)]
cnt = 0
for ind,x in enumerate(dots):
    for i in range(ind+1,N):
        ans = 0
        for j in range(D):
            ans+=(x[j]-dots[i][j])**2
        ans **= 0.5
        if(int(ans)==ans):
            cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
from math import sqrt
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):
        d = 0
        for p1, p2 in zip(x[i], x[j]):
            d += (p1 - p2) ** 2
        if int(sqrt(d)) ** 2 == d:
            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-1):
    for j in range(i+1, N):
        t = 0
        for k in range(D):
            t += (X[i][k] - X[j][k])**2
        if t == ( int(t**(1/2)))**2:
            ans += 1
print(ans)

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

import math

ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        dist2 = sum([(Xi - Xj) ** 2 for Xi, Xj in zip(X[i], X[j])])
        dist = int(math.sqrt(dist2))
        if dist ** 2 == dist2:
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
N, D = list(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):
    d = 0
    for k in range(D):
      d += pow(X[i][k] - X[j][k], 2)
    if pow(int(d ** 0.5), 2) == d:
      ans += 1

print(ans)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,D = MI()
    X = LLIN(N)
    ans = 0

    def distanse(A,B):
        sum = 0
        for a,b in zip(A,B):
            sum += (a-b) ** 2
        return sqrt(sum)

    for i in range(N):
        for j in range(i+1,N):
            if i == j:
                continue
            dist =  distanse(X[i], X[j])
            if dist == int(dist):
                ans+=1
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
from itertools import combinations

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

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

cnt = 0
for p,q in combinations(points, 2):
    d = np.linalg.norm(p-q)
    if float(d) == int(d):
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
from math import sqrt

n, d = [int(i) for i in input().split()]
X = [[int(i) for i in input().split()] for j in range(n)]

ans = 0

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

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs,math
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): #intのlist
	return list(map(int,input().split()))
def m(): #複数文字
	return map(int,input().split())
def onem(): #Nとかの取得
	return int(input())
def s(x): #圧縮
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x): #listをスペースごとに分ける
	return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
	return max(map(max,x))

n,d = m()

a =[]
for i in range(n):
	a.append(l())
co = 0
su = 0
for i in range(n-1):
	for j in range(i+1,n):
		su = 0
		for k in range(d):
			su += (a[i][k] - a[j][k])**2
		if math.sqrt(su) == int(math.sqrt(su)):
			co += 1
print(co)

Submission status: Accepted

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

import numpy as np
x = np.array(x)

counter = 0
for i in range(n):
    y = x[i]
    for j in range(n):
        z = x[j]
        if j != i:
            dist = np.sqrt(np.sum((y - z) ** 2))
            if dist == int(dist):
                counter += 1

print(int(counter / 2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from operator import itemgetter
# gcd
from fractions import gcd
# 切り上げ，切り捨て
from math import ceil, floor, sqrt
# リストの真のコピー（変更が伝播しない）
from copy import deepcopy
# 累積和。list(accumulate(A))としてAの累積和
from itertools import accumulate
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# S = Counter(l)  # カウンタークラスが作られる。S=Counter({'b': 5, 'c': 4, 'a': 3})
# print(S.most_common(2))  # [('b', 5), ('c', 4)]
# print(S.keys())  # dict_keys(['a', 'b', 'c'])
# print(S.values())  # dict_values([3, 5, 4])
# print(S.items())  # dict_items([('a', 3), ('b', 5), ('c', 4)])
from collections import Counter
import math
from functools import reduce
sys.setrecursionlimit(200000)
# input関係の定義
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
def debug(x): print("debug: ", x, file=sys.stderr)
# template


# fin = open('in_1.txt', 'r')
# sys.stdin = fin
# # local only


def solve(N: int, D: int, X: "List[List[int]]"):
    ans = 0
    for i in range(N):
        for j in range(i + 1, N):
            tmp = sum([(X[i][t] - X[j][t]) ** 2 for t in range(D)])
            for _i in range(int(sqrt(tmp)) - 2, int(sqrt(tmp)) + 2):
                if _i * _i == tmp:
                    ans += 1
                    break
    print(ans)


def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


def main():
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    X = [[int(next(tokens)) for _ in range(D)]
         for _ in range(N)]  # type: "List[List[int]]"
    solve(N, D, X)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

    pairs = 0

    for i in range(n):
        for j in range(i+1,n):
            dist = 0
            for k in range(d):
                dist += (points[i][k]-points[j][k])**2

            if int(dist**0.5) == dist**0.5:
                pairs += 1

    print(pairs)


main()

Submission status: Accepted

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

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

ANS=0
import math

for i in range(N-1):
    for j in range(i+1,N):
        DIS=math.sqrt(sum([(X[i][k]-X[j][k])**2 for k in range(D)]))

        if DIS==int(DIS):
            ANS+=1

print(ANS)

Submission status: Accepted

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

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        pi, pj = P[i], P[j]
        tmp = 0
        for d in range(D):
            tmp += (pi[d] - pj[d]) ** 2
        if (tmp ** 0.5).is_integer():
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
def solve(N, D, Xs):
    ans = 0
    for i in range(N):
        for j in range(i + 1, N):
            k = 0
            for d in range(D):
                k += pow(Xs[i][d] - Xs[j][d], 2)
            d = pow(k, 0.5)
            if d == int(d):
                ans += 1
    return ans

if __name__ == "__main__":
    N, D = tuple(map(int, input().split(" ")))
    Xs = [tuple(map(int, input().split(" "))) for _ in range(N)]
    print(solve(N, D, Xs))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math
INF = float("inf")

tol = 1e-6


def solve(N: int, D: int, X: "List[List[int]]"):

    counter = 0
    for i in range(N):
        for j in range(i+1, N):
            dist = 0
            for a, b in zip(X[i], X[j]):
                dist += (a-b)**2
            dist = math.sqrt(dist)
            if abs(dist - round(dist)) < tol:
                counter += 1
    print(counter)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    X = [[int(next(tokens)) for _ in range(D)]
         for _ in range(N)]  # type: "List[List[int]]"
    solve(N, D, X)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,d = map(int,input().split())
x = [list(map(int,input().split())) for i in range(n)]
ans = 0
def k(a,b):
    dist = 0
    for i in range(d):
        dist +=(a[i]-b[i])**2
    dist = dist**(1/2)
    while dist > 0:
        dist -= 1
        if dist == 0:
            return True
    return False
for i in range(n):
    for j in range(i+1,n):
        if k(x[i],x[j]):
            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 a in range(1,N):
    for b in range(a):
        jijou = sum([(y-z)**2 for y,z in zip(X[a],X[b])])
        if int(sqrt(jijou))**2 == jijou:
            ans+=1
print(ans)

Submission status: Accepted

Solution code:
n, d = list(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):
    t = 0
    for k in range(d):
      t += (x[i][k]-x[j][k])**2
    t = t**(1/2)
    if t.is_integer():
      ans += 1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as ite
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N, D = map(int, raw_input().split())
X = []
for i in range(N):
    p = map(int, raw_input().split())
    X += [p]

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        S = sum((X[i][k] - X[j][k]) * (X[i][k] - X[j][k]) for k in range(D))
        for q in range(1000):
            if q * q >= S:
                ans += (q * q == S)
                break
print ans

Submission status: Accepted

Solution code:
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N, D = nl()

points = []
for i in range(N):
    Xs = nl()
    points.append(Xs)

import math
count  = 0
for i in range(N-1):
    for j in range(i+1, N):
        t = 0
        for k in range(D):
            diff = points[i][k] - points[j][k]
            t += diff * diff
        st = int(math.sqrt(t))
        if st * st == t:
            count +=1

print(count)

Submission status: Accepted

Solution code:
N, D = 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):
        tmp = 0
        for k in range(D):
            tmp += (X[i][k] - X[j][k]) ** 2

        if int(tmp ** 0.5) ** 2 == tmp:
            num += 1

print(num)

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,a,b = LI()
    print(min(a*n,b))
    return

#B
def B():
    n,d = LI()
    x = LIR(n)
    f = defaultdict(lambda : 0)
    for i in range(1000000):
        f[i*i] = 1
    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 f[s]:
                ans += 1
    print(ans)
    return

#C
def C():
    n = I()

    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return


#Solve
if __name__ == "__main__":
    B()

Submission status: Accepted

Solution code:
# @author 

import sys

class BGoodDistance:
    def dist(self, u, v):
        return (sum((u[i] - v[i]) ** 2 for i in range(len(u)))) ** .5
    def solve(self):
        n, d = [int(_) for _ in input().split()]
        p = []
        for _ in range(n):
            p.append([int(_) for _ in input().split()])
        
        ans = 0
        for i in range(n):
            for j in range(i + 1, n):
                d = self.dist(p[i], p[j])
                if abs(d - int(d)) < 1e-9:
                    ans += 1
        
        print(ans)

solver = BGoodDistance()
input = sys.stdin.readline

solver.solve()

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import numpy as np
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N,D = inpl()
xx = [inpl() for _ in range(N)]

db = [i**2 for i in range(1,500)]

def dist(i,j):
    tmp = 0
    for k in range(D):
        tmp += (xx[i][k]-xx[j][k])**2
    return tmp

ans = 0
for i in range(N):
    for j in range(i+1,N):
        tmp = dist(i,j)
        if tmp in db:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
from math import sqrt
n, d = map(int, input().split())
xs = [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):
        a = 0
        for k in range(d):
            a += (xs[i][k]-xs[j][k])**2
        if int(sqrt(a))**2 == a:
            ans += 1
print(ans)

Submission status: Accepted

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

def dst(i, j):
    a = pnt[i]
    b = pnt[j]
    ret = 0
    for p, q in zip(a, b):
        ret += (p - q) ** 2
    r = int(ret ** (0.5))
    if r ** 2 == ret:
        return True
    else:
        return False

ret = 0
for i in range(n):
    for j in range(i + 1, n):
        tmp = dst(i, j)
        if tmp:
            ret += 1
print(ret)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, D: int, X: "List[List[int]]"):
    ret = 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 tmp == int(tmp ** 0.5) * int(tmp ** 0.5):
                ret += 1
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    X = [ [ int(next(tokens)) for _ in range(D) ] for _ in range(N) ]  # type: "List[List[int]]"
    solve(N, D, X)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, d = na()
co = []
for i in range(n):
    co.append(na())

ct = 0
for i in range(n):
    for j in range(i+1,n):
        a = co[i]
        b = co[j]
        s = 0
        for k in range(d):
            s += (a[k] - b[k])*(a[k] - b[k])
        ss = int(math.sqrt(s))
        if ss * ss == s:
            ct += 1
print(ct)

Submission status: Runtime Error

Solution code:
#ABC133 B

import math
N,D = map(int,input().split())
point = []
count = 0
for i in range(N):
    X = list(map(int,input().split()))
    point.append(X)
for i in range(1,N):
    for j in range(i+1,N+1):
        S = 0
        for k in range(D):
            square = []
            square.append(((point[i-1][k])-(point[j-1][k]))**2)
            S += math.sqrt(square)
            if S % 1 ==0:
                count += 1
        

print(count)

Submission status: Runtime Error

Solution code:
#ABC133 B

import math
N,D = map(int,input().split())
point = []
dist = []
count = 0
for i in range(N):
    X = list(map(int,input().split()))
    point.append(X)
for i in range(1,N+1):
    for j in range(i+1,N+1):
        for k in range(D):
            square = []
            square.append(((point[i-1][k])-(point[j-1][k]))**2)
            S = math.sqrt(sum(square))
            dist.append(S)

for i in range(N*(N-1)/2):
    if S[i] == int(S[i]):
        count += 1

print(count)

Submission status: Runtime Error

Solution code:
n,d=map(int, readline().rstrip().split())
m=[]
for _ in range(n):
    arr=list(map(int, readline().rstrip().split()))
    m.append(arr)
ans=0
for i in range(n-1):
    for j in range(i+1,n):
        a = 0
        for k in range(d):
            a += (m[i][k] - m[j][k])**2
        x = int(a**(1/2))
        if x*2 == a:
            ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
N,D = map(int,input().split())
import numpy as np
xlist = [list(map(int,input().split())) for i in range(n)]
xarray = np.array(xlist)
ans = 0
for i in range(n):
  for j in range(i+1,n):
    if np.linalg.norm(xarray[i]-xarray[j])==int(np.linalg.norm(xarray[i]-xarray[j])):
      ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
N,D = map(int,input().split())
import numpy as np
xlist = [list(map(int,input().split())) for i in range(n)]
xarray = np.array(xlist)
ans = 0
for i in range(n):
  for j in range(i+1,n):
    if np.linalg.norm(xarray[i]-xarray[j])%1!=0:
      ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
N,D = map(int,input().split())
import numpy as np
xlist = [list(map(int,input().split())) for i in range(n)]
xarray = np.array(xlist)
ans = 0
for i in range(n):
  for j in range(i+1,n+1):
    if np.linalg.norm(xarray[i]-xarray[j])%1!=0:
      ans+=1
print(ans)