Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int main() {

    ios_base::sync_with_stdio(0);
    cout.tie(0); cout.tie(0);

    int tt;
    cin >> tt;
    while(tt--){

        int n;
        cin >> n;
        vector<int> a(n);
        for(int &x : a){
            cin >> x;
        }
        int mx = *max_element(a.begin() , a.end());
        vector<ll> cnt(mx + 1 , 0);

        ll ans = 0;
        for(int i = 0 ; i < n ; i++){
            for(ll j = 1 ; j * j <= a[i] ; j++){
                if(a[i] % j == 0){
                    ans += (n - i) * 1LL * cnt[j];
                    if(a[i] / j != j){
                        ans += (n - i) * 1LL * cnt[a[i] / j];
                    }
                }
            }
            cnt[a[i]] += i + 1;
        }

        cout << ans << '\n';
    }
    return 0;
}
Copy
Powerful Inversions Greedious
GNU G++17
723 ms
1.7 MB
Accepted