Advertisement
Zeinab_Hamdy

Untitled

Aug 6th, 2023
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl "\n"
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define ll long long
  8. #define ull unsigned ll
  9. #define RV  return void
  10. #define inf 2000000000
  11. #define sz(x) int(x.size())
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define Mini(x) *min_element(all(x))
  15. #define Maxi(x) *max_element(all(x))
  16. #define fixed(n) fixed << setprecision(n)
  17. #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0))
  18. #define cin(v) for (auto&i:v) cin >> i;
  19. #define cout(v) for (auto&i:v) cout << i << " ";
  20. #define clr(memo, x) memset(memo, x, sizeof memo)
  21. #define updmin(a, b) a = min(a, b)
  22. #define updmax(a, b) a = max(a, b)
  23. #define vi vector < int >
  24. #define vl vector < ll >
  25. #define vc vector < char >
  26. #define vs vector < string >
  27. #define v2i vector < vector < int > >
  28. #define v2l vector < vector < int > >
  29. #define seti set < int >
  30. #define setl set < ll >
  31. #define mapii map < int , int >
  32. #define mapll map < ll , ll >
  33. #define mapli map < ll , int >
  34. #define mapci map < char , int >
  35. #define mapsi map < string , int >
  36. #define pll pair < ll , ll >
  37. #define pii pair < int , int >
  38. #define range(l,r,x) for(int i=l ; i < r ; i+=x)
  39. #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  40. vector < string > ternary= {"NO\n" , "YES\n"};
  41.  
  42. void  Zainab(){
  43.             #ifndef ONLINE_JUDGE
  44.               freopen("input.txt", "r", stdin);  
  45.               freopen("output.txt", "w", stdout);
  46.             #endif
  47. }
  48.  
  49.  
  50. /*================================  Prblem solution  ================================ */
  51.  
  52.  
  53.  
  54. void myCode(){
  55.  
  56.  
  57. int n , m;
  58. cin >> n >> m ;
  59. vector < ll > v(n), freq(m+1 , 0) ;
  60. ll ans=0;
  61. cin(v);
  62.  
  63. for(auto& i : v){
  64.     for(int j =1 ; j * j <= i ; j++){
  65.          if( i % j ==0){
  66.             freq[j]++ ;
  67.             if( j!= i/j )
  68.                 freq[i/j]++;
  69.         }
  70.     }
  71. }
  72.  
  73.  
  74. ll rem = n -1;
  75. for(ll i =m ; i >= 1 ; i--){
  76.    
  77.     if(freq[i] > 1){
  78.          if( rem == 0 ) break;
  79.       //  cout << rem << " " << i << nl;
  80.         ans += i * min(rem , freq[i]-1);
  81.         ll temp = min(rem , freq[i]-1);
  82.         rem -= temp;
  83.         for(ll j =2 ; j * j <= i ; j++){
  84.             if(i % j ==0 ){
  85.                 freq[j]-= temp+1;
  86.                 if( j != i/j )
  87.                     freq[i/j]-=temp+1;
  88.             }
  89.         }
  90.        
  91.     }
  92.      if( rem == 0 ) break;
  93.    
  94. }
  95. //for(int i =1 ; i <= m ; i++) cout << i << " " << freq[i] << nl;
  96. // cout << nl;
  97.  
  98.  cout << ans << nl;
  99. }
  100.  
  101.  
  102.  
  103.  
  104. int main(){
  105.                                    FastCode ;
  106.                               //      Zainab() ;
  107.  
  108.     int testCase=1;
  109.         cin >> testCase ;
  110.       for(int i=1 ; i<= testCase ; i++)
  111.         myCode();
  112.        
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement