Ankit_132

D

Nov 26th, 2023
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll     long long
  6. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  7. #define pb     push_back
  8.  
  9. int main()
  10. {
  11.     int N = 1e5 + 10;
  12.  
  13.     vector<vector<int>> div(N);
  14.     for(int i=1; i<N; i++)
  15.     {
  16.         for(int j=i; j<N; j+=i)
  17.             div[j].pb(i);
  18.     }
  19.  
  20.     for(int i=1; i<N; i++)
  21.         sort(div[i].rbegin(), div[i].rend());
  22.  
  23.     vector<ll int> tmp(N), cnt(N);
  24.  
  25.     _test
  26.     {
  27.         int n;
  28.         cin>>n;
  29.  
  30.         vector<int> a(n);
  31.         for(auto &e: a)     cin>>e;
  32.  
  33.         sort(a.begin(), a.end());
  34.  
  35.         ll int ans = 0;
  36.         ll int rem = n;
  37.  
  38.         for(auto e: a)
  39.         {
  40.             rem--;
  41.             ll int val;
  42.  
  43.             for(auto d: div[e])
  44.             {
  45.                 val = cnt[d] - tmp[d];
  46.  
  47.                 for(auto d2: div[d])
  48.                     tmp[d2] += val;
  49.  
  50.                 ans += d*1ll*val*1ll*rem;
  51.             }
  52.  
  53.             for(auto d: div[e])
  54.             {
  55.                 for(auto d2: div[d])
  56.                     tmp[d2] = 0;
  57.             }
  58.  
  59.             for(auto d: div[e])
  60.                 cnt[d]++;
  61.         }
  62.  
  63.         for(auto e: a)
  64.         {
  65.             for(auto d: div[e])
  66.                 cnt[d] = 0;
  67.         }
  68.  
  69.         cout<<ans<<"\n";
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment