Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Distance II

  Your task is to calculate the distance between two $n$ dimensional vectors $x = \{x_1, x_2, ..., x_n\}$ and $y = \{y_1, y_2, ..., y_n\}$.

  The Minkowski's distance defined below is a metric which is a generalization of both the Manhattan distance and the Euclidean distance.

\[
D_{xy} = (\sum_{i=1}^n |x_i - y_i|^p)^{\frac{1}{p}}
\]

It can be the Manhattan distance 

\[
D_{xy} = |x_1 - y_1| + |x_2 - y_2| + ... + |x_n - y_n|
\]

where $p = 1 $.

It can be the Euclidean distance

\[
D_{xy} = \sqrt{(|x_1 - y_1|)^{2} + (|x_2 - y_2|)^{2} + ... + (|x_n - y_n|)^{2}}
\]

where $p = 2 $.

Also, it can be the Chebyshev distance

\[
D_{xy} = max_{i=1}^n (|x_i - y_i|)
\]

where $p = \infty$

Write a program which reads two $n$ dimensional vectors $x$ and $y$, and calculates Minkowski's distance where $p = 1, 2, 3, \infty$ respectively.

Input

  In the first line, an integer $n$ is given. In the second and third line, $x = \{x_1, x_2, ... x_n\}$ and $y = \{y_1, y_2, ... y_n\}$ are given respectively. The elements in $x$ and $y$ are given in integers.

Output

  Print the distance where $p = 1, 2, 3$ and $\infty$ in a line respectively.
The output should not contain an absolute error greater than 10-5.

Constraints

$1 \leq n \leq 100$

$0 \leq x_i, y_i \leq 1000$

Sample Input

3
1 2 3
2 0 4

Sample Output

4.000000
2.449490
2.154435
2.000000



Sample input:
3
1 2 3
2 0 4


Sample output:
4.000000
2.449490
2.154435
2.000000

Submission status: Wrong Answer

Solution code:
from math import fabs, sqrt

n = int(input())
x = list(map(float, input().strip().split()))
y = list(map(float, input().strip().split()))

sum1 = 0
sum2 = 0
sum3 = 0
pil = []

for i in range(n):
    sum1 += fabs(x[i] - y[i])
    sum2 += fabs(x[i] - y[i]) ** 2
    sum3 += fabs(x[i] - y[i]) ** 3
    pil.append(x[i] - y[i])

pil = sorted(pil)
p1 = sum1
p2 = sqrt(sum2)
p3 = sum3 ** (1 / 3)
pi = pil[n-1]

print(round(p1, 8))
print(round(p2, 8))
print(round(p3, 8))
print(round(pi, 8))

Submission status: Wrong Answer

Solution code:
from math import fabs, sqrt

n = int(input())
x = list(map(float, input().strip().split()))
y = list(map(float, input().strip().split()))

sum1 = 0
sum2 = 0
sum3 = 0
pil = []

for i in range(n):
    sum1 += fabs(x[i] - y[i])
    sum2 += fabs(x[i] - y[i]) ** 2
    sum3 += fabs(x[i] - y[i]) ** 3
    pil.append(fabs(x[i] - y[i]))

pil = sorted(pil)
p1 = sum1
p2 = sqrt(sum2)
p3 = sum3 ** (1 / 3)
pi = pil[n-1]
print(pil)
print(round(p1, 8))
print(round(p2, 8))
print(round(p3, 8))
print(round(pi, 8))

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))

def suan(p):
	d = 0
	for i in range(n):
		d += (math.fabs(x[i] - y[i]))**p
	print(d**(1/p))

suan(1)
suan(2)
suan(3)
suan(999)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))

def suan(p):
	d = 0
	for i in range(n):
		d += (math.fabs(x[i] - y[i]))**p
	print(d**(1/p))

suan(1)
suan(2)
suan(3)
suan(float('inf') )

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))

def suan(p):
	d = 0
	for i in range(n):
		d += (math.fabs(x[i] - y[i]))**p
	print('%.6f'%(d**(1/p)))

suan(1)
suan(2)
suan(3)
suan(float('inf') )

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))

def suan(p):
	d = 0
	for i in range(n):
		d += (math.fabs(x[i] - y[i]))**p
	print('%.8f'%(d**(1/p)))

suan(1)
suan(2)
suan(3)
suan(float('inf') )

Submission status: Wrong Answer

Solution code:
import math
def mink(x,y,p):
    return (sum([math.fabs(_x - _y)**p for _x, _y in zip(x,y)]))**(1/p)
n = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
print(mink(x,y,1))
print(mink(x,y,2))
print(mink(x,y,3))
print(mink(x,y,1000))

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
Z = list(itertools.product(X,Y))
Z = Z[::len(X)+1]
def fn(p):
    D = 0
    for z in Z:
        D += abs(z[0]-z[1])**p
    else:
        print(D**(1/p))
