Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ld long double
#define ll long long int
#define ull unsigned long long int
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define INF 1e9

using namespace std;

/**
 * calculate GCD(greatest common divisor)
 * @param a
 * @param b
 * @return
 */
unsigned euclidean_gcd(unsigned a, unsigned b) {
  if (a < b)
    return euclidean_gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

/**
 * change minimum
 * @tparam T
 * @param a
 * @param b
 * @return bool
 */
template <class T> inline bool changeMinimum(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

/**
 * change maximum
 * @tparam T
 * @param a
 * @param b
 * @return bool
 */
template <class T> inline bool changeMaximum(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

/**
 * output answer
 * @tparam T
 * @param answer
 * @return
 */
template <typename T> T output(T answer) {
  cout << answer << endl;
  return 0;
}

/** ----from here ---------------------------------------------------------- */

int main() {

  ll N, K;
  cin >> N >> K;

  ll n = N;

  n = N % K;
  while (1) {
    //        n = abs( - K);
    if (abs(n - K) < n) {
      //            continue;
      n = abs(n - K);
      //            cout << n;
    } else {
      cout << n << endl;
      return 0;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ld long double
#define ll long long int
#define ull unsigned long long int
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define INF 1e9

using namespace std;

/**
 * calculate GCD(greatest common divisor)
 * @param a
 * @param b
 * @return
 */
unsigned euclidean_gcd(unsigned a, unsigned b) {
  if (a < b)
    return euclidean_gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

/**
 * change minimum
 * @tparam T
 * @param a
 * @param b
 * @return bool
 */
template <class T> inline bool changeMinimum(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

/**
 * change maximum
 * @tparam T
 * @param a
 * @param b
 * @return bool
 */
template <class T> inline bool changeMaximum(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

/**
 * output answer
 * @tparam T
 * @param answer
 * @return
 */
template <typename T> T output(T answer) {
  cout << answer << endl;
  return 0;
}

/** ----from here ---------------------------------------------------------- */

int main() {

  ll N, K;
  cin >> N >> K;

  ll n = N;

  while (1) {
    //        n = abs( - K);
    if (abs(n - K) < n) {
      //            continue;
      n = abs(n - K);
      //            cout << n;
    } else {
      cout << n << endl;
      return 0;
    }
  }
}