Magic Triples (Easy Version) (Codeforces 1822G1)
Tags: Math, number theory

This is the easy version of the problem. The only difference is that in this version, $$$a_i \le 10^6$$$.For a given sequence of $$$n$$$ integers $$$a$$$, a triple $$$(i, j, k)$$$ is called magic if:   $$$1 \le i, j, k \le n$$$.  $$$i$$$, $$$j$$$, $$$k$$$ are pairwise distinct.  there exists a positive integer $$$b$$$ such that $$$a_i \cdot b = a_j$$$ and $$$a_j \cdot b = a_k$$$. Kolya received a sequence of integers $$$a$$$ as a gift and now wants to count the number of magic triples for it. Help him with this task!Note that there are no constraints on the order of integers $$$i$$$, $$$j$$$ and $$$k$$$.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows.The first line of the test case contains a single integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) — the length of the sequence.The second line of the test contains $$$n$$$ integers $$$a_1, a_2, a_3, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the elements of the sequence $$$a$$$.The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each test case, output a single integer — the number of magic triples for the sequence $$$a$$$.

Note: In the first example, there are $$$6$$$ magic triples for the sequence $$$a$$$ — $$$(2, 3, 5)$$$, $$$(2, 5, 3)$$$, $$$(3, 2, 5)$$$, $$$(3, 5, 2)$$$, $$$(5, 2, 3)$$$, $$$(5, 3, 2)$$$.In the second example, there is a single magic triple for the sequence $$$a$$$ — $$$(2, 1, 3)$$$.

Sample Inputs:
7
5
1 7 7 2 7
3
6 2 18
9
1 2 3 4 5 6 7 8 9
4
1000 993 986 179
7
1 10 100 1000 10000 100000 1000000
8
1 1 2 2 4 4 8 8
9
1 1 1 2 2 2 4 4 4

Sample Outputs:
6
1
3
0
9
16
45

Solution (Python):
""""""
import sys
import time
from collections import Counter, defaultdict
DEBUG = False
s = time.time()

def get_triplets():
    triplets = []
    for b in range(2, 1001):
        a1 = 1
        a2 = b
        a3 = b * b
        while a3 <= 10 ** 6:
            triplets.append((a1, a2, a3))
            a1 += 1
            a2 += b
            a3 += b * b
    return triplets

def convert_to_dict(triplets: list):
    d_last = defaultdict(list)
    for (a, b, c) in triplets:
        d_last[c].append((a, b))
    return d_last

def get_last_to_other_dict():
    triplets = get_triplets()
    d_last = convert_to_dict(triplets)
    return d_last
LAST_TO_OTHER = get_last_to_other_dict()

def solution():
    n = int(input())
    a = sorted(list(map(int, input().split())))
    cnt = Counter(a)
    res = 0
    for (k, v) in cnt.items():
        if v >= 3:
            res += v * (v - 1) * (v - 2)
        for (a, b) in LAST_TO_OTHER[k]:
            res += v * cnt.get(a, 0) * cnt.get(b, 0)
    print(res)
if DEBUG:
    sys.stdin = open('G-test.txt')
t = int(input())
for _ in range(t):
    try:
        solution()
    except Exception as e:
        print(e)
        raise e
f = time.time()
if DEBUG:
    print(f'Time = {f - s}')

Solution (Python):
""""""
import sys
from collections import Counter, defaultdict

def get_triplets():
    triplets = []
    for b in range(2, 1001):
        a1 = 1
        a2 = b
        a3 = b * b
        while a3 <= 10 ** 6:
            triplets.append((a1, a2, a3))
            a1 += 1
            a2 += b
            a3 += b * b
    return triplets

def convert_to_dict(triplets: list):
    d_middle = defaultdict(list)
    d_last = defaultdict(list)
    for (a, b, c) in triplets:
        d_middle[b].append((a, c))
        d_last[c].append((a, b))
    return (d_middle, d_last)

def get_last_to_other_dict():
    triplets = get_triplets()
    (_, d_last) = convert_to_dict(triplets)
    return d_last
LAST_TO_OTHER = get_last_to_other_dict()

def solution():
    n = int(input())
    a = sorted(list(map(int, input().split())))
    cnt = Counter(a)
    res = 0
    for (k, v) in cnt.items():
        if v >= 3:
            res += v * (v - 1) * (v - 2)
        for (a, b) in LAST_TO_OTHER[k]:
            res += v * cnt.get(a, 0) * cnt.get(b, 0)
    print(res)
t = int(input())
for _ in range(t):
    try:
        solution()
    except Exception as e:
        print(e)
        raise e

Solution (Python):
def main():
    cnts = [0] * 1000001
    t = int(input())
    allans = []
    for _ in range(t):
        n = int(input())
        a = readIntArr()
        a.sort()
        for x in a:
            cnts[x] += 1
        ans = 0
        for x in set(a):
            c = cnts[x]
            if c >= 3:
                ans += c * (c - 1) * (c - 2)
            for m in range(2, 1001):
                y = x * m
                z = y * m
                if z > 1000000:
                    break
                ans += cnts[x] * cnts[y] * cnts[z]
        allans.append(ans)
        for x in a:
            cnts[x] = 0
    multiLineArrayPrint(allans)
    return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
    return [int(x) for x in input().split()]

def makeArr(defaultValFactory, dimensionArr):
    dv = defaultValFactory
    da = dimensionArr
    if len(da) == 1:
        return [dv() for _ in range(da[0])]
    else:
        return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(a, b):
    print('? {} {}'.format(a, b))
    sys.stdout.flush()
    return int(input())

def answerInteractive(ans):
    print('! {}'.format(ans))
    sys.stdout.flush()
inf = float('inf')
from math import gcd, floor, ceil
import math
for _abc in range(1):
    main()