Source Code
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

map<int,ll>m;
int n;
void inc(int x, int idx){
	
	for(int i=1;i<=(int)sqrt(x);i++){
		
		if(x%i==0)
			m[i] += (n-idx);
		
		if(i*i != x && x%i==0)
			m[x/i] += (n-idx);
	}
	
}

int main() {
	
	
	int t;
	scanf("%d",&t);
	while(t--){
		
		scanf("%d",&n);
		int a[n];
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		ll ans = 0;
		
		for(int i=n-1;i>=0;i--){
			ans += (i+1)*m[a[i]];
			inc(a[i],i);
		}
		cout<<ans<<endl;
		m.clear();
	}
	
	return 0;
}
Copy
Powerful Inversions Bahaa
GNU G++17
554 ms
5.6 MB
Accepted