fn(1)
fn(2)
fn(3)
fn(999)

Submission status: Accepted

Solution code:
import math
def main():

    n = int(input())
    x = list(map(float, input().split()))
    y = list(map(float, input().split()))    

    print(Minkowski(n, x, y, 1))
    print(Minkowski(n, x, y, 2))
    print(Minkowski(n, x, y, 3))
    print(Chebyshev(n, x, y))


def Minkowski(n, x, y, p):
    temp = 0
    for i in range(n):
        temp += abs(y[i] - x[i])**p
    return temp**(1/p)

def Chebyshev(n, x, y):
    temp = 0
    for i in range(n):
        temp = max(temp, abs(y[i] - x[i]))
    return temp

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
d=[abs(a-b) for (a,b) in zip(x,y)]
for p in range(1,4):
    print(sum(map(lambda t: t**p,d))**(1/p))
print(max(d))

Submission status: Accepted

Solution code:
def main():
    input()
    x, y = [list(map(int, input().split())) for _ in range(2)]
    for p in (1, 2, 3):
        distance = (sum(map(lambda i: i ** p, [abs(xi - yi) for xi, yi, in zip(x, y)]))) ** (1/p)
        print('{:.5f}'.format(distance))
    distance = max([abs(xi - yi) for xi, yi in zip(x, y)])
    print('{:.5f}'.format(distance))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
p1 = p2 = p3 = p = 0
for x,y in zip(x,y):
    d = abs(x-y)
    p1 += d
    p2 += d**2
    p3 += d**3
    if d>p: p = d
print('{0:.5f}\n{1:.5f}\n{2:.5f}\n{3:.5f}'.format(p1,math.sqrt(p2),math.pow(p3,1/3),p))

Submission status: Accepted

Solution code:
import decimal
d = int(input())

x = list(map(int, input().split()))
y = list(map(int, input().split()))

D_1 = 0
for i in range(d):
    L_i = abs(x[i] - y[i])
    D_1 += L_i

D_2 = 0
for i in range(d):
    L_i = abs(x[i]-y[i])**2
    D_2 += L_i
D_2 = (D_2) **(1/2)

D_3 = 0
for i in range(d):
    L_i = abs(x[i]-y[i])**3
    D_3 += L_i
D_3 = (D_3) ** (1/3)

L =[]
for i in range(d):
    L.append(abs(x[i]-y[i]))

D_ = max(L)

print('{:.8f}'.format(D_1))
print('{:.8f}'.format(D_2))
print('{:.8f}'.format(D_3))
print('{:.8f}'.format(D_))

Submission status: Accepted

Solution code:
def dist(p, X, Y):
    s = 0
    for x, y in zip(X, Y):
        s += abs(x - y)**p
    return s**(1/p)
N = int(input())
*X, = map(int, input().split())
*Y, = map(int, input().split())
print(dist(1, X, Y))
print(dist(2, X, Y))
print(dist(3, X, Y))
print(max(abs(x - y) for x, y in zip(X, Y)))

Submission status: Accepted

Solution code:
n=int(input())
s_str=[]
for i in range(2):
    s_str.append(input().split())
s_int=[]
for i in s_str:
    s_int.append([int(s) for s in i])
manh=0
yu=0
p_3=0
che=[]
for i in range(n):
    manh+=abs(s_int[0][i]-s_int[1][i])
    yu+=(s_int[0][i]-s_int[1][i])**2
    p_3+=(abs(s_int[0][i]-s_int[1][i]))**3
    che.append(abs(s_int[0][i]-s_int[1][i]))
print(manh)
print(yu**(1/2))
print(p_3**(1/3))
print(max(che))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
Distance = [abs(xi-yi) for (xi,yi) in zip(x,y)]
print(sum(Distance))#マンハッタン距離
Euclid=[i**2 for i in Distance]
print(sum(Euclid)**0.5)
wa = [i**3 for i in Distance]
print(sum(wa)**(1/3))
print(max(Distance))

Submission status: Accepted

Solution code:
from sys import stdin
 
n = int(stdin.readline().rstrip())
x = [float(x) for x in stdin.readline().rstrip().split()]
y = [float(x) for x in stdin.readline().rstrip().split()]
 
def minkowski_dist(x, y, p="INF"):
    if p == "INF":
        return max([abs(x[i]-y[i]) for i in range(n)])
    elif isinstance(p, int):
        return (sum([(abs(x[i]-y[i]))**p for i in range(n)]))**(1/p)
 
print(minkowski_dist(x, y, 1))
print(minkowski_dist(x, y, 2))
print(minkowski_dist(x, y, 3))
print(minkowski_dist(x, y))

Submission status: Accepted

Solution code:
import math

