alexjercan/bugnet huggingface dataset:
Problem p02470 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

// 素数判定(素数ならtrue)
bool Prime(int prime) {
  // 偶数除く
  if (prime == 2) {
    return true;
  } else if (prime % 2 == 0 || prime < 2) {
    return false;
  }

  float Asqrt = sqrtf((float)prime);

  for (int i = 3; i <= Asqrt; i += 2) {
    if (prime % i == 0) {
      return false;
    }
  }

  return true;
}

// 1からnまでのnと互いに素の個数
int Euler(int n) {
  // 最初のnを記録  素因数被り防止
  int newprime = 0;
  double ans = (double)n;

  // 少数第一位四捨五入
  auto Rounding = [](double ans) {
    if (fmod(ans, 1) >= 0.5) {
      return ans + 0.5F;
    }
    return ans;
  };

  for (int prime = 2;; prime++) {

    if (Prime(n)) {
      // 素因数被りでない
      if (newprime != n) {
        ans *= (1.0 - (1.0 / n));
        newprime = n;
      }
      // 値を返す
      return Rounding(ans);
    }

    while (true) {
      // 素数 かつ 素因数の場合
      if (Prime(prime) && n % prime == 0) {
        n /= prime;

        if (n == 1) {
          // 素因数被りでない
          if (newprime != prime) {
            ans *= (1.0 - (1.0 / prime));
            newprime = prime;
          }
          // 値を返す
          return Rounding(ans);
        }

        // 素因数被りでない
        if (newprime != prime) {
          ans *= (1.0 - (1.0 / prime));
          newprime = prime;
        }

        // 分解可能なら続ける
        continue;
      }

      // 分解不可でbreak;
      break;
    }
  }
}

