Source Code
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t;
	cin >> t;
	while (t--)
	{
		ll n;
		cin >> n;

		map<ll, ll> freq;
		ll ans = 0, x;
		for (ll i = 1; i <= n; i++)
		{
			cin >> x;
			for (ll j = 1; j * j <= x; j++)
			{
				if (x % j == 0)
				{
					if (freq[j])
						ans += (freq[j] * (n - i + 1));

					if (x / j != j && freq[x / j])
						ans += (freq[x / j] * (n - i + 1));
				}
			}

			freq[x] += i;
		}

		cout << ans << "\n";
	}

	return 0;
}
Copy
Powerful Inversions Yamanabdullah1
GNU G++17
714 ms
5.2 MB
Accepted