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

/**
 * Created by hiramekun at 20:50 on 2020-04-04.
 */
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using P = pair<ll, ll>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename K> using ump = unordered_map<T, K>;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll e5 = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--)
#define each(i, mp) for (auto &i : mp)
#define eb emplace_back
#define F first
#define S second
#define all(obj) (obj).begin(), (obj).end()

template <class T> ostream &operator<<(ostream &out, const vector<T> &list) {
  ll n = list.size();
  rep(i, n) out << list[i] << ' ';
  return out;
}

template <class T> istream &operator>>(istream &in, vector<T> &list) {
  ll n = list.size();
  rep(i, n) in >> list[i];
  return in;
}

template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &list) {
  ll n = list.size();
  rep(i, n) out << list[i] << '\\n';
  return out;
}

/* ------------- ANSWER ------------- */
/* ---------------------------------- */
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll rec(ll n, ll k, ll prev) {
  if (n >= prev)
    return prev;
  return rec(abs(n - k), k, n);
}

ll lcm(ll m, ll n) {
  if ((0 == m) || (0 == n))
    return 0;
  return ((m / gcd(m, n)) * n);
}

void solve() {
  ll n, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0 << '\\n';
  else if (n < k)
    cout << min(n, abs(n - k)) << '\\n';
  else {
    n %= k;
    cout << min(n, abs(n - k)) << '\\n';
  }
}

int main() {
#ifdef MY_DEBUG
  while (true) {
#endif
    solve();
#ifdef MY_DEBUG
  }
#endif
  return 0;
}

Error: TLE

Fixed submission:

/**
 * Created by hiramekun at 20:50 on 2020-04-04.
 */
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using P = pair<ll, ll>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename K> using ump = unordered_map<T, K>;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll e5 = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--)
#define each(i, mp) for (auto &i : mp)
#define eb emplace_back
#define F first
#define S second
#define all(obj) (obj).begin(), (obj).end()

template <class T> ostream &operator<<(ostream &out, const vector<T> &list) {
  ll n = list.size();
  rep(i, n) out << list[i] << ' ';
  return out;
}

template <class T> istream &operator>>(istream &in, vector<T> &list) {
  ll n = list.size();
  rep(i, n) in >> list[i];
  return in;
}

template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &list) {
  ll n = list.size();
  rep(i, n) out << list[i] << '\\n';
  return out;
}

/* ------------- ANSWER ------------- */
/* ---------------------------------- */
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll rec(ll n, ll k, ll prev) {
  if (n >= prev)
    return prev;
  return rec(abs(n - k), k, n);
}

ll lcm(ll m, ll n) {
  if ((0 == m) || (0 == n))
    return 0;
  return ((m / gcd(m, n)) * n);
}

void solve() {
  ll n, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0 << '\\n';
  else
    cout << rec(n, k, ll_inf) << '\\n';
}

int main() {
#ifdef MY_DEBUG
  while (true) {
#endif
    solve();
#ifdef MY_DEBUG
  }
#endif
  return 0;
}