int main() {
  int n;
  cin >> n;

  cout << Euler(n) << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

// 素数判定(素数ならtrue)
bool Prime(int prime) {
  // 偶数除く
  if (prime == 2) {
    return true;
  } else if (prime % 2 == 0 || prime < 2) {
    return false;
  }

  float Asqrt = sqrtf(prime);

  for (int i = 3; i <= Asqrt; i += 2) {
    if (prime % i == 0) {
      return false;
    }
  }

  return true;
}

// 1からnまでのnと互いに素の個数
int Euler(int n) {
  // 最初のnを記録
  int n1 = n;
  // 素因数の種類を記録
  vector<int> vec;

  for (int prime = 2;; prime++) {

    if (Prime(n)) {
      // まだ出てない場合
      if (vec.back() != n) {
        vec.push_back(n);
      }
      break;
    }

    while (true) {
      // 素数 かつ 素因数の場合
      if (Prime(prime) && n % prime == 0) {
        n /= prime;

        if (n == 1) {
          if (vec.back() != prime) {
            vec.push_back(prime);
          }
          goto MATH;
        }

        if (vec.back() != prime) {
          vec.push_back(prime);
        }
        // 分解可能なら続ける
        continue;
      }

      // 分解不可でbreak;
      break;
    }
  }

MATH:
  // 1/素因数
  double a, ans = 1.0;

  for (int i = 0; i < vec.size(); i++) {
    a = (double)1 / vec.at(i);
    ans = ans * (1.0 - a);
  }

  return n1 * ans;
}

int main() {
  int n;
  cin >> n;

  cout << Euler(n) << endl;

  return 0;
}
Problem p02470 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define int long long
#define mod 1000000007
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
using namespace std;
int n;
signed main() {
  cin >> n;
  int ans = n;
  if (n % 2 == 0) {
    ans /= 2;
    while (n % 2 == 0)
      n /= 2;
  }
  for (int i = 3; i <= sqrt(n); i += 2) {
    if (n % i == 0) {
      ans *= i - 1;
      ans /= i;
      while (n % i == 0)
        n /= i;
    }
  }
  if (n > 1) {
    ans *= n - 1;
    ans /= n;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
long gcd(long long a, long long b) {
  long long gccd;
  long long c = max(a, b);
  long long d = min(a, b);
  while (true) {
    if (c % d == 0) {
      gccd = d;
      return gccd;
    } else {
      c -= d;
      if (c <= d) {
        swap(c, d);
      }
    }
  }
}
int main() {
  long long n;
  cin >> n;
  long long sum = 0;
  for (int i = 1; i < n; i++) {
    if (gcd(i, n) == 1)
      sum++;
  }
  cout << sum << endl;
}
Problem p02470 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int tot(int n, int k) {
  if (k == 0)
    return 1;
  else if (k == 1)
    return n - 1;
  else
    return n * tot(n, k - 1);
}
int totient(int n, int k) {
  int ret = 1;
  for (int i = 2; i <= n; i++) {
    int cnt = 0;
    while (n % i == 0) {
      n /= i;
      cnt++;
    }
    ret *= tot(i, cnt);
  }
  return ret;
}

int main(void) {
  int n;
  cin >> n;
  cout << totient(n, 1) << endl;

  return 0;
}

// EOF

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<bool> isPrime(int n) {
  vector<bool> p(n + 1, true);
  p[0] = false;
  p[1] = false;
  for (int i = 2; i <= sqrt(n); i++) {
    if (!p[i])
      continue;
    for (int j = i + i; j <= n; j += i)
      p[j] = false;
  }
  return p;
}

int totient(int n, int k, vector<bool> p) {
  if (k == 0)
    return 1;
  if (p[n] && k == 1)
    return n - 1;
  if (p[n])
    return n * totient(n, k - 1, p);
  int ret = 1;
  for (int i = 2; i <= n; i++) {
    int cnt = 0;
    while (n % i == 0) {
      n /= i;
      cnt++;
    }
    ret *= totient(i, cnt, p);
  }
  return ret;
}

int main(void) {
  int n;
  cin >> n;
  cout << totient(n, 1, isPrime(n)) << endl;

  return 0;
}

// EOF
Problem p02470 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 ********************************************************************************
 *  Author: ThoseBygones
 *  Version: V1.0
 *  Date: 2018-07-06
 *  Subject: ACM-ICPC
 *  Language: C/C++11
 *  OJ: AOJ
 *  Algorithm:
 ********************************************************************************
 *  Algo-Description:
 ********************************************************************************
 */

// #include <bits/stdc++.h>  //For CodeForces only

// #pragma comment(linker,"/STACK:102400000,102400000")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
template <class T> inline T sqr(T x) { return x * x; }
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
typedef pair<LL, LL> PLL;
typedef pair<LL, int> PLI;
typedef pair<LD, LD> PDD;
#define MP make_pair
#define PB push_back
#define sz(x) ((int)(x).size())
#define clr(ar, val) memset(ar, val, sizeof(ar))
#define istr stringstream
#define FOR(i, n) for (int i = 0; i < (n); ++i)
#define forIt(mp, it)                                                          \\
  for (__typeof(mp.begin()) it = mp.begin(); it != mp.end(); it++)
const double EPS = 1e-6;
const int INF = 0x3fffffff;
const LL LINF = INF * 1ll * INF;
const double PI = acos(-1.0);

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define lowbit(u) (u & (-u))

int eulerPhi(int n) {
  int ans = n;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ans -= ans / i;
      while (n % i == 0)
        n /= i;
    }
  }
  if (n > 1)
    ans -= ans / n;
  return ans;
}

int main() {
  int n;
  scanf("%d", &n);
  printf("%d\\n", eulerPhi(n));
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int euler(int a, int b) {
  if (gcd(a, b) == 1)
    return 1;
  else
    return 0;
}

int main() {
  int n, cnt = 0;
  cin >> n;

  for (int i = 1; i <= n; i++)
    if (euler(n, i))
      cnt++;
  cout << cnt << endl;
  return 0;
}
Problem p02470 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MOD 1000000007
#define EPS 1e-9

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct edge {
  int from, to;
  ll cost;
};

int not_prime[31625];

vector<ll> factorize(ll n) {
  vector<ll> ret;
  ll i = 2, rt = sqrt(n);
  while (n != 1 && i <= rt) {
    if (not_prime[i]) {
      i++;
      continue;
    }
    ll j = i;
    while (j <= rt) {
      not_prime[j] = 1;
      j += i;
    }

    while (n % i == 0) {
      n /= i;
      ret.push_back(i);
    }

    i++;
  }
  if (n != 1) {
    ret.push_back(n);
  }
  return ret;
}

ll euler_phi_function(ll n) {
  vector<ll> facts = factorize(n);
  facts.erase(unique(all(facts)), facts.end());

  ll ret = n;
  for (ll fact : facts) {
    ret *= (fact - 1);
    ret /= fact;
  }
  return ret;
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  cout << euler_phi_function(n) << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MOD 1000000007
#define EPS 1e-9

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct edge {
  int from, to;
  ll cost;
};

int not_prime[31625];

vector<ll> factorize(ll n) {
  vector<ll> ret;
  int i = 2, rt = sqrt(n);
  while (n != 1 && i <= rt) {
    if (not_prime[i]) {
      i++;
      continue;
    }
    int j = i;
    while (j <= rt) {
      not_prime[j] = 1;
      j += i;
    }

    while (n % i == 0) {
      n /= i;
      ret.push_back(i);
    }

    i++;
  }
  if (n != 1) {
    ret.push_back(n);
  }
  return ret;
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  set<ll> fact;
  for (ll x : factorize(n)) {
    fact.insert(x);
  }
  vector<ll> ans(n + 1, 0);
  for (ll x : fact) {
    ll i = x;
    while (i <= n) {
      ans[i] = 1;
      i += x;
    }
  }
  cout << n - accumulate(all(ans), 0) << endl;
}
Problem p02470 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

struct Phi {
  Phi(const int &p = 0) : prime(p), exp(0){};
  int prime, exp;
  void operator++() { ++exp; }
  int get_phi() { return (prime - 1) * exponent(prime, exp - 1); }
  int exponent(const int &p, const int &e) {
    if (e == 0) {
      return 1;
    } else if (e == 1) {
      return p;
    } else {
      auto temp = exponent(p, e / 2);
      return temp * temp * exponent(p, e % 2);
    }
  }
};
int phi(int n, int p) {
  if (n == 1) {
    return 1;
  } else if (n % p == 0) {
    Phi ph(p);
    while ((n % p) == 0) {
      ++ph;
      n = n / p;
    }
    return phi(n, p + 2) * ph.get_phi();
  } else {
    while (n % p != 0)
      p += 2;
    return phi(n, p);
  }
}
int phi2(int n) {
  if ((n & 1) == 0) {
    Phi ph(2);
    while ((n & 1) == 0) {
      ++ph;
      n = n >> 1;
    }
    return phi(n, 3) * ph.get_phi();
  } else {
    return phi(n, 3);
  }
}
int main() {
  int n;
  std::cin >> n;
  std::cout << phi2(n) << std::endl;
}

Error: 0

Fixed submission:

#include <iostream>

struct Phi {
  Phi(const int &p = 0) : prime(p), exp(0){};
  int prime, exp;
  void operator++() { ++exp; }
  int get_phi() { return (prime - 1) * exponent(prime, exp - 1); }
  int exponent(const int &p, const int &e) {
    if (e == 0) {
      return 1;
    } else if (e == 1) {
      return p;
    } else {
      auto temp = exponent(p, e / 2);
      return temp * temp * exponent(p, e % 2);
    }
  }
};
int phi(int n, const int &p) {
  if (n == 1) {
    return 1;
  } else if (n % p == 0) {
    Phi ph(p);
    while ((n % p) == 0) {
      ++ph;
      n = n / p;
    }
    return phi(n, p + 2) * ph.get_phi();
  } else {
    return phi(n, p + 2);
  }
}
int phi2(int n) {
  if ((n & 1) == 0) {
    Phi ph(2);
    while ((n & 1) == 0) {
      ++ph;
      n = n >> 1;
    }
    return phi(n, 3) * ph.get_phi();
  } else {
    return phi(n, 3);
  }
}
int main() {
  int n;
  std::cin >> n;
  std::cout << phi2(n) << std::endl;
}
Problem p02470 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int eulerPhi(int);

int main(void) {

  int ans = 0;
  unsigned n;
  cin >> n;
  cout << eulerPhi(n) << endl;
  return 0;
}

int eulerPhi(int n) {

  int ans = n;
  for (int i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      ans -= ans / i;
      while (n % i == 0)
        n /= i;
    }
  }
  if (n > 1)
    ans -= ans / n;
  return ans;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int gcd(int, int);

int main(void) {

  int ans = 0;
  unsigned n;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    if (gcd(n, i) == 1) {
      ++ans;
    }
  }
  cout << ans << endl;

  return 0;
}

int gcd(int t0, int t1) {

  if (t0 < t1) {
    swap(t0, t1);
  }
  while (t1 > 0) {
    int temp = t0 % t1;
    t0 = t1;
    t1 = temp;
  }
  // cout << "gcd : " << t0 << endl;
  return t0;
}
Problem p02470 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int phi(int n) {
  int ans = n;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ans = ans / i * (i - 1);
      while (n % i == 0) {
        n /= i;
      }
    }
  }

  if (n > 1) {
    ans = ans / n * (n - 1);
  }
  return ans;
}

