def sort(data):
n = len(data)
for i in range(0, n-1):
for j in range(0, n-i-1):
if data[j] > data[j+1]:
temp = data[j]
data[j] = data[j+1]
data[j+1] = temp
return(data)
T = int(input())
for _ in range(T):
N = int(input())
a_i = list(map(int, input().split()))
if N < 3:
print(0)
else:
a_i = sort(a_i)
count = 0
for i in range(N-2):
for j in range(i+1,N-1):
for k in range(j+1,N):
if (a_i[i]^2) + (a_i[j]^2) == a_i[k]^2:
count += 1
print(count)
T = int(input())
for _ in range(T):
N = int(input())
a_i = list(map(int, input().split()))
if N < 3:
print(0)
else:
a_i.sort()
count = 0
for i in range(N-2):
for j in range(i+1,N-1):
for k in range(j+1,N):
if (a_i[i]^2) + (a_i[j]^2) == a_i[k]^2:
count += 1
print(count)
數字有可能是重複的,測資有可能是 3 3 3 4 4 4 5 5 5 這種形式
可以紀錄一下數字出現的頻率,然後直接用集合去重後再 sorted()
這樣就可以過了