結果

問題 No.3461 Min GCD
コンテスト
ユーザー tnakao0123
提出日時 2026-03-01 15:31:51
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 1,593 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,149 ms
コンパイル使用メモリ 76,132 KB
実行使用メモリ 60,888 KB
最終ジャッジ日時 2026-03-01 15:31:55
合計ジャッジ時間 2,684 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/* -*- coding: utf-8 -*-
 *
 * 3461.cc:  No.3461 Min GCD - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_A = 100000;
const int INF = 1 << 30;

/* typedef */

using ll = long long;
using vi = vector<int>;

/* global variables */

int as[MAX_N], bs[MAX_N];
vi dvs[MAX_A + 1], mncs[MAX_N];

/* subroutines */

vi &divisors(int a) {
  if (! dvs[a].empty()) return dvs[a];
  vi &v = dvs[a];
  for (int p = 1; p * p <= a; p++)
    if (a % p == 0) {
      v.push_back(p);
      int q = a / p;
      if (q != p) v.push_back(q);
    }
  sort(v.begin(), v.end());
  return v;
}

ll calc(int n, int x) {
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    auto &dv = dvs[as[i]];
    int j = lower_bound(dv.begin(), dv.end(), x) - dv.begin();
    sum += mncs[i][j];
  }
  return sum;
}

/* main */

int main() {
  int n;
  ll k;
  scanf("%d%lld", &n, &k);
  for (int i = 0; i < n; i++) scanf("%d", as + i);
  for (int i = 0; i < n; i++) scanf("%d", bs + i);

  for (int i = 0; i < n; i++) {
    auto &dv = divisors(as[i]);
    int l = dv.size();
    mncs[i].resize(l + 1);
    mncs[i][l] = INF;
    for (int j = l - 1; j >= 0; j--) {
      int c = ((bs[i] + dv[j] - 1) / dv[j]) * dv[j] - bs[i];
      mncs[i][j] = min(mncs[i][j + 1], c);
    }
    //for (auto c: mncs[i]) printf(" %d", c); putchar('\n');
  }

  int x0 = 1, x1 = *min_element(as, as + n) + 1;
  while (x0 + 1 < x1) {
    int x = (x0 + x1) / 2;
    if (calc(n, x) <= k) x0 = x;
    else x1 = x;
  }

  printf("%d\n", x0);
  
  return 0;
}

0