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: Runtime Error

Solution code:
import numpy as np

def sq(x,i):
    if i > x/2:
        return False
    else:
        if x%i == 0:
            if int(x/i) == i:
                return i
            else:
                return sq(x,i+1)
        else:
            return sq(x,i+1)

def Ten(x,i):
    if x/(10**i) > 10:
        return Ten(x/10,i+1)
    else:
        return i

a = input().split(" ")
N = int(a[0])
D = int(a[1])

X = []
count = 0
for i in range(N):
    a = input().split(" ")
    Y = [int(i) for i in a]
    X.append(Y)
X = np.array(X)

if D == 1:
    for i in range(N-1):
        for j in range(i+1,N):
            count += 1
else:
    for i in range(N-1):
        for j in range(i+1,N):
            Z = X[i]-X[j]
            if sq(np.dot(Z.T,Z),10**Ten(np.dot(Z.T,Z),0)) != False:
                count += 1
print(count)

Submission status: Runtime Error

Solution code:
import math

N = int(input()) 
D = int(input()) 

array = [[0]*10]*10
dis = 0
sum_dis = 0
for i in range(N-1):
    for j in range(i+1,N):
        for k in range(D):
            dis += (array[i][k] - array[i][k])**2
        dis = math.sqrt(dis)
        if(type(dis) == int): sum_dis += 1
print(sum_dis)

Submission status: Runtime Error

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

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

cnt = 0
for i in range(0, len(X)):
    sum = 0
    for j in range(0, len(X[i])):
        if i >= len(X[i]):
            sum += X[i][j] - X[0][j]
        else:
            sum += (X[i][j] - X[i+1][j])**2

    if math.sqrt(sum).is_integer():
        cnt += 1
print(cnt)

Submission status: Runtime Error

Solution code:
import numpy as np

n,d=map(int,input().split())
arr=np.array(n)
for i in range(n):
  arr[i]=np.array(list(map(float,input().split())))
ans=0
for i in range(n):
  for j in range(i,n):
    if j!=i:
      k = np.linalg.norm(arr[i]-arr[j],ord=2)
    else:
      k=np.nan
    if k==int(k):
      ans +=1
print(ans)

Submission status: Runtime Error

Solution code:
s=input()
#s = ("4 2 9")
#print(s) 
s = s.split(" ")

n = int( s[0] )
d = int( s[1] )

Z=[]

for i in range(0,n-1):
    zahyou = input()
    zahyou = zahyou.split("")
    Z.append(zahyou)
    

for i in range(0,n-2):
    for j in range(i,n-1):
        zzz = 0
        for k in range(0,d-s):
            zz =( (int(zahyou[i][k])-int([j][k]))**2 ) **0.5
            zzz =+ zz
        if (zzz % 1):
            result =+ 1

print(result)

Submission status: Runtime Error

Solution code:
import numpy as np

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

p = np.zeros(n,d)

for i in range(n):
  p[i] = np.array(list(map(int, input().split())))

cnt = 0
d = 0
for i in range(n):
  for j in range(i+1,n):
    d = np.sqrt((p[i] - p[j]) ** 2)
    if d % 1 == 0:
      cnt += 1

print(cnt)

Submission status: Runtime Error

Solution code:
#include<stdio.h>
#include<math.h>
int dp[100][100];
int n, d;
int main() {
    scanf("%d%d", &n, &d);
    int counter = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < d; j++) {
            scanf("%d", &dp[i][j]);
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = i+1; j < n; j++) {
            int su = 0;
            for (int k = 0; k < d; k++) {
                su += (dp[i][k] - dp[j][k]) * (dp[i][k] - dp[j][k]);
                //printf("su: %d\n", su);
            }
            
            if (sqrt(su) * sqrt(su) == su) {
                counter++;
                //printf("%d %d %d\n", i, j, su);
            }
        }
    }
    printf("%d\n", counter);
    return 0;
}

Submission status: Runtime Error

Solution code:
import math
import itertools

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

X = [[0]*D+1]*N+1

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

def distance(x, y):
    dis = 0
    for d in range(D):
        dis += abs(x[d] - y[d]) ** 2
    
    return math.sqrt(dis).is_integer()
    
distance(X[1], X[2])

count = 0

for c in itertools.combinations(list(range(N)), 2):
    if distance(X[c[0]], X[c[1]]):
        count += 1
        
print(count)

Submission status: Runtime Error

Solution code:
def distance(a, b):
    s = 0
    for i in range(len(a)):
        s += (a[i] - b[i]) ** 2

    return s ** 0.5

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

X = []

for i in range(D):
    X.append([int(x) for x in input().split()])

count = 0

for i in range(D):
    for j in range(i+1, D):
        if distance(X[i], X[j]).is_integer():
            count += 1

print(count)

Submission status: Runtime Error

Solution code:
import math
import itertools

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

X = [[0]*D]*N

for n in range(N):
    X[n] = list(map(int, input().split()))
    
#print(X)

def distance(x, y):
    dis = 0
    for d in range(D):
        dis += abs(x[d] - y[d]) ** 2
    
    #print(math.sqrt(dis))
    return math.sqrt(dis).is_integer()
    
distance(X[1], X[2])

#print(list(range(N)))

count = 0

for c in itertools.combinations(list(range(N)), 2):
    #print(c)
    if distance(X[c[0]], X[c[1]]):
        count += 1
        
print(count)

Submission status: Runtime Error

Solution code:
n , d =map(int,input().split())
import sys
import math
 
a = [map(int,input().split()) for l in range(n)]
count = 0
 
for i in range(n):
  for j in range(n):
    if i == j:
      count += 0
    else:
      d = math.sqrt((a[i][0] - a[j][0]) ** 2 + (a[i][1] - a[j][1]) ** 2 +(a[i][2] - a[j][2]) ** 2  +(a[i][3] - a[j][3]) ** 2  + (a[i][4] - a[j][4]) ** 2)
      d = float(d)
      if d.is_integer():
        count += 1
print(count)

Submission status: Runtime Error

Solution code:
import math

n,d=map(int,input().split())
x=[]
for j in range(n):
tmp=list(map(int,input().split()))
x.append(tmp)

count=0
for i in range(n-1):
for j in range(i+1,n):
ans=0
for (a,b) in zip(x[i],x[j]):
ans+=(a-b)**2
if math.sqrt(ans).is_integer():
count+=1

print(count)

Submission status: Runtime Error

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


import itertools,math
dis = []
for x in itertools.combinations(x_list_kai,2):
    distance = 0
    for dim in range(0,d):
        result  = 0
        for y in x:
            if result == 0:
                result = y[dim]
            else:
                result -= y[dim]
            
        result  = result **2
        distance += result
    dis.append(math.sqrt(distance))
    
counter = 0
for i in dis:
    if i.is_integer() == True:
        counter += 1
print(counter)

Submission status: Runtime Error

Solution code:
import numpy as np
n,d=map(int, input().split())
x = [np.array(list(map(int, input().split()))) for i in range(n)]
a=0
for i in range(len(x)):
    for j in range(i+1, len(x)):
        if float.is_integer(np.linalg.norm(x[i] - x[j])):
            a += 1
print(a)

Submission status: Runtime Error

Solution code:
import numpy as np

def sq(x,i):
    if i > x/2:
        return False
    else:
        if x%i == 0:
            if int(x/i) == i:
                return i
            else:
                return sq(x,i+1)
        else:
            return sq(x,i+1)
a = input().split(" ")
N = int(a[0])
D = int(a[1])

X = []
count = 0
for i in range(N):
    a = input().split(" ")
    Y = [int(i) for i in a]
    X.append(Y)
X = np.array(X)

if D == 1:
    for i in range(N-1):
        for j in range(i+1,N):
            count += 1
else:
    for i in range(N-1):
        for j in range(i+1,N):
            Z = X[i]-X[j]
            if sq(np.dot(Z.T,Z),1) != False:
                count += 1
print(count)

Submission status: Runtime Error

Solution code:
import itertools
import math

N, D = [int(x) for x in input().strip().split()]
if D == 1:
    print(int(N*(N-1)/2)
else:    
    points = []
    for data in range(N):
        points.append([int(x) for x in input().strip().split()])

    result = 0
    for p1,p2 in itertools.combinations(points, 2):
        distance = 0
        for y,z in zip(p1,p2):
            distance += (y - z) ** 2
        
        distance = math.sqrt(distance)
        if distance.is_integer(): result += 1    
    print(result)

Submission status: Runtime Error

Solution code:
n , d =map(int,input().split())
import sys
import math
 
a = [[int(c) for c in l.strip()] for l in sys.stdin]
count = 0
 
for i in range(n):
  for j in range(n):
    if i == j:
      count += 0
    else:
      d = math.sqrt((a[i][0] - a[j][0]) ** 2 + (a[i][1] - a[j][1]) ** 2 +(a[i][2] - a[j][2]) ** 2  +(a[i][3] - a[j][3]) ** 2  + (a[i][4] - a[j][4]) ** 2)
      d = float(d)
      if d.is_integer():
        count += 1
print(count)

Submission status: Runtime Error

Solution code:
import math
b, c = map(int, input().split())
count  = 0
sum_a = 0
ans = 0

dim_b = list(input().split())
while count < b :
  dim_a = list(input().split())
  
  for i in len(dim_b):
    sum_a +=  (dim_b[i] - dim_a[i])^2
  
  
  n = math.sqrt(sum_a)
  
  if n.is_integer():
    ans +=1
  
  sum_a = 0
  dim_b = dim_a
  count +=1

Submission status: Runtime Error

Solution code:
import numpy as np

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

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

ans=0
for i in range(n-1):
    for j in range(i+1,n):
        if np.linalg.norm(x[i]-x[j]).is_integer():
#            print(np.linalg.norm(x[i]-x[j]))
            ans+=1

print(ans)

Submission status: Runtime Error

Solution code:
import itertools
import math

N, D = [int(x) for x in input().strip().split()]
if D == 1:
    print(int(N*(N-1)/2))
    exit(1)
    
points = []
for data in range(N):
    points.append([int(x) for x in input().strip().split()])

result = 0
for p1,p2 in itertools.combinations(points, 2):
    distance = 0
    for y,z in zip(p1,p2):
        distance += (y - z) ** 2
        
    distance = math.sqrt(distance)
    if distance.is_integer(): result += 1
        
print(result)

Submission status: Runtime Error

Solution code:
def input_from_console():
    n, d = map(int, input().split())
    x_matrix = []
    for i in range(n):
        x = map(int, input().split())
        x_matrix.append(list(x))
    return n, d, x_matrix


def solve(n, d, x_matrix):
    result = 0
    for i in range(n):
        for j in range(n - i -1):
            distance = math.sqrt(sum([(p1 - p2) ** 2 for p1, p2 in zip(x_matrix[i], x_matrix[j + i + 1])]))
            if distance == int(distance):
                result += 1
    return result


def main():
    n, d, x_matrix = input_from_console()
    print(solve(n, d, x_matrix))


if __name__ == "__main__":
    import sys

    if sys.gettrace():  # Check if the python interpreter is in debug mode or not.
        check_cases()
    else:
        main()

Submission status: Runtime Error

Solution code:
#!/usr/local/bin/python3
#coding:utf-8
#import numpy as np


def main():

   in0 = input()

   IN = in0.split(' ')

   N = int(IN[0])
   D = int(IN[1])

   X = np.zeros((N,D))

   for i in range(N):
      in0 = input()
      IN = in0.split(' ')
      for j in range(D):
         X[i,j] = int(IN[j])


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

         dist = np.sqrt(dis)

         i_dist = int(dist)

         if((dist - i_dist)==0):
            cnt = cnt + 1


   print ("%s" % (cnt))




if __name__ == "__main__":
   main()

Submission status: Runtime Error

Solution code:
#!/usr/local/bin/python3
#coding:utf-8
#import numpy as np


def main():

   in0 = input()

   IN = in0.split(' ')

   N = int(IN[0])
   D = int(IN[1])

   X = np.zeros((N,D))

   for i in range(N):
      in0 = input()
      IN = in0.split(' ')
      for j in range(D):
         X[i,j] = int(IN[j])


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

         dist = np.sqrt(dis)

         i_dist = int(dist)

         if((dist - i_dist)==0):
            cnt = cnt + 1


   print ("%s" % (cnt))




if __name__ == "__main__":
   main()

Submission status: Runtime Error

Solution code:
import numpy as np

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

X = []

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

res = 0
X = np.array(X)
for i in range(N):
    if i == j:
        continue
    for j in range(i + 1, N):
        if np.linalg.norm(X[i] - X[j]).is_integer():
            res += 1

print(res)

Submission status: Runtime Error

Solution code:

import numpy as np

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

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

ans=0
for i in range(n-1):
    for j in range(i+1,n):
        if np.linalg.norm(x[i]-x[j]) % 1 == 0:
#            print(np.linalg.norm(x[i]-x[j]))
            ans+=1

print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/local/bin/python3
#coding:utf-8
#import numpy as np


def main():

   in0 = input()

   IN = in0.split(' ')

   N = int(IN[0])
   D = int(IN[1])

   X = np.zeros((N,D))
   
   for i in range(N):
      in0 = input()
      IN = in0.split(' ')
      for j in range(D):
         X[i,j] = int(IN[j])


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

      dist = np.sqrt(dis)

      i_dist = int(dist)
      
      if((dist - i_dist)==0):
         cnt = cnt + 1


   print ("%s" % (cnt))




if __name__ == "__main__":
   main()

Submission status: Runtime Error

Solution code:
import numpy


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

a = numpy.array(X[0])
b = numpy.array(X[1])
u = b - z
print(numpy.linalg.norm(u))

Submission status: Runtime Error

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

import math

count = 0
for x in range(len(arX)):
    xp = arX[x]
    for y in range(x, len(arX)):
        if x == y:
            continue
        
        yp = arX[y]
        
        powSum = 0
        for d in range(D):
            powSum += (xp[d] - yp[d]) ** 2
        
        if debug:
            print(x, y, sqPowSum)
            
        sqPowSum = math.sqrt(powSum)
        if sqPowSum == int(sqPowSum):
            count += 1

print(count)

Submission status: Runtime Error

Solution code:
n , d =map(int,input().split())
import sys
import numpy as np
 
a = [[int(c) for c in l.strip()] for l in sys.stdin]
a_ = np.array(a)
count = 0
 
for i in range(n):
  for j in range(n):
    if i == j:
      count += 0
    else:
      u = a[i] - a[j]
      norm = numpy.linalg.norm(u)
      if norm.is_integer():
        count += 1
print(count)

Submission status: Runtime Error

Solution code:
n = list(map(int, input().rstrip().split(" ")))
ans = 0
coor = []
for a in n[0]:
  coor.append(list(map(int, input().rstrip().split(" "))))
  
def dis(a,b):
  ans = 0
  for i in range(a):
    ans += (a[i] - b[i])**2
  return ans**0.5
  
for a in range(len(coor)):
  for b in range(0,a):
    if coor[a]!=coor[b]:
      if dis(coor[a], coor[b]).is_integer():
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

c = [1 if np.linalg.norm(x[i]-x[i-1]).is_integer() else 0 for i in range(len(x)) ]
print(sum(c))

Submission status: Runtime Error

Solution code:
import numpy as np

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

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

ans=0
for i in range(n-1):
    for j in range(i+1,n):
        if np.linalg.norm(x[i]-x[j]) % 1 == 0.0:
#            print(np.linalg.norm(x[i]-x[j]))
            ans+=1





print(ans)

Submission status: Runtime Error

Solution code:
mport 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 d in range(D):
            dis += (X[i][d] - X[j][d])**2
        if math.sqrt(dis) == int(math.sqrt(dis)):
            cnt += 1

print(cnt)

Submission status: Runtime Error

Solution code:
import math
def dist(a,b,d):
  distance=0
  for i in range(d):
    distance+=(a[i]-b[i])**2
  return distance
n,d=map(int,input().split())
x=[]
c=0
for i in range(n):
  x.append(list(map(int,input().split())))
for i in range(1,n):
  for j in range(i-1):
    k=dist(x[i],x[j])
    if k==(int(math.sqrt(k)))**2:
      c+=1

Submission status: Runtime Error

Solution code:
import numpy as np

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

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

def distance(p1, p2):
  squares = np.square(np.arange(p1) - np.arange(p2))
  return np.sqrt(squares.sum())

count = 0
for i in range(N):
  for j in range(N):
    if i != j:
      dist = distance(points[i], points[j])
      count += dist.is_integer() * 1

print(count)

Submission status: Runtime Error

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

a = [[int(c) for c in l.strip()] for l in sys.stdin]
a_ = np.array(a)
count = 0

for i in range(n):
  for j in range(n):
    if i == j:
      count += 0
    else:
      u = a[i] - a[j]
      norm = numpy.linalg.norm(u)
      if norm.is_integer():
        count += 1
print(count)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
using namespace std;

int a[19][19];

int check(int d,int s)
{
	int che=0;
    int i=0;
	while(1)
	{
		int ji=i*i;
		if (ji>4000) break;
		if (ji==s) che=1;
		i++;
	}		
	
	return che;
}
int main()
{
	int n,d; cin>>n>>d;
	for (int i=1; i<=n; i++)
	    for( int j=1; j<=d; j++)
	        cin>>a[i][j];
	
	int ans=0;        
    for (int i=1; i<=n-1; i++)
        for (int j=i+1; j<=n; j++)
        {
        	int res=0;
        	for (int u=1; u<=d; u++)
        	    res+=(a[i][u]-a[j][u])*(a[i][u]-a[j][u]);
        	        
        	if (check(d,res)) ans++;   
		}
		
	cout << ans;
 }

Submission status: Runtime Error

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

row = list()
count = 0
for i in range(N):
  x = [int(_) for _ in input().split()]
  if i == 0:
    row.append(x)
  else:
    for j in len(row):
      dist = 0
      for k in range(D):
        dist += (row[j][k]-x[k])^2
      if (dist**1/2) == int:
      	count += 1
    row.append(x)
      
print(count)

Submission status: Runtime Error

Solution code:
n,d=map(int,input().split())
x=[list(map(int,input().split())) for _ in range(n)]
p=0
def c(i,j):
  global x
  s=0
  for k in range(d):
    s+=(x[i][d]-x[j][d])**2
  if s**(1/2).is_integer()==True:
    return 1
  else:
    return 0
for i in range(n):
  for j in range(i+1,n):
    p+=c(i,j)
print(p)

Submission status: Runtime Error

Solution code:
from math import sqrt, isclose, floor
N, D = [int(elem) for elem in input().split()]
coords = [[int(elem) for elem in input().split()] for _ in range(N)]
assert len(coords[0]) == D

num_pairs = 0
for i in range(N):
    c1 = coords[i]
    for j in range(i + 1, N):
        c2 = coords[j]
        sqaured_dist = sum((p1 - p2) ** 2 for p1, p2 in zip(c1, c2))
        dist = floor(sqrt(sqaured_dist))
        if sqaured_dist // dist == dist:
            num_pairs += 1
print(num_pairs)

Submission status: Runtime Error

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

def helper(a, b):
  s = 0
  for i in range(n):
    s += (a[i] - b[i]) ** 2
  a = int(0.5 + (s ** 0.5))
  if a * a == s:
    return 1
  return 0

ans = 0
for i in range(n):
	for j in range(i + 1, n):
		ans += helper(points[i], points[j])
print(ans)