Source Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int a[1000000];
vector<int> divs[1000000];
map<int, ll> ans;
int main() {
    ios_base::sync_with_stdio(0);
    for (int i=1;i<=100000;i++) {
        for (int j=i;j<=100000;j+=i) {
            divs[j].push_back(i);
        }
    }
    int t;
    cin>>t;
    while(t--) {
        ans.clear();
        int n;
        cin>>n;
        ll ret = 0;
        for (int i=1;i<=n;i++){
            cin>>a[i];
            for (auto x:divs[a[i]]){
                auto it = ans.find(x);
                if (it!=ans.end()) {
                    ret += (it->second) * (n-i+1LL);
                }
            }
            ans[a[i]] += i;
        }
        cout<<ret<<endl;
    }
}
Copy
Powerful Inversions RedNextCentury
GNU G++17
275 ms
36.4 MB
Accepted