Problem p02719 (C++)

Buggy submission (status: Runtime Error):
/*
    Author : Nishant Gupta 2.0
*/
#include <bits/stdc++.h>

#define LL long long int
#define getcx getchar_unlocked
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define MAX 100005
#define LOG_MAX 20
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL (LL(1e18))
#define chk(a) cerr << endl << #a << " : " << a << endl
#define chk2(a, b)                                                             \
  cerr << endl << #a << " : " << a << "\t" << #b << " : " << b << endl
#define chk3(a, b, c)                                                          \
  cerr << endl                                                                 \
       << #a << " : " << a << "\t" << #b << " : " << b << "\t" << #c << " : "  \
       << c << endl
#define chk4(a, b, c, d)                                                       \
  cerr << endl                                                                 \
       << #a << " : " << a << "\t" << #b << " : " << b << "\t" << #c << " : "  \
       << c << "\t" << #d << " : " << d << endl
#define rep(i, a, n) for (i = a; i < n; i++)
#define rev(i, a, n) for (i = a; i >= n; i--)
#define MSV(A, a) memset(A, a, sizeof(A))
#define rep_itr(itr, c) for (itr = (c).begin(); itr != (c).end(); itr++)

using namespace std;

inline void inp(int &n) {
  n = 0;
  int ch = getcx();
  int sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      sign = -1;
    ch = getcx();
  }
  while (ch >= '0' && ch <= '9')
    n = (n << 3) + (n << 1) + ch - '0', ch = getcx();
  n = n * sign;
}

inline void inp1(LL &n) {
  n = 0;
  LL ch = getcx();
  LL sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      sign = -1;
    ch = getcx();
  }
  while (ch >= '0' && ch <= '9')
    n = (n << 3) + (n << 1) + ch - '0', ch = getcx();
  n = n * sign;
}

void solve() {

  LL n, k;
  cin >> n >> k;
  if (n < k)
    swap(n, k);
  if (n % k == 0) {
    cout << k << endl;
    return;
  }

  LL q = (n / k) * k;
  LL ans = n - q;

  if (n % k != 0) {
    ans = min(ans, abs(((n + k - 1) / k) * k - n));
  }
  cout << ans << endl;
}

int main() {
  int t = 1;

  //~cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
/*
    Author : Nishant Gupta 2.0
*/
#include <bits/stdc++.h>

#define LL long long int
#define getcx getchar_unlocked
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define MAX 100005
#define LOG_MAX 20
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL (LL(1e18))
#define chk(a) cerr << endl << #a << " : " << a << endl
#define chk2(a, b)                                                             \
  cerr << endl << #a << " : " << a << "\t" << #b << " : " << b << endl
#define chk3(a, b, c)                                                          \
  cerr << endl                                                                 \
       << #a << " : " << a << "\t" << #b << " : " << b << "\t" << #c << " : "  \
       << c << endl
#define chk4(a, b, c, d)                                                       \
  cerr << endl                                                                 \
       << #a << " : " << a << "\t" << #b << " : " << b << "\t" << #c << " : "  \
       << c << "\t" << #d << " : " << d << endl
#define rep(i, a, n) for (i = a; i < n; i++)
#define rev(i, a, n) for (i = a; i >= n; i--)
#define MSV(A, a) memset(A, a, sizeof(A))
#define rep_itr(itr, c) for (itr = (c).begin(); itr != (c).end(); itr++)

using namespace std;

inline void inp(int &n) {
  n = 0;
  int ch = getcx();
  int sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      sign = -1;
    ch = getcx();
  }
  while (ch >= '0' && ch <= '9')
    n = (n << 3) + (n << 1) + ch - '0', ch = getcx();
  n = n * sign;
}

inline void inp1(LL &n) {
  n = 0;
  LL ch = getcx();
  LL sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      sign = -1;
    ch = getcx();
  }
  while (ch >= '0' && ch <= '9')
    n = (n << 3) + (n << 1) + ch - '0', ch = getcx();
  n = n * sign;
}

void solve() {

  LL n, k;
  cin >> n >> k;
  LL ans;
  if (n > k) {
    ans = min(n % k, abs(k - n % k));
  } else if (n < k) {
    ans = min(n, k - n);
  } else
    ans = 0;
  cout << ans << endl;
}

int main() {
  int t = 1;

  //~cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}