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

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define all(v) v.begin(), v.end()
#define pb push_back    
#define sz(x) (int)(x).size()

#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
  cout << vars << " = ";
  string delim = "";
  (..., (cout << delim << values, delim = ", "));
  cout << '\n';
}

const int N = 1e5 + 1;

inline void solve() {
  int n;
  cin >> n;
  vi a(n);
  map<ll, ll> ad;
  for(int i = 0; i < n; ++i) {
    cin >> a[i];
    ad[a[i]] += (i + 1);
  }
  ll ans = 0;
  for(int i = n - 1; i >= 0; --i) {
    ad[a[i]] -= (i + 1);
    for(int j = 1; j * j <= a[i]; ++j) {
      if(a[i] % j) continue;
      int d1 = j;
      int d2 = a[i] / j;
      ans += ad[d1] * (n - i);
      if(d2 != d1) {
        ans += ad[d2] * (n - i);
      }
    }
  }
  cout << ans << '\n';
  // for(int x : a) ad[x] = 0;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  // freopen("input.txt", "r", stdin);
  int T = 1;
  cin >> T;
  while(T--) {
    solve();
  }
}
Copy
Powerful Inversions noomaK
GNU G++17
551 ms
5.6 MB
Accepted