Submission #35553241


Source Code Expand

// #define NDEBUG
#include <bits/stdc++.h>

#define REP_(i, a_, b_, a, b, ...) for (int i = (a), END_##i = (b); i < END_##i; ++i)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(x) std::begin(x), std::end(x)
using Int = long long;
using Uint = unsigned long long;
using Real = long double;

template<typename T, typename U>
inline bool chmax(T &a, U b) { return a < b and ((a = b), true); }
template<typename T, typename U>
inline bool chmin(T &a, U b) { return a > b and ((a = b), true); }
template<typename T>
constexpr T kBig = std::numeric_limits<T>::max() / 2;
#if __cplusplus < 202002L
template<typename T>
inline int ssize(const T &a) { return (int) a.size(); }
#endif

struct CastInput {
  template<typename T>
  operator T() const {
    T x;
    std::cin >> x;
    assert(bool(std::cin));
    return x;
  }
  struct Sized {
    int n;
    template<typename T>
    operator T() const {
      T xs(n);
      for (auto &x: xs) {
        std::cin >> x;
        assert(bool(std::cin));
      }
      return xs;
    }
  };
  Sized operator()(int n) const { return {n}; }
} in;

template<typename Container>
std::ostream &out_seq(const Container &seq, const char *sep = " ",
                      const char *ends = "\n", std::ostream &os = std::cout) {
  const auto itl = std::begin(seq), itr = std::end(seq);
  for (auto it = itl; it != itr; ++it) {
    if (it != itl) os << sep;
    os << *it;
  }
  return os << ends;
}

template<typename T>
std::ostream &out_one(const T &x, char endc) {
  if constexpr (std::is_same<T, bool>::value) {
    return std::cout << (x ? "Yes" : "No") << endc;
  } else {
    return std::cout << x << endc;
  }
}
template<typename T>
std::ostream &out(const T &x) {
  return out_one(x, '\n');
}
template<typename T, typename... Ts>
std::ostream &out(const T &head, Ts... tail) {
  return out_one(head, ' '), out(tail...);
}

void init_(bool interactive = false) {
  std::ios::sync_with_stdio(false);
  if (not interactive) std::cin.tie(nullptr);
  std::cout << std::fixed << std::setprecision(18);
}

[[noreturn]] void exit_() {
#ifdef MY_DEBUG
  std::string _;
  assert((std::cin >> _).fail());
#endif
  std::cout.flush(), std::cerr.flush(), std::_Exit(0);
}

#ifdef MY_DEBUG
#include "debug_dump.hpp"
#include "backward.hpp"
backward::SignalHandling kSignalHandling;
#else
#define DUMP(...)
#define test_case(...)
#define cerr if(false)cerr
#endif

using namespace std;

template<class T>
T ceil_div(T x, T y) {
  assert(y != 0);
  return x / y + (((x ^ y) >= 0) and (x % y));
}

template<class T>
T floor_div(T x, T y) {
  assert(y != 0);
  return x / y - (((x ^ y) < 0) and (x % y));
}

// Returns ceil(sqrt(x)).
Int ceil_sqrt(Int x) {
  assert(x >= 0);
  if (x <= 1) return x;
  Int r = std::ceil(sqrtl((long double) x));
  while (r * r > x) --r;
  while (r * r < x) ++r;
  return r;
}

auto solve(int test_case) -> Int {
  Int A = in, B = in;
  if (B <= A) {
    return A - B;
  }
  Int M = ceil_sqrt(B);
  Int ans = B - A;
  if (A <= M) {
    for (Int x = 0; x < A; ++x) {
      Int q = ceil_div(B, A + x);
      Int y = q * (A + x) - B;
      chmin(ans, x + y);
    }
  } else {
    Int q0 = ceil_div(B, A);
    chmin(ans, A * q0 - B);
    for (Int q = 1; q <= q0; ++q) {
      Int x = ceil_div(B, q) - A;
      if (x < 0) continue;
      Int y = (A + x) * q - B;
      assert(y >= 0);
      chmin(ans, x + y);
    }
  }
  return ans;
}

int main() {
  init_();
  const int T = in;
  REP(t, T) {
    test_case(t, T);
    out(solve(t));
  }
  exit_();
}

Submission Info

Submission Time
Task B - Make Divisible
User keijak
Language C++ (GCC 9.2.1)
Score 500
Code Size 3685 Byte
Status AC
Exec Time 12 ms
Memory 3420 KiB

Compile Error

./Main.cpp: In function ‘Int solve(int)’:
./Main.cpp:121:16: warning: unused parameter ‘test_case’ [-Wunused-parameter]
  121 | auto solve(int test_case) -> Int {
      |            ~~~~^~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 24
Set Name Test Cases
Sample 00-sample-001.txt
All 00-sample-001.txt, 01-random-001.txt, 01-random-002.txt, 01-random-003.txt, 01-random-004.txt, 01-random-005.txt, 01-random-006.txt, 01-random-007.txt, 01-random-008.txt, 01-random-009.txt, 01-random-010.txt, 01-random-011.txt, 01-random-012.txt, 01-random-013.txt, 01-random-014.txt, 01-random-015.txt, 02-random-001.txt, 02-random-002.txt, 02-random-003.txt, 02-random-004.txt, 02-random-005.txt, 03-test-001.txt, 03-test-002.txt, 03-test-003.txt
Case Name Status Exec Time Memory
00-sample-001.txt AC 6 ms 3356 KiB
01-random-001.txt AC 6 ms 3372 KiB
01-random-002.txt AC 7 ms 3376 KiB
01-random-003.txt AC 6 ms 3356 KiB
01-random-004.txt AC 5 ms 3376 KiB
01-random-005.txt AC 5 ms 3372 KiB
01-random-006.txt AC 12 ms 3360 KiB
01-random-007.txt AC 11 ms 3364 KiB
01-random-008.txt AC 11 ms 3372 KiB
01-random-009.txt AC 10 ms 3412 KiB
01-random-010.txt AC 10 ms 3372 KiB
01-random-011.txt AC 2 ms 3364 KiB
01-random-012.txt AC 2 ms 3420 KiB
01-random-013.txt AC 3 ms 3376 KiB
01-random-014.txt AC 2 ms 3372 KiB
01-random-015.txt AC 3 ms 3376 KiB
02-random-001.txt AC 1 ms 3308 KiB
02-random-002.txt AC 2 ms 3376 KiB
02-random-003.txt AC 3 ms 3416 KiB
02-random-004.txt AC 2 ms 3376 KiB
02-random-005.txt AC 2 ms 3312 KiB
03-test-001.txt AC 2 ms 3364 KiB
03-test-002.txt AC 2 ms 3360 KiB
03-test-003.txt AC 2 ms 3352 KiB