# 提出用
n = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]

# # 動作確認用
# with open('input_itp1_10_d_1.txt', mode='r')as fin:
#     n = int(next(fin))
#     x = [int(i) for i in next(fin).split()]
#     y = [int(i) for i in next(fin).split()]

tmp = 0
for i in range(n):
    tmp += abs(x[i] - y[i])
print('%f' % (tmp))

tmp = 0
for i in range(n):
    tmp += (abs(x[i] - y[i]))**2
print('%f' % (math.sqrt(tmp)))

tmp = 0
for i in range(n):
    tmp += (abs(x[i] - y[i]))**3
print('%f' % (pow(tmp, 1 / 3)))

tmp = 0
for i in range(n):
    tmp = abs(x[i] - y[i]) if tmp < abs(x[i] - y[i]) else tmp
print('%f' % (tmp))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

dsum1 = 0
dsum2 = 0
dsum3 = 0
dmax = 0
for i in range(len(x)):
	d = abs(x[i]-y[i])
	dsum1 += d
	dmax = max(dmax, d)
	dsum2 += d**2
	dsum3 += d**3
print('{0:.6f}'.format(dsum1))
print('{0:.6f}'.format(dsum2**(1/2)))
print('{0:.6f}'.format(dsum3**(1/3)))
print('{0:.6f}'.format(dmax))

Submission status: Accepted

Solution code:
import math

num = int(input())

x = list(map(float,input().split()))

y = list(map(float,input().split()))

diff = [0]*num

for a in range(num):
    diff[a] = abs(x[a] - y[a])
    
print(sum(diff)) #1

po = [0]*num

for b in range(num):
    po[b] = diff[b] * diff[b]
    
print(math.sqrt(sum(po))) #2



for c in range(num):
    po[c] = diff[c] * diff[c] * diff[c]
    
print((sum(po))**(1/3)) #3

_max = 0

for d in diff:
    if _max < d:
        _max = d

print(_max)#infinity

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import math

num = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

result = 0
for i in range(num):
    result += abs(y[i]-x[i])
print(result)

result = 0
for i in range(num):
    result += abs(y[i] - x[i]) ** 2
result = math.sqrt(result)
print(result)

result = 0
for i in range(num):
    result += abs(y[i] - x[i]) ** 3
result = math.pow(result,1.0/3.0)
print(result)

result = 0
for i in range(num):
    if i == 0:
        result = abs(y[i] - x[i])
    else:
        if abs(y[i] - x[i]) > result:
            result = abs(y[i] - x[i])
print(result)

Submission status: Accepted

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

if __name__ == '__main__':
    n = int(raw_input())
    X = map(int, raw_input().split())
    Y = map(int, raw_input().split())

    dist = []
    for i in range(n):
        dist += [abs(X[i]-Y[i])]

    print sum(dist)
    print sum(map(lambda x : x**2, dist))**(1.0/2)
    print sum(map(lambda x : x**3, dist))**(1.0/3)
    print max(dist)

Submission status: Accepted

Solution code:
import math
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
a=[1,2,3,4]
for i in a:
	num=0
	if i==1:
		for j in range(len(x)):
			num+=abs(x[j]-y[j])
		print(num)
			
	elif i==2 or i==3:
		for j in range(len(x)):
			num+=(abs(x[j]-y[j]))**i
		num=math.pow(num,1/i)
		print(num)
	
	else:
		b=[]
		for j in range(len(x)):
			b.append(abs(x[j]-y[j]))
		print(float(max(b)))

Submission status: Accepted

Solution code:
def dist(p, X, Y):
    s = 0
    for x, y in zip(X, Y):
        s += abs(x-y)**p
    return s**(1/p)
N = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
print(dist(1,X,Y))
print(dist(2,X,Y))
print(dist(3,X,Y))
print(max(abs(x-y)for x, y in zip(X, Y)))

Submission status: Accepted

Solution code:
num = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
first = second = third = inf = 0.00000
for i in range(num):
    dif = x[i] - y[i]
    first = first + abs(dif)
    second = second + dif ** 2
    third = third + abs(dif) ** 3
    inf = max(inf, abs(dif))
print('{:.6f}'.format(first))
print('{:.6f}'.format(second ** 0.5))
print('{:.6f}'.format(third ** 0.333333333333333333))
print('{:.6f}'.format(inf))

Submission status: Accepted

Solution code:
import math

class vector():

    def __init__(self, x):
        self.x = x

def minkowski_p(x, y, p):
    s = 0

    for xi, yi in zip(x, y):
        s += abs(xi - yi) ** p

    s = s ** (1 / p)

    return s

def minkowski_infty(x, y):
    return max([abs(xi - yi) for xi, yi in zip(x, y)])

_ = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]