int main() {
  int n;
  int cnt = 0;

  cin >> n;
  cout << phi(n) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int gcd(int a, int b) {
  if (b == 0) {
    return a;
  } else {
    return (gcd(b, a % b));
  }
}

int main() {
  int n;
  int cnt = 0;

  cin >> n;
  FOR(i, 2, n) {
    if (gcd(n, i) == 1) {
      cnt++;
    }
  }

  cout << cnt + 1 << endl;

  return 0;
}
Problem p02470 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  unsigned long long int phi;
  unsigned long long int n;
  unsigned long long int i;

  cin >> n;
  phi = n;

  i = 2;
  while (i <= n) {
    if (n % i == 0) {
      n = n / i;
      phi = phi * (i - 1) / i;

      while (n % i == 0) {
        n = n / i;
      }
    }
    ++i;
  }

  cout << phi << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long phi;
  long n;
  long temp;
  long i;

  cin >> n;
  temp = n;
  phi = n;

  i = 2;
  while (i <= temp) {
    if (n % i == 0) {
      n = n / i;
      phi = phi * (i - 1) / i;

      while (n % i == 0) {
        n = n / i;
      }
    }
    ++i;
  }

  cout << phi << endl;

  return 0;
}
Problem p02470 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (a == 0 || b == 0)
    return max(a, b);
  else
    return gcd(b, a % b);
}

