/* -*- coding: utf-8 -*- * * 3461.cc: No.3461 Min GCD - yukicoder */ #include #include #include 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; /* 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; }