print(minkowski_p(x, y, 1))
print(minkowski_p(x, y, 2))
print(minkowski_p(x, y, 3))
print(minkowski_infty(x, y))

Submission status: Accepted

Solution code:

from math import *
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
p_1 = 0
p_2 = 0
p_3 = 0
p_inf = []
for i in range(n):
        p_1 += abs(x[i]-y[i])
        p_2 += pow(abs(x[i]-y[i]),2)
        p_3 += pow(abs(x[i]-y[i]),3)
        p_inf.append(abs(x[i]-y[i]))
print("%f" %p_1)
print("%f" %(sqrt(p_2)))
print("%f" %(pow(p_3,1/3)))
print("%f" %(max(p_inf)))

Submission status: Accepted

Solution code:
def minkowsuki(x, y, n):
    return sum(abs(x[i] - y[i]) ** n for i in range(len(x))) ** (1 / n)

n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))

print(minkowsuki(x, y, 1))
print(minkowsuki(x, y, 2))
print(minkowsuki(x, y, 3))
print(max(abs(x[i] - y[i]) for i in range(n)))

Submission status: Accepted

Solution code:
import math
n = input()
x = map(float,raw_input().split(' '))
y = map(float,raw_input().split(' '))
d1,d2,d3,d4 = 0.0,0.0,0.0,0.0
for i in range(0,n):
	d1 += abs(x[i]-y[i])
	d2 += abs(x[i]-y[i])**2.0
	d3 += abs(x[i]-y[i])**3.0
	if d4 < abs(x[i]-y[i]):
		d4 = abs(x[i]-y[i])
print d1
print math.sqrt(d2)
print d3**(1.0/3)
print d4

Submission status: Accepted

Solution code:
n = int(input())
x, y = [list(map(int, input().split())) for _ in range(2)]
print(sum([abs(x[i] - y[i]) for i in range(n)]))
print(pow(sum([(x[i] - y[i]) ** 2 for i in range(n)]), 1/2))
print(pow(sum([abs(x[i] - y[i]) ** 3 for i in range(n)]), 1/3))
print(max([abs(x[i] - y[i]) for i in range(n)]))

Submission status: Accepted

Solution code:
def f(): return map(int,raw_input().split())
def f1(n): return sum([e**n for e in x])**(1.0/n)
def f2(n): print '%.6f' %(n)

raw_input()
x = [abs(a-b) for a,b in zip(f(),f())]
f2(f1(1))
f2(f1(2))
f2(f1(3))
f2(max(x))

Submission status: Accepted

Solution code:
n = int(input())
x = map(int, input().split())
y = map(int, input().split())

x_y = [abs(xi - yi) for xi, yi in zip(x, y)]
p1 = sum(x_y)
p2 = sum(i ** 2 for i in x_y) ** (1 / 2)
p3 = sum(i ** 3 for i in x_y) ** (1 / 3)
p_inf = max(x_y)

print(p1)
print(p2)
print(p3)
print(p_inf)

Submission status: Accepted

Solution code:
import math
input()
vx = [int(i) for i in input().split()]
vy = [int(i) for i in input().split()]

Dxy = []
for p in range(1,4):
    Dxy.append(sum([abs(vx[i] - vy[i]) ** p for i in range(len(vx))]) ** (1 / p))

Dxy.append(max([abs(vx[i] - vy[i]) for i in range(len(vx))]))
for res in Dxy:
    print('{0:.5f}'.format(res))

Submission status: Accepted

Solution code:
import math

input()
vx = [int(i) for i in input().split()]
vy = [int(i) for i in input().split()]

Dxy = []
for p in range(1,4):
    Dxy.append(sum([abs(vx[i] - vy[i]) ** p for i in range(len(vx))]) ** (1/p))

Dxy.append(max([abs(vx[i] - vy[i]) for i in range(len(vx))]))
for res in Dxy:
    print("{0:.5f}".format(res))

Submission status: Accepted

Solution code:
import math

input()

vx = [int(i) for i in input().split()]
vy = [int(i) for i in input().split()]

Dxy = []
for p in range(1, 4):
    Dxy.append(sum([abs(vx[i] - vy[i]) ** p for i in range(len(vx))]) ** (1 / p))

Dxy.append(max([abs(vx[i] - vy[i]) for i in range(len(vx))]))

for res in Dxy:
    print('{0:5f}'.format(res))

Submission status: Accepted

Solution code:
from math import *

n = input()
x = map(float, raw_input().split())
y = map(float, raw_input().split())

for i in range(1,4):
    temp = 0
    for j in range(n):
        temp += fabs(x[j]-y[j])**i
    print pow(temp,1.0/i)

d = 0
for i in range(n):
    if(fabs(x[i]-y[i]) > d):
        d = fabs(x[i]-y[i])
