orazma

d.cpp

May 23rd, 2026
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define pb push_back
  5. #define mp make_pair
  6. #define ll long long
  7.  
  8. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  9.  
  10. using namespace std;
  11. const int INF = 1e9 + 1;
  12. const int MOD = 1e9 + 7;
  13. const int MAXN = 1e6 + 10;
  14. int cnt[MAXN], a[MAXN], s[MAXN], kk[MAXN];
  15. void solve(){
  16.  
  17.     int n;
  18.     cin >> n;
  19.  
  20.     for (int i = 1; i <= n; i++){
  21.         cnt[i] = 0;
  22.         s[i] = 0;
  23.         kk[i] = 0;
  24.     }
  25.  
  26.     for (int i = 1; i <= n; i++){
  27.         cin >> a[i];
  28.         cnt[a[i]]++;
  29.     }
  30.     sort(a + 1, a + n + 1);
  31.     int c = 0;/*
  32.     for (int i = 1; i <= n; i++){
  33.         for (int j = i + 1; j <= n; j++){
  34.             for (int k = 1; k <= n; k++){
  35.                 if (a[i] % a[k] == 0 && a[j] % a[k] == 0){
  36.                     c++;
  37.                     cout << "@@@ c = " << c << ": " << a[i] << ' ' << a[j] << ' ' << a[k] << endl;
  38.                     break;
  39.                 }
  40.             }
  41.         }
  42.     }*/
  43.  
  44.     ll ans = n * (n - 1LL) / 2;
  45.     ll ans2 = 0;
  46.     //cout << "ans = " << ans << endl;
  47.     for (int i = 1; i <= n; i++){
  48.         if (cnt[i] == 0 && kk[i] == 0){
  49.             continue;
  50.         }
  51.         ll k = 0;
  52.         for (int j = i; j <= n; j += i){
  53.             kk[j]++;
  54.             k += cnt[j];
  55.         }
  56.         //cout << i << ' ' << s[i] << ' ' << cnt[i] << ' ' << k << endl;
  57.         ans -= (1 - s[i]) * k * (k - 1) / 2;
  58.         ans2 += (1 - s[i]) * k * (k - 1) / 2;
  59.        // cout << "ans = " << ans << endl;
  60.         int tmp = (1 - s[i]);
  61.         for (int j = i; j <= n; j += i){
  62.             s[j] += tmp;
  63.         }
  64.     }
  65.     cout << ans << "\n";
  66. }
  67. /*
  68. 1
  69. 4
  70. 2 4 4 4
  71.  
  72. */
  73.  
  74. int main() {
  75.     IOS;
  76.     int t = 1;
  77.     cin >> t;
  78.  
  79.     while(t--){
  80.         solve();
  81.     }
  82.     return 0;
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment