Source Code
#include <iostream>
#include <iomanip>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <set>
#include <cstdio>
#include <string>
#include <stack>
#include <map>
#include <sstream>

#define ll long long
#define sz(s) (int)s.size()
#define all(x) x.begin(), x.end()
using namespace std;
void AIA()

{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
void file()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
const int OO = 1e9 + 7;
const long double PI = acos(-1);
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dx4[] = {0, 1, 0, -1};
int dy[] = {1, -1, 0, 0, 1, -1, -1, 1}, dy4[] = {1, 0, -1, 0};
ll cal(ll L, ll R, ll n)
{
    return L + (n - R) + (L - 1) * (n - R);
}
int main()
{
    // Verify Your thought before Coding.
    AIA();
    file();
    map<int, vector<int>> div;
    for (int i = 1; i <= 100000; i++)
    {
        for (int j = 1; j * j <= i; j++)
        {
            if (i % j == 0)
            {
                div[i].push_back(j);
                if (i / j != j)
                    div[i].push_back(i / j);
            }
        }
    }
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<int> v(n + 1);
        for (int i = 1; i <= n; i++)
            cin >> v[i];
        map<int, ll> mp;
        ll res = 0;
        //1 2 3 4 5
        //x 2 x 2 4 -> 5 4 = 4 times,5 2 = 2 times = 6 = 4+2
        for (int i = 1; i <= n; i++)
        {
            for (auto j : div[v[i]])
                res += mp[j] * (n - i + 1);
            mp[v[i]] += i;
        }
        cout << res << "\n";
    }
}
Copy
Powerful Inversions kareemabdelrhmane
GNU G++17
805 ms
20.6 MB
Accepted