思路:
质因子分解可以顺着分解,也可以逆着分解
即找到每一个数字的倍数,再找到每一个数字的因数
const int N = 5e5+10;
vector<int> ff[N];
vector<int> f[N];
vector<int> g[N];void solve(){int n;cin>>n;vector<int> nums(n+1,0);ll ans = 0;for(int i = 1;i<=n;i++){cin>>nums[i];}for(int i = 1;i<=n;i++){int w = gcd(i,nums[i]);int x = i/w,y = nums[i]/w;//cout<<x<<" "<<y<<" eeeeee"<<endl;if(nums[i]%i == 0) ans--;f[x].emplace_back(y);g[y].emplace_back(x);}vector<int>cnt(n+1,0);for(int i = 1;i<=n;i++){int m = f[i].size();if(m == 0) continue;sort(f[i].begin(),f[i].end());for(int j = i;j<=n;j+=i){for(auto x : g[j]){cnt[x]++;}}for(int j = 0;j<m;j++){int k = 1;while(j<m-1 && f[i][j] == f[i][j+1]){j++;k++;}int y = f[i][j];ll sum = 0;for(auto son : ff[y]){sum += cnt[son];}sum *= k;ans += sum;}for(int j = i;j<=n;j+=i){for(auto x : g[j]){cnt[x]--;}}}for(int i = 1;i<=n;i++){f[i].clear();g[i].clear();}cout<<ans/2<<endl;
}