Source Code
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define INF INT_MAX
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define nl "\n"
using namespace std;
const ll MOD  = 1e9+7;
const int mxN = 2e5+1;

int main()
{
    FAST
    //==================
    ll t; cin >> t;
    while(t--){
        ll n; cin >> n;
        map<ll,ll> m;
        ll a[n];
        for(int i=1;i<=n;i++){
            cin >> a[i];
            m[a[i]]+= i;
        }
        ll ans =0 ;
        for(int i=n ;i>0;i--){
            if(m[a[i]] > 0){
                m[a[i]]-=i;
            }
            for(int j=1;j*j<=a[i];j++){
                if(a[i]%j == 0){
                    ans+= (n-i+1)*m[j];
                    if(j*j != a[i]){
                        ans+= (n-i+1)*m[a[i]/j];
                    }
                }

            }

        }

        cout << ans << nl;
    }



}
Copy
Powerful Inversions Imam Droubi
GNU G++17
761 ms
5.9 MB
Accepted