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);
		vector<int> a(n + 1);
		int mx = 0;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			pos[a[i]].push_back(i);
			mx = max(mx, a[i]);
		}

		for (int i = 1; i <= 1e5; i++)
			reverse(pos[i].begin(), pos[i].end());

		ll ans = 0;
		for (int i = 1; i <= n; i++)
		{
			if (pos[a[i]].size())
			{
				int g = pos[a[i]][pos[a[i]].size() - 1];
				pos[a[i]].pop_back();

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

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

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