Advertisement
Dmaxiya

因数计数 参考代码

Mar 25th, 2025 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 100000 + 100;
  6. LL n;
  7. int num[maxn];
  8. LL ans;
  9. LL cnt[maxn], facCnt[maxn], multCnt[maxn];
  10.  
  11. int main() {
  12. #ifdef ExRoc
  13.     freopen("test.txt", "r", stdin);
  14. #endif
  15.     ios::sync_with_stdio(false);
  16.  
  17.     cin >> n;
  18.     for (int i = 0; i < n; ++i) {
  19.         cin >> num[i];
  20.         ++cnt[num[i]];
  21.     }
  22.     for (int i = 1; i < maxn; ++i) {
  23.         for (int j = i; j < maxn; j += i) {
  24.             multCnt[i] += cnt[j];
  25.             facCnt[j] += cnt[i];
  26.             if (i == j) {
  27.                 --multCnt[i];
  28.                 --facCnt[j];
  29.             }
  30.         }
  31.     }
  32.     for (int i = 0; i < n; ++i) {
  33.         ans += multCnt[num[i]];
  34.     }
  35.     ans *= ans;
  36.     for (int i = 0; i < n; ++i) {
  37.         int x = num[i];
  38.         ans -= multCnt[x] * multCnt[x];     // i == j
  39.         ans -= multCnt[x] * facCnt[x] * 2;  // i == l, j == k
  40.         ans -= facCnt[x] * facCnt[x];       // k == l
  41.         ans += multCnt[x];                  // i == j && k == l
  42.         ans += cnt[x] - 1;                  // i == l && j == k
  43.     }
  44.     cout << ans << endl;
  45.  
  46.     return 0;
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement