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, x;
		cin >> n;

		vector<vector<int>> pos(1e5 + 1);
		for (int i = 1; i <= n; i++)
		{
			cin >> x;
			pos[x].push_back(i);
		}

		ll ans = 0;
		for (int i = 1; i <= 1e5; i++)
		{
			if (pos[i].size())
			{
				for (int j = i; j <= 1e5; j += i)
				{
					for (int k = pos[j].size() - 1; k >= 0; k--)
					{
						if (pos[j][k] > pos[i][0])
						{
							ans += ((ll)pos[i][0] * (ll)(n - pos[j][k] + 1));
						}
						else
							break;
					}
				}
			}
		}

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

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