链接 :
思路 :
枚举值域
统计每一个值出现得次数到cnt数组中
枚举 i , j , ans += cnt[i]*cnt[j]*cnt[i*j] ;
时间复杂度为O(u/1+u/2+u/3+...+u/u) = O(UlogU) , 调和级数 ;
代码 :
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
const int N = 2e5 + 10 ;
const int Mod = 1e9 + 7 ;
// int xx[] = { 1,0,-1,0 };
// int yy[] = { 0,1,0,-1 };
// https://atcoder.jp/contests/abc249/tasks/abc249_d
LL qmi(LL m, LL k){LL res = 1 % Mod, t = m;while (k){if (k&1) res = res * t % Mod;t = t * t % Mod;k >>= 1;}return res;}
// 枚举值域
// 统计每一个值出现得次数到cnt数组中
// 枚举 i , j , ans += cnt[i]*cnt[j]*cnt[i*j] ;
int cnt[N] ;
inline void solve(){
int n ; cin >> n ;
int ma = 0 , ans = 0 ;
for(int i=1;i<=n;i++){
int x ; cin >> x ;
ma = max(ma,x) ;
cnt[x] ++ ;
}
for(int i=1;i<=N-1;i++){
for(int j=1;i*j<=N-1;j++){
ans += cnt[i]*cnt[j]*cnt[i*j] ;
}
}
cout << ans << endl ;
}
signed main(){
IOS
int _ = 1;
while(_ --) solve();
return 0;
}