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



/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */

#include <bits/stdc++.h>
using namespace std;

const double PI = acos(-1);
typedef long long ll;
#define int ll
#define fori(n) for (int i = 0; i < int(n); i++)

#define cins(s)                                                                \\
  string s;                                                                    \\
  cin >> s;
#define cini(i)                                                                \\
  int i;                                                                       \\
  cin >> i;
#define cind(d)                                                                \\
  double d;                                                                    \\
  cin >> d;
#define cinai(a, n)                                                            \\
  vi a(n);                                                                     \\
  fori(n) { cin >> a[i]; }
#define cinas(s, n)                                                            \\
  vs s(n);                                                                     \\
  fori(n) { cin >> s[i]; }
#define cinad(a, n)                                                            \\
  vd a(n);                                                                     \\
  fori(n) { cin >> a[i]; }

typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<string> vs;

void solve() {
  cini(n);
  cini(k);
  int x = n;
  do {
    n = x;
    if (abs(n - k) <= k)
      x = abs(n - k);
    else {
      x = min(n % k, k % n);
    }
  } while (x < n && n > 0);
  cout << min(n, x) << endl;
}

signed main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  solve();
}


Error: TLE

Fixed submission:



/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */

#include <bits/stdc++.h>
using namespace std;

const double PI = acos(-1);
typedef long long ll;
#define int ll
#define fori(n) for (int i = 0; i < int(n); i++)

#define cins(s)                                                                \\
  string s;                                                                    \\
  cin >> s;
#define cini(i)                                                                \\
  int i;                                                                       \\
  cin >> i;
#define cind(d)                                                                \\
  double d;                                                                    \\
  cin >> d;
#define cinai(a, n)                                                            \\
  vi a(n);                                                                     \\
  fori(n) { cin >> a[i]; }
#define cinas(s, n)                                                            \\
  vs s(n);                                                                     \\
  fori(n) { cin >> s[i]; }
#define cinad(a, n)                                                            \\
  vd a(n);                                                                     \\
  fori(n) { cin >> a[i]; }

typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<string> vs;

void solve() {
  cini(n);
  cini(k);
  int x = n;
  do {
    n = x;
    x = abs(n - k);
  } while (x < n && n > 0);
  cout << min(n, x) << endl;
}

signed main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  solve();
}