int eulerPhi(int n) {
  if (n == 0)
    return 0;
  int ans = n;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ans -= ans / i;
      while (n % i == 0)
        n /= i;
    }
  }
  if (n > 1)
    ans -= ans / n;
  return ans;
}

int main() {
  int n, cnt = 0;
  cin >> n;
  cout << eulerPhi(n) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (a == 0 || b == 0)
    return max(a, b);
  else
    return gcd(b, a % b);
}

int main() {
  int n, cnt = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    if (gcd(i, n) == 1)
      cnt++;
  cout << cnt << endl;
  return 0;
}
Problem p02471 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;

int ExtendedGCD(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  } else {
    int g = ExtendedGCD(b, a % b, x, y);
    tie(x, y) = make_tuple(y, x - a / b * y);
    return g;
  }
}

// int ExtendedGCD(int a,int b,int& x,int& y)
//{
//	x=1,y=0;
//	for(int u=0,v=1;b;){
//		ll t=a/b;
//		swap(a-=b*t,b);
//		swap(x-=u*t,u);
//		swap(y-=v*t,v);
//	}
//	return a;
// }

int main() {
  for (int a, b; cin >> a >> b && a | b;) {
    int x, y;
    for (int i = 0; i < 1000000; i++)
      ExtendedGCD(a, b, x, y);
    cout << x << ' ' << y << endl;
    ;
  }
}

Error: TLE

Fixed submission:

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

#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;

int ExtendedGCD(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  } else {
    int g = ExtendedGCD(b, a % b, x, y);
    tie(x, y) = make_tuple(y, x - a / b * y);
    return g;
  }
}

// int ExtendedGCD(int a,int b,int& x,int& y)
//{
//	x=1,y=0;
//	for(int u=0,v=1;b;){
//		ll t=a/b;
//		swap(a-=b*t,b);
//		swap(x-=u*t,u);
//		swap(y-=v*t,v);
//	}
//	return a;
// }

int main() {
  for (int a, b; cin >> a >> b && a | b;) {
    int x, y;
    for (int i = 0; i < 10000000; i++)
      ExtendedGCD(a, b, x, y);
    cout << x << ' ' << y << endl;
    ;
  }
}
Problem p02471 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void search(int a, int b, int &x, int &y) {
  int c = a % b;
  if (c == 0) {
    x = 0;
    y = 1;
  } else {
    search(b, c, y, x);
    y -= a / b * x;
  }
}

int main() {
  int a, b, x, y;
  cin >> a >> b;
  search(a, b, x, y);
  cout << x << ' ' << y << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int yaku(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return yaku(b, a % b);
  }
}

int main() {
  int a, b;
  cin >> a >> b;
  int c = yaku(a, b);
  a = a / c;
  b = b / c;
  int x, y;
  int *p = &x, *q = &y;
  if (a > b) {
    swap(a, b);
    swap(p, q);
  }

  if (a == 1) {
    x = 1;
    y = 0;
  }

  else {
    long long int bd = b;
    for (int i = 1;; i++) {
      if (bd % a == 1) {
        x = -(bd - 1) / a;
        y = i;
        break;
      } else {
        bd += b;
      }
    }
    if (2 * (y - x) > a + b) {
      x = x + b;
      y = y - a;
    }
  }

  cout << *p << " " << *q << endl;

  return 0;
}
Problem p02471 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>

using namespace std;

long long d = 0;
bool flag = 0;

