Problem - G2 - Codeforces
//小范围数
#include<bits/stdc++.h>
using namespace std;
char a[200010];
typedef pair<int,int> P;
vector<P> q;
int vis[30];
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
map<long long,int> mp;
while(t--)
{
int n;
cin>>n;
mp.clear();
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
mp[x]++;
}
map<long long,int> :: iterator it,it1;
it = mp.begin();
long long cut=0;
for(it;it!=mp.end();it++)
{
int temp = it->first;
if(it->second>=3) cut+=1ll*(it->second)*(it->second-1)*(it->second-2);
for(int j=2;j*j<=temp;j++)
{
if(temp%(j*j)==0) cut+=1ll*mp[temp]*mp[temp/j]*mp[temp/(j*j)];
}
cout<<temp<<" "<<cut<<endl;
}
cout<<cut<<endl;
}
return 0;
}
//大范围数 值域分治
#include <bits/stdc++.h>
using namespace std;
long long ans;
map<int,int> freq;
void fac(int a){
for (int i=2; i*i*i<=a; i++){
if(a%(i*i)) continue;
ans += 1ll*freq[a]*freq[a/i]*freq[a/(i*i)];
}
if(a>=1e6) return;
for (int i=1; i*i<a; i++){
if(a%i) continue;
if(a/i > 1e9/a) continue;
ans += 1ll*freq[a]*freq[i]*freq[a/i*a];
}
}
int main() {
int t; cin >> t;
while(t--){
ans = 0;
freq.clear();
set<int> arr;
int n; cin >> n;
for (int i=0; i<n; i++){
int a; cin >> a; freq[a]++;
arr.insert(a);
}
set<int> ::iterator it;
for (it=arr.begin();it!=arr.end();it++){
int x = *it;
fac(x);
ans += 1ll*freq[x]*(freq[x]-1)*(freq[x]-2);
cout<<x<<" "<<ans<<endl;
}
cout << ans << endl;
}
}