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, vector<int>> mp;
        ll res = 0;
        mp[v[1]].push_back(1);
        for (int i = 2; i <= n; i++)
        {
            for (auto j : div[v[i]])
            {
                for (auto it : mp[j])
                    res += cal(it, i, n);
            }
            mp[v[i]].push_back(i);
        }
        cout << res << "\n";
    }
}
Copy
Powerful Inversions kareemabdelrhmane
GNU G++17
3046 ms
18.7 MB
Time Limit Exceeded