long long gcd(long long a, long long b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

long long judge(long long x, long long y) { return abs(x) + abs(y); }

pair<long long, long long> calc(long long a, long long b, long long x,
                                long long y) {
  if (a * x + b * y == d)
    return pair<long long, long long>(x, y);
  if (a * x + b * y == -d)
    return pair<long long, long long>(-x, -y);
  flag ^= 1;
  long long n = a * x + b * y, tmp = 0;
  if (b % n == 0)
    return calc(b, a, y, x);
  tmp = b / n;
  x *= tmp;
  y = y * tmp - 1;
  long long k = (x + y) / (b - a);
  x -= b * k;
  y += a * k;
  return calc(b, a, y, x);
}

int main() {
  long long a = 0, b = 0;
  cin >> a >> b;
  if (a == b) {
    cout << "0 1" << endl;
    return 0;
  }
  d = gcd(a, b);
  pair<long long, long long> ans = calc(a, b, 1, 0);
  if (flag) {
    long long tmp = ans.first;
    ans.first = ans.second;
    ans.second = tmp;
  }
  long long k = (ans.first + ans.second) / (b - a);
  ans.first -= b * k;
  ans.second += a * k;

  while (judge(ans.first - b / d, ans.second + a / d) <
         judge(ans.first, ans.second)) {
    ans.first -= b / d;
    ans.second += a / d;
  }
  while (judge(ans.first + b / d, ans.second - a / d) <
         judge(ans.first, ans.second)) {
    ans.first += b / d;
    ans.second -= a / d;
  }

  cout << ans.first << " " << ans.second << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

long long d = 0;
bool flag = 0;

long long gcd(long long a, long long b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

long long judge(long long x, long long y) { return abs(x) + abs(y); }

pair<long long, long long> calc(long long a, long long b, long long x,
                                long long y) {
  if (a * x + b * y == d)
    return pair<long long, long long>(x, y);
  if (a * x + b * y == -d)
    return pair<long long, long long>(-x, -y);
  flag ^= 1;
  long long n = a * x + b * y, tmp = 0;
  if (b % n == 0)
    return calc(b, a, y, x);
  tmp = b / n;
  x *= tmp;
  y = y * tmp - 1;
  long long k = (x + y) / (b - a);
  x -= b * k;
  y += a * k;
  return calc(b, a, y, x);
}

int main() {
  long long a = 0, b = 0;
  cin >> a >> b;
  d = gcd(a, b);
  pair<long long, long long> ans = calc(a, b, 1, 0);
  if (flag) {
    long long tmp = ans.first;
    ans.first = ans.second;
    ans.second = tmp;
  }
  long long k = (ans.first + ans.second) / (b - a);
  ans.first -= b * k;
  ans.second += a * k;

  while (judge(ans.first - b / d, ans.second + a / d) <
         judge(ans.first, ans.second)) {
    ans.first -= b / d;
    ans.second += a / d;
  }
  while (judge(ans.first + b / d, ans.second - a / d) <
         judge(ans.first, ans.second)) {
    ans.first += b / d;
    ans.second -= a / d;
  }

  cout << ans.first << " " << ans.second << endl;
  return 0;
}
Problem p02471 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main() {
  int a, b, m, m1, m2;
  long x, y, x1 = 0, x2 = 1, y1 = 1, y2 = 0;
  scanf("%d %d", &a, &b);
  /*printf("a=%d,b=%d\\n",a,b);*/
  m2 = a;
  m1 = b;
  m = a % b;
  x = 1;
  y = -(a / b);
  while (m) {
    m2 = m1;
    m1 = m;
    m = m2 % m1;
    x2 = x1;
    x1 = x;
    x = x2 - (m2 / m1) * x1;
    y2 = y1;
    y1 = y;
    y = y2 - (m2 / m1) * y1;
    /*printf("%d = %d*a + %d*b\\n",m1,x1,y1);*/
  }
  printf("%d %d\\n", x1, y1);
}

Error: TLE

Fixed submission:

#include <stdio.h>
int gcm(int a, int b) {
  int m = a % b;
  while (m) {
    a = b;
    b = m;
    m = a % b;
  }
  return b;
}
int main() {
  int a, b, g;
  long int x, y;
  int tf;
  scanf("%d %d", &a, &b);
  g = gcm(a, b);
  a /= g;
  b /= g;
  /*printf("%d\\n",g);*/
  for (y = 0; y < a; y++) {
    if (!((1 - b * y) % a)) {
      x = (1 - b * y) / a;
      break;
    }
  }
  tf = 2 * (y - x) - a - b; /*(y-x)<>((x+b)-(y-a))????????¢*/
  if (tf > 0 || (tf == 0 && x > y)) {
    x += b;
    y -= a;
  }
  printf("%d %d\\n", x, y);
}