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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define per(i, a, n) for (ll i = (a); i > (n); i--)
#define lep(i, a, n) for (ll i = (a); i <= (n); i++)
#define pel(i, a, n) for (ll i = (a); i >= (n); i--)
#define clr(a, b) memset((a), (b), sizeof(a))
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz size()
#define endl "\\n"
#define print(X) cout << (X) << "\\n"
#define input(X) getline(cin, X)
static const int INF = 1e+9 + 7;
static const ll INFL = 1e+18 + 7;
ll n, m, l;
int p;
string s, t;
ll d[200010], dp[300][300];
char field[200][200];

int extgcd(int a, int b, int &x, int &y) {
  int p = a;
  if (b != 0) {
    p = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return p;
}

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int main() {
  while (cin >> n >> m >> l, (n + m + l)) {
    int g = gcd(n, m);
    n /= g, m /= g, l /= g;
    int x, y;
    extgcd(n, m, x, y);
    x *= l, y *= l;
    int ans_x = abs(x), ans_y = abs(y);
    // cout << "ans:" << ans_x << " " << ans_y << endl;
    lep(i, -5e+4, 5e+4) {
      int p = abs(x + m * i), q = abs(y - n * i);
      if (ans_x + ans_y > p + q ||
          (ans_x + ans_y == p + q && n * ans_x + m * ans_y > n * p + m * q)) {
        ans_x = p, ans_y = q;
      }
    }
    cout << ans_x << " " << ans_y << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define per(i, a, n) for (ll i = (a); i > (n); i--)
#define lep(i, a, n) for (ll i = (a); i <= (n); i++)
#define pel(i, a, n) for (ll i = (a); i >= (n); i--)
#define clr(a, b) memset((a), (b), sizeof(a))
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz size()
#define endl "\\n"
#define print(X) cout << (X) << "\\n"
#define input(X) getline(cin, X)
static const int INF = 1e+9 + 7;
static const ll INFL = 1e+18 + 7;
ll n, m, l;
int p;
string s, t;
ll d[200010], dp[300][300];
char field[200][200];

int main() {
  while (cin >> n >> m >> l, (n + m + l)) {
    vector<P> vec;
    bool f = false, swaped = false;
    if (n < m) {
      swap(n, m);
      swaped = true;
    }
    lep(i, 0, max(max(n, m), l)) {
      lep(j, 0, max(max(n, m), l)) {
        if (i * n == j * m + l || i * n + l == j * m || i * n + j * m == l) {
          // if(swaped)cout << j << " " << i << endl;
          // else cout << i << " " << j << endl;
          vec.pb(P(i, j));
        }
      }
      // if(f)break;
    }
    sort(all(vec));
    ll index = 0;
    ll ans = INF;
    if (vec.empty()) {
      cout << "no solution" << endl;
      continue;
    }
    rep(i, 0, vec.sz) {
      if (vec[i].first + vec[i].second < ans) {
        ans = vec[i].first + vec[i].second;
        index = i;
      }
    }
    cout << vec[index].first << " " << vec[index].second << endl;
  }
  return 0;
}