print d

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline())
x = [float(i) for i in sys.stdin.readline().split()]
y = [float(i) for i in sys.stdin.readline().split()]
d = [abs(x_i - y_i) for (x_i, y_i) in zip(x, y)]

# p = 1 ~ 3
for p in range(1, 4):
    print(sum([a ** p for a in d]) ** (1/p))

# p = INFINITE
print(max(d))

Submission status: Accepted

Solution code:
def dist(x,y,p):
    tmp = 0
    for k,j in zip(x,y):
        k,j = sorted((k,j))
        tmp += (j-k)**p
    print tmp**(1/float(p))

def cheb(x,y):
    maxjk = None
    for k,j in zip(x,y):
        k,j = sorted((k,j))
        if maxjk is None:
            maxjk = j-k
        elif j-k > maxjk:
            maxjk = j-k
    print float(maxjk)

n = int(raw_input())
x = map(int,raw_input().split())
y = map(int,raw_input().split())

dist(x,y,1)
dist(x,y,2)
dist(x,y,3)
cheb(x,y)

Submission status: Accepted

Solution code:
import math

def minkowski(x, y, p):
    if p == 0:
        return max(map(lambda xi,yi: abs(xi-yi), x, y))
    else:
        return math.pow(sum(map(lambda xi,yi: math.pow(abs(xi-yi), p), x, y)), 1/p)

if __name__ == '__main__':
    n = int(input())
    x = [int(i) for i in input().split()]
    y = [int(i) for i in input().split()]
    print("{0:.6f}".format(minkowski(x, y, 1)))
    print("{0:.6f}".format(minkowski(x, y, 2)))
    print("{0:.6f}".format(minkowski(x, y, 3)))
    print("{0:.6f}".format(minkowski(x, y, 0)))

Submission status: Accepted

Solution code:
#coding:utf-8
#1_10_D 2015.4.14

def distance(p,vectorX,vectorY):
    d = sum([abs(vectorX[i] - vectorY[i]) ** p for i in range(len(vectorX))]) ** (1/p)
    return d

n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))
print('{:.10f}'.format(distance(1,x,y)))
print('{:.10f}'.format(distance(2,x,y)))
print('{:.10f}'.format(distance(3,x,y)))
print('{:.10f}'.format(max([abs(x[i] - y[i]) for i in range(len(x))])))

Submission status: Accepted

Solution code:
import math

def getDistance( x, y, n, p ):
	dxy = 0
	for i in range( n ):
		dxy += abs( x[i] - y[i] )**p
	return dxy**(1/float( p ) )


n = int( raw_input() )
x = [ float( i ) for i in raw_input( ).split( " " ) ]
y = [ float( i ) for i in raw_input( ).split( " " ) ]
print( getDistance( x, y, n, 1 ) )
print( getDistance( x, y, n, 2 ) )
print( getDistance( x, y, n, 3 ) )
dxy = [ 0 ]*(n+1)
for i in range( n ):
	dxy[i] = abs( x[i] - y[i] )

print( max( dxy ) )

Submission status: Accepted

Solution code:
import math

def getDistance( x, y, n, p ):
	dxy = 0
	for i in range( n ):
		dxy += abs( x[i] - y[i] )**p
	return dxy**(1/float( p ) )


n = int( input() )
x = [ float( i ) for i in input( ).split( " " ) ]
y = [ float( i ) for i in input( ).split( " " ) ]
print( getDistance( x, y, n, 1 ) )
print( getDistance( x, y, n, 2 ) )
print( getDistance( x, y, n, 3 ) )
dxy = [ 0 ]*(n+1)
for i in range( n ):
	dxy[i] = abs( x[i] - y[i] )

print( max( dxy ) )

Submission status: Accepted

Solution code:
def dist(X, Y, p):
    res = 0
    for x, y in zip(X, Y):
        res += abs(x - y) ** p
    return res ** (1.0 / p)
    
n = int(raw_input())
X = map(int, raw_input().split())
Y = map(int, raw_input().split())
for i in [1, 2, 3]:
    print dist(X, Y, i)
print max(map(lambda xy: abs(xy[0] - xy[1]), zip(X, Y)))

Submission status: Accepted

Solution code:
def dist(X, Y, p):
    return sum(abs(x - y) ** p for x, y in zip(X,Y)) ** (1.0 / p)
    
n = input()
X = map(int, raw_input().split())
Y = map(int, raw_input().split())
for i in [1, 2, 3]:
    print dist(X, Y, i)
print max(map(lambda xy: abs(xy[0] - xy[1]), zip(X, Y)))

Submission status: Accepted

Solution code:
import math
n=int(raw_input())
x=list(map(int,raw_input().split()))
y=list(map(int,raw_input().split()))
z=[0]*n
zz=[0]*n
zzz=[0]*n
for i in range(n):
	x.append(y[i])
