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--)
	{
		int n;
		cin >> n;

		vector<int> a(n + 1), pos(1e5 + 1, -1), freq(1e5 + 1, 0);
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			if (pos[a[i]] == -1)
				pos[a[i]] = i;
		}

		ll ans = 0;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j * j <= a[i]; j++)
			{
				if (a[i] % j == 0)
				{
					if (freq[j] > 0)
						ans += (pos[j] * (n - i + 1));

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

			freq[a[i]]++;
		}

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

	return 0;
}
Copy
Powerful Inversions Yamanabdullah1
GNU G++17
3088 ms
1.1 MB
Time Limit Exceeded