fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<int, int> ii;
  6.  
  7. const int INF = 1e9;
  8. const ll LINF = 1e18;
  9.  
  10. const int N = 2e5 + 5;
  11. const int MAX_A = 2e5 + 5;
  12.  
  13. // A_i / A_j = A_k <=> A_i = A_j * A_k
  14. // Như vậy với mỗi A_i thì ta đếm số cặp (j, k) sao cho A_j, A_k là ước của A_i và A_j * A_k = A_i
  15.  
  16. int n;
  17. int a[N];
  18. int cnt[MAX_A]; // cnt[x] = tần số x xuất hiện trong mảng a
  19.  
  20. int main() {
  21. ios::sync_with_stdio(0); cin.tie(0);
  22. cin >> n;
  23. for (int i = 1; i <= n; i++) cin >> a[i], cnt[a[i]]++;
  24.  
  25. ll ans = 0;
  26. for (int i = 1; i <= n; i++) {
  27. for (int x = 1; x * x <= a[i]; x++) {
  28. if (a[i] % x == 0) {
  29. int y = a[i] / x;
  30. if (x == y) {
  31. ans += 1ll * cnt[x] * cnt[y];
  32. }
  33. else {
  34. ans += 2ll * cnt[x] * cnt[y]; // (i, j, k) và (i, k, j)
  35. }
  36. }
  37. }
  38. }
  39.  
  40. cout << ans << '\n';
  41. }
Success #stdin #stdout 0s 5284KB
stdin
10
1 3 2 4 6 8 2 2 3 7
stdout
62