for i in range(n):
	z[i]=math.fabs(x[i]-x[n+i])
	zz[i]=z[i]**2
	zzz[i]=z[i]**3
print sum(z)
print math.sqrt(sum(zz))
print sum(zzz)**(1/3.0)
print max(z)

Submission status: Accepted

Solution code:
n = input()
x = map(int, input().split())
y = map(int, input().split())

x_y = [abs(xi - yi) for xi, yi in zip(x, y)]
for p in range(1, 4):
    print(sum(i ** p for i in x_y) ** (1 / p))
print(max(x_y))

Submission status: Accepted

Solution code:
# coding=utf-8
from __future__ import division
from math import sqrt


def main():
    d = input()
    x = map(int, raw_input().split())
    y = map(int, raw_input().split())
    z = [x - y for x, y in zip(x, y)]
    print sum([abs(w) for w in z])
    print sqrt(sum([w ** 2 for w in z]))
    print sum([abs(w) ** 3 for w in z]) ** (1 / 3)
    print max([abs(w) for w in z])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding=utf-8
from math import sqrt


def main():
    d = input()
    x = map(int, raw_input().split())
    y = map(int, raw_input().split())
    z = [abs(x - y) for x, y in zip(x, y)]
    print sum(z)
    print sqrt(sum([w ** 2 for w in z]))
    print sum([w ** 3 for w in z]) ** (1.0 / 3)
    print max(z)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
for p in range(1, 5):
    if p < 4:
        print(sum(abs(x - y) ** p for x, y in zip(X, Y)) ** (1 / p))
    else:
        print(max(abs(x - y) for x, y in zip(X, Y)))

Submission status: Accepted

Solution code:
import math

def getDistance(n, x, y, p):
  temp = 0.0
  for i in xrange(n):
    temp += math.fabs(x[i] - y[i]) ** p
    # print "temp = " + str(temp)
  temp2 = 1.0 / p
  # print temp2
  return math.pow(temp, temp2)


n = int(raw_input())
x = map(float, raw_input().split(" "))
y = map(float, raw_input().split(" "))

for i in xrange(1, 4):
  print "{0:.6f}".format(getDistance(n, x, y, i))

max = 0.0
for i in xrange(n):
  d = math.fabs(x[i] - y[i])
  if d > max:
    max = d

print "{0:.6f}".format(max)

Submission status: Accepted

Solution code:
n=int(raw_input())
x=map(float,raw_input().split())
y=map(float,raw_input().split())
for p in [1,2,3]:
    d=sum([abs(x[i]-y[i])**p for i in xrange(len(x))])**(1.0/p)
    print '%6f' %(d)
print '%6f' %max([abs(x[i]-y[i]) for i in xrange(len(x))])

Submission status: Accepted

Solution code:
import math
n = input()
x = map(int, raw_input().split())
y = map(int, raw_input().split())
print sum([abs(x[i]-y[i]) for i in range(len(x))])
print math.sqrt(sum([(x[i] - y[i])**2 for i in range(len(x))]))
print pow(sum([abs(x[i] - y[i])**3 for i in range(len(x))]), 1/3.0)
print max([abs(x[i]-y[i]) for i in range(len(x))])

Submission status: Accepted

Solution code:
n = input()
x =map(int,raw_input().split())
y =map(int,raw_input().split())
sum_1,sum_2,sum_3  = [0,0,0]
sum_end = []
for i in range(n):
	if x[i] > y[i]:
		sum_1 += x[i]-y[i]
		sum_2 += (x[i]-y[i])**2
		sum_3 += (x[i]-y[i])**3
		sum_end.append(x[i]-y[i])
	else :
		sum_1 += y[i]-x[i]
		sum_2 += (y[i]-x[i])**2
		sum_3 += (y[i]-x[i])**3
		sum_end.append(y[i]-x[i])
print(sum_1)
print(pow(sum_2,0.5))
print(pow(sum_3,1.0/3.0))
sum_end.sort()
print(sum_end[len(sum_end)-1])

Submission status: Accepted

Solution code:
n = int(input())
x = map(int, input().split())
y = map(int, input().split())

n_vec = zip(x, y)

xy_diff = [abs(xi - yi) for xi, yi in n_vec]

Ma_d = sum(xy_diff)
Eu_d = sum([i ** 2 for i in xy_diff]) ** 0.5
Mi3_d = sum([i ** 3 for i in xy_diff]) ** (1 / 3)
Ch_d = max(xy_diff)

print("{0}\n{1}\n{2}\n{3}".format(Ma_d, Eu_d, Mi3_d, Ch_d))

Submission status: Accepted

Solution code:
import math
n = int(raw_input())
x = []
y = []
# input part
in_line = raw_input().split()
for i in range(n):
    x.append(float(in_line[i]))
