/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 11ms 1.07 MiB
#3 Accepted 12ms 1.066 MiB
#4 Accepted 12ms 1.312 MiB
#5 Accepted 13ms 1.066 MiB
#6 Accepted 15ms 1.309 MiB
#7 Accepted 18ms 1.27 MiB
#8 Accepted 39ms 1.312 MiB
#9 Accepted 123ms 1.391 MiB
#10 Accepted 285ms 2.066 MiB
#11 Accepted 460ms 3.758 MiB
#12 Accepted 462ms 3.742 MiB
#13 Accepted 471ms 3.703 MiB
#14 Accepted 459ms 3.742 MiB
#15 Accepted 459ms 3.758 MiB
#16 Accepted 462ms 3.738 MiB
#17 Accepted 469ms 1.957 MiB
#18 Accepted 473ms 1.078 MiB

Code

#include <bits/stdc++.h>
using namespace std;

#define int long long

const int mx = 1e6+9;

int val[mx];

int32_t main () {
  cin.tie(0)->sync_with_stdio(0);
  int t = 1;
  cin >> t;
  while (t--) {
      int n, x, ans = INT_MAX, tot = INT_MAX;
      cin >> n >> x;
      int a[n];
      map<int,int>m;
      vector<int>v;
      for (int i = 0; i < n; i++) {
          cin >> a[i];
      }
      for (int i = 0; i < n; i++) {
          for (int j = 1; j*j <= a[i]; j++) {
              if (a[i]%j == 0) {
                  val[j] += a[i];
                  if (!m[j]) v.push_back(j);
                  if (j != a[i]/j) {
                      val[a[i]/j] += a[i];
                      if (!m[a[i]/j]) v.push_back(a[i]/j);
                  }
                  m[j] = m[a[i]/j] = 1;
              }
          }
      }
      sort (v.begin(), v.end());
          for (int i = 0; i < v.size(); i++) {
              if (val[v[i]] < tot && v[i] <= x) {
                  ans = v[i];
                  tot = val[v[i]];
              }
              if (val[v[i]+1] < tot && v[i]+1 <= x) {
                  ans = v[i]+1;
                  tot = val[v[i]+1];
              }
          }
      cout << ans << "\n";
      for (int i = 0; i < n; i++) {
          for (int j = 1; j*j <= a[i]; j++) {
              if (a[i]%j == 0) {
                  val[j] = val[a[i]/j] = 0;
              }
          }
      }
  }
}

Information

Submit By
Type
Submission
Problem
P1039 Prince Roy, the undisputed ruler of the world
Contest
Brain Booster #3
Language
C++20 (G++ 13.2.0)
Submit At
2024-05-06 16:52:40
Judged At
2024-10-03 13:49:25
Judged By
Score
100
Total Time
473ms
Peak Memory
3.758 MiB