/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 8ms 960.0 KiB
#3 Accepted 9ms 840.0 KiB
#4 Accepted 9ms 832.0 KiB
#5 Accepted 9ms 836.0 KiB
#6 Accepted 9ms 856.0 KiB
#7 Accepted 9ms 840.0 KiB
#8 Accepted 10ms 956.0 KiB
#9 Accepted 10ms 992.0 KiB
#10 Accepted 14ms 1.184 MiB
#11 Accepted 23ms 2.027 MiB
#12 Accepted 23ms 2.023 MiB
#13 Accepted 21ms 2.051 MiB
#14 Accepted 26ms 2.152 MiB
#15 Accepted 28ms 2.027 MiB
#16 Accepted 30ms 2.188 MiB
#17 Accepted 152ms 1.91 MiB
#18 Accepted 927ms 1.879 MiB

Code

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
using namespace std;

void solve(int cs) {
  int n, x;
  cin >> n >> x;
  
  int mx = 0;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }

  if (x == 1) {
    cout << "1\n";
    return;
  }

  unordered_map<int, int> seen;
  for (auto &it : a) seen[it] += 1;
  pair<int64_t,int> res = {1e13,-1};
  auto check = [&](int k) -> void {
    int cnt = 0;
    int64_t sum = 0;
    for (int i = k; i <= mx; i += k) {
      if (seen[i] > 0) {
        sum += i * seen[i];
      }
    }
    if (res.first > sum) {
      res.first = sum;
      res.second = k;
    }
  };

  for (int i = 1; i <= x; i++) {
    check(i);
  }
  cout << res.second << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  cin >> tc;
  for (int cs = 1; cs <= tc; cs++) {
    solve(cs);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1039 Prince Roy, the undisputed ruler of the world
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-11 10:37:02
Judged At
2024-11-11 03:27:39
Judged By
Score
100
Total Time
927ms
Peak Memory
2.188 MiB