in_line = raw_input().split()
for i in range(n):
    y.append(float(in_line[i]))
# output part
# p=1
d = 0.0
for i in range(n):
    d += math.fabs(x[i]-y[i])
print d
# p=2
d = 0.0
for i in range(n):
    d += math.fabs(x[i]-y[i])**2
print math.sqrt(d)
# p=3
d = 0.0
for i in range(n):
    d += math.fabs(x[i]-y[i])**3
print d**(1.0/3.0)
# p=inf
d = 0.0
for i in range(n):
    if d < math.fabs(x[i]-y[i]):
        d = math.fabs(x[i]-y[i])
print d

Submission status: Accepted

Solution code:
import math

def manhattan(xs, ys):
    return sum([abs(x-y) for x, y in zip(xs, ys)])

def euclid(xs, ys):
    return math.sqrt(sum([(x-y)**2 for x, y in zip(xs, ys)]))

def l3(xs, ys):
    return sum([abs(x-y)**3 for x, y in zip(xs, ys)])**(1/3)

def chev(xs, ys):
    return max([abs(x-y) for x, y in zip(xs, ys)])

def norm():
    n = int(input())
    xs = list(map(int, input().split()))
    ys = list(map(int, input().split()))
    print(manhattan(xs, ys))
    print(euclid(xs, ys))
    print(l3(xs, ys))
    print(chev(xs, ys))

norm()

Submission status: Accepted

Solution code:
inf = float('inf')

def distance(p,n,x,y):
    D = 0
    s = []
    for i in range(0,n):
        s.append(x[i]-y[i])
        if s[i]<0.0:
            s[i]*=-1.0
        D += s[i]**p 
        
    if p==inf:
        return max(s)
    else:
        return D**(1.0/p)

if __name__ == '__main__':
    n = int(input())
    x = map(int,raw_input().split())
    y = map(int,raw_input().split())
        
    print float(distance(1,n,x,y))
    print float(distance(2,n,x,y))
    print float(distance(3,n,x,y))
    print float(distance(inf,n,x,y))

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
from math import *
n = input()
xli=map(float,raw_input().split())
yli=map(float,raw_input().split())
md1 = 0 #minkowski distance p=1
md2 = 0 #p=2
md3 = 0 #p=3
mdinf = 0 #p=???(infinity)
for i in xrange(n):
    d = fabs(xli[i]-yli[i])
    md1 += d
    md2 += d**2
    md3 += d**3
    if d>mdinf:
        mdinf = d
md2 = md2**(1/2.0)
md3 = md3**(1/3.0)
print md1
print md2
print md3
print mdinf

Submission status: Accepted

Solution code:
n, x, y = int(input()), list(map(int, input().split())), list(map(int, input().split()))


def dist(p):
    return pow(sum(pow(abs(a - b), p) for a, b in zip(x, y)), 1.0 / p)


for i in [1, 2, 3]:
    print(dist(i))

print(max(abs(a - b) for a, b in zip(x, y)))

Submission status: Accepted

Solution code:
import math

n = int(input())
xv = [int(xi) for xi in input().split()]
yv = [int(yi) for yi in input().split()]

print(sum([abs(xi-yi) for (xi, yi) in zip(xv, yv)]))
print(math.sqrt(sum([pow(abs(xi-yi), 2) for (xi, yi) in zip(xv, yv)])))
print(math.pow(sum([pow(abs(xi-yi), 3) for (xi, yi) in zip(xv, yv)]), 1/3))
print(max([abs(xi-yi) for (xi, yi) in zip(xv, yv)]))

Submission status: Accepted

Solution code:
n = int(input())
data = list(zip([int(n) for n in input().split()], [int(n) for n in input().split()]))
dist = []
for pair in data:
    dist.append(abs(pair[0] - pair[1]))
print(sum(dist))
print(pow(sum(n ** 2 for n in dist), 1 / 2))
print(pow(sum(n ** 3 for n in dist), 1 / 3))
print(max(dist))

Submission status: Accepted

Solution code:
import math

n = input()

dis_1 = 0.0
dis_2 = 0.0
dis_3 = 0.0
dis_inf = 0.0

x = map(float, raw_input().split())
y = map(float, raw_input().split())

for i in xrange(n):
    dis_1 += abs(x[i] - y[i])
print "%.6f" % dis_1

for i in xrange(n):
    dis_2 += (x[i] - y[i])**2
print "%.6f" % math.sqrt(dis_2)

for i in xrange(n):
    dis_3 += abs(x[i] - y[i])**3
print "%.6f" % math.pow(dis_3, (1.0/3.0))

max_num = 0
for i in xrange(n):
    max_num = max(max_num, abs(x[i] - y[i]))
dis_inf = max_num
print "%.6f" % dis_inf

Submission status: Accepted

Solution code:
import math
n=int(input())
A=[]
#print(A)
for i in range(2):
	A.append(input().split())

#Manhattan=|x1-y1|+|x2-y2|+|x3-y3|
Manhattan=0
#Euclidean
Euclidean=0
#Minkowski_3
Minkowski_3=0
#Chebyshev
Chebyshev=[]
for i in range(n):	
	a=math.fabs(int(A[0][i])-int(A[1][i]))
	Manhattan+=a
	Euclidean+=a**2
	Minkowski_3+=a**3
	Chebyshev.append(a)




Euclidean=math.sqrt(Euclidean)
Minkowski_3=Minkowski_3**(1/3)
Chebyshev.sort()
Chebyshev.reverse()
print(Manhattan)
print(Euclidean)
print(Minkowski_3)
print(Chebyshev[0])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import math

n = int(raw_input())
x = map(int, raw_input().split())
y = map(int, raw_input().split())
d1 = d2 = d3 = d4 = 0.0
for i in range(n):
    d = math.fabs(x[i]-y[i])
    d1 += d
    d2 += d*d
    d3 += d*d*d
    if d4 < d:
        d4 = d
d2 = math.pow(d2, (1.0/2.0))
d3 = math.pow(d3, (1.0/3.0))

print d1
print d2
print d3
print d4

Submission status: Accepted

Solution code:
import math
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

for p in range(1, 4):
    D = []
    for count in range(n):
        D.append((math.fabs(A[count] - B[count]))**p)
    if p==1:
        di = max(D)
    d=(sum(D))**(1/p)
    print('%.06f' % d)
print('%.06f' % di)

Submission status: Accepted

Solution code:
import math

n = input()
a = map(float, raw_input().split(" "))
b = map(float, raw_input().split(" "))

p1 = sum([abs(x-y) for x, y in zip(a, b)])
p2 = math.sqrt(sum([(x - y)**2 for x, y in zip(a, b)]))
p3 = math.pow(sum([math.fabs(x - y)**3 for x, y in zip(a, b)]), (1.0/3.0))
p = max([abs(x - y) for x, y in zip(a, b)])

print "%f\n%f\n%f\n%f" % (p1, p2, p3, p)

Submission status: Accepted

Solution code:
import math

n = input()
x = [int(_) for _ in raw_input().split()]
y = [int(_) for _ in raw_input().split()]

for p in range(1,4):
    sum = 0
    for i in range(n):
        sum += math.pow(abs(x[i] - y[i]),p)
    print math.pow(sum,1/float(p))

print max([abs(float(i) - float(j)) for i,j in zip(x,y) ])

Submission status: Accepted

Solution code:
import math

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

print(sum([math.fabs((x[i] - y[i])) for i in range(n)]))
print(sum([math.fabs((x[i] - y[i]))**2 for i in range(n)])**(1/2))
print(sum([math.fabs((x[i] - y[i]))**3 for i in range(n)])**(1/3))
print(max([math.fabs((x[i] - y[i]))for i in range(n)]))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

for p in range(1, 3 + 1):
    s = sum([(pow(abs(x[i] - y[i]), p)) for i in range(n)])
    print('{0:.6f}'.format(pow(s, 1 / p)))

print('{0:.6f}'.format(max([abs(x[i] - y[i]) for i in range(n)])))

Submission status: Accepted

Solution code:
#   ????????? n ?¬?????????????????????????p ??????????????? 1???2???3????????? ?????????????????????????????¢????±???????????????°??????
import math
#   ?¬??????°n????¨??????\???????????????
n = int(input())
#   ????????????x????¨??????\???????????????
x = list(input())
x = "".join(x).split()
#   ????????????y????¨??????\???????????????
y = list(input())
y = "".join(y).split()
#   p=1,2,????????§???????????????????????????????????¢???????´?????????????
#   ?????????????????????
dxy = [0 for i in range(4)]
#   p=1???????????????????????????????????¢????¨????
for i in range(0, n):
    dxy[0] += math.fabs(int(x[i]) - int(y[i]))
#   p=2
for i in range(0, n):
    dxy[1] += (math.fabs(int(x[i]) - int(y[i]))) ** 2
dxy[1] = math.sqrt(dxy[1])
#   p=3
for i in range(0, n):
    dxy[2] += (math.fabs(int(x[i]) - int(y[i]))) ** 3
dxy[2] = math.pow(dxy[2], 1.0 / 3.0)
#   p=????????§
dxy[3] = math.fabs(int(x[0]) - int(y[0]))
for i in range(1, n):
    if dxy[3] < math.fabs(int(x[i]) - int(y[i])):
        dxy[3] = math.fabs(int(x[i]) - int(y[i]))
#   ??????
for i in range(0, 4):
    print("{0}".format(dxy[i]))