alexjercan/bugnet huggingface dataset:
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int p, q;
  cin >> p >> q;
  p = q / __gcd(p, q);
  vector<int> v;
  // cout<<p<<endl;
  for (int i = 2; i <= p; i++) {
    if (i * i > p) {
      v.push_back(p);
      break;
    }
    if (p % i)
      continue;
    v.push_back(i);
    while (p % i == 0)
      p /= i;
  }
  int ans = 1;
  for (int i = 0; i < v.size(); i++) {
    ans *= v[i];
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int p, q;
  cin >> p >> q;
  p = q / __gcd(p, q);
  int f = 0;
  for (int i = 2; i * 1 < p; i++) {
    if (p % i)
      continue;
    f = i;
    break;
  }
  if (f == 0) {
    cout << p << endl;
    return 0;
  }
  int t = p;
  while (t % f == 0 && t != 1)
    t /= f;
  if (t == 1)
    cout << f << endl;
  else
    cout << p << endl;
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, p, t, ans = 1, i = 2;
  cin >> q >> p;
  t = p;
  p /= __gcd(q, p);
  for (; i * i <= t; i++)
    if (p % i == 0) {
      if (ans % i)
        ans *= i;
      p /= i--;
    }
  cout << ans * p << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, p, t, ans = 1, i, j, c;
  cin >> q >> p;
  t = p;
  p /= __gcd(q, p);
  for (i = 2; i <= t; i++) {
    if (p % i == 0) {
      p /= i;
      ans *= i;
      i--;
    }
  }
  cout << ans << endl;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int p, q;
int gcd(int x, int y) {
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
bool prime(int x) {
  bool f = true;
  for (int i = 2; i * i <= x; ++i)
    if (x % i == 0)
      f = false;
  return f;
}

int main() {
  //	cin.tie(0);
  //	ios::sync_with_stdio(false);
  cin >> p >> q;
  int g = gcd(p, q);
  q /= g;
  //	cout << p / g << " " << q << "\\n";
  vector<int> a;
  if (prime(q)) {
    cout << q << "\\n";
    return 0;
  }
  for (int i = 2; i <= q; ++i) {
    if (q % i == 0) {
      int t = 1;
      q /= i;
      while (q % i == 0) {
        ++t;
        q /= i;
      }
      a.push_back(i);
    }
  }
  int ans = 1;
  for (int i = 0; i < a.size(); ++i)
    ans *= a[i];
  cout << ans << "\\n";
}


Error: TLE

Fixed submission:

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

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

typedef pair<long long, long long> P;
void factor(long long n, vector<P> &v) {
  vector<long long> p;
  for (int i = 2; i <= n; ++i) {
    bool f = true;
    for (int j = 0; j < p.size(); ++j)
      if (i % p[j] == 0) {
        f = false;
        break;
      }
    if (f)
      p.push_back(i);
  }
  for (int i = 0; i < p.size(); ++i) {
    int t = 0;
    while (n % p[i] == 0) {
      n /= p[i];
      ++t;
    }
    v.push_back(P(p[i], t));
    if (n == 1)
      break;
  }
}

int main() {
  //	cin.tie(0);
  //	ios::sync_with_stdio(false);
  cin >> p >> q;
  long long g = gcd(p, q);
  p /= g;
  q /= g;
  //	cout << q << "\\n";
  vector<P> v;
  factor(q, v);
  long long mi = q;
  long long ma = 1;
  for (int i = 0; i < v.size(); ++i) {
    mi = min(mi, v[i].second);
    ma = max(ma, v[i].second);
  }
  if (mi == ma) {
    long long t = 1;
    for (int i = 0; i < v.size(); ++i)
      t *= v[i].first;
    q = t;
  }
  cout << q << "\\n";
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
  int p, q;
  cin >> p >> q;
  q /= gcd(p, q);
  long long int x = 1;
  for (int i = 2; i * i <= q; i++) {
    if (q % i == 0) {
      x *= i;
      while (q % i == 0)
        q /= i;
    }
  }
  cout << x * q << endl;
}

Error: TLE

Fixed submission:

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

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
  int p, q;
  cin >> p >> q;
  q /= gcd(p, q);
  for (int i = 2;; i++) {
    double x = log(q) / log(i);
    if (ceil(x) == floor(x)) {
      cout << i << endl;
      break;
    }
  }
}
Problem p01809 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)

using namespace std;

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

int main() {
  int p, q;
  cin >> p >> q;

  q = q / gcd(p, q); /*??????????´??????????????????????*/

  // int qq = q/gcd(p,q);
  int qq = q;
  long long ans = 1;
  for (int j = 2; j * j <= qq; j++) {
    if (qq % j == 0) {
      ans *= j;
      while (qq % j == 0) {
        qq /= j;
      }
    }
  }
  if (qq != 1)
    ans *= qq;

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)

using namespace std;

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

vector<bool> primes;
void make_primes(int n) {
  primes.resize(n + 1, true);
  primes[0] = primes[1] = false;
  REP(i, 2, sqrt(n)) {
    if (primes[i]) {
      for (int j = 0; i * (j + 2) < n; j++)
        primes[i * (j + 2)] = false;
    }
  }
}

int main() {
  int p, q;
  cin >> p >> q;

  q = q / gcd(p, q); /*??????????´??????????????????????*/

  vector<int> sie;
  make_primes(q);
  REP(i, 0, primes.size()) {
    if (primes[i])
      sie.push_back(i);
  }

  set<int> s;
  int t = 0;
  while (1) {
    if (q % sie[t] == 0) {
      s.insert(sie[t]);
      q /= sie[t];
    } else {
      t++;
    }
    if (q == 1) {
      break;
    }
  }

  long long ans = 1;
  set<int>::iterator it = s.begin();
  while (it != s.end()) {
    ans *= (*it);
    it++;
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set;
using std::setprecision;
using std::stack;
using std::string;
using std::vector;

typedef vector<int> VI;
typedef vector<vector<int>> VII;
typedef vector<string> VS;
typedef pair<int, int> PII;

int gcd(int p, int q) {
  int temp;
  if (q == 0) {
    return p;
  } else {
    temp = p % q;
    p = q;
    q = temp;
    return gcd(p, q);
  }
  return 0;
}

int main(void) {
  int p;
  int q;
  cin >> p >> q;
  q /= gcd(p, q);

  int ans = 1;

  for (int i = 2; i * i <= q; i++) {
    if (q == 1) {
      break;
    }
    if (q % i != 0) {
      continue;
    }
    while (q % i == 0) {
      q /= i;
    }
    ans *= i;
  }
  cout << ans * q << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set;
using std::setprecision;
using std::stack;
using std::string;
using std::vector;

typedef vector<int> VI;
typedef vector<vector<int>> VII;
typedef vector<string> VS;
typedef pair<int, int> PII;

int gcd(int p, int q) {
  int temp;
  if (q == 0) {
    return p;
  } else {
    temp = p % q;
    p = q;
    q = temp;
    return gcd(p, q);
  }
  return 0;
}

bool prime_check(int number) {
  if (number == 2) {
    return true;
  } else if (number % 2 == 0) {
    return false;
  }
  for (int i = 3; i * i <= number; i++) {
    if (number % i == 0) {
      return false;
    }
  }
  return true;
}

int main(void) {
  int p;
  int q;
  cin >> p >> q;
  q /= gcd(p, q);
  vector<bool> table(1e09, true);
  for (int i = 2; i * i < 1e09; i++) {
    if (table[i] == true) {
      int times = 2;
      while (i * times < 1e09) {
        table[i * times] = false;
        times++;
      }
    }
  }

  int ans = 1;
  for (int i = 2; i <= q; i++) {
    if (table[i] == true && q % i == 0) {
      ans *= i;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<i_i>
#define all(a) (a).begin(), (a).end()
#define Int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%lld", &x);

// int dxy[5] = {0, 1, 0, -1, 0};
//  assign

int gcd(int a, int b);

signed main() {
  Int(p);
  Int(q);

  while (p && q) {
    int base = gcd(p, q);
    q /= base;
    for (int i = 2; i * i <= q; i++) {
      if (!(q % i)) {
        int ret = q / i;
        while (!(ret % i)) {
          q /= i;
          ret /= i;
        }
      }
    }
    cout << q << endl;
    p = 0;
    q = 0;
  }

  return 0;
}

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<i_i>
#define all(a) (a).begin(), (a).end()
#define Int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%lld", &x);

// int dxy[5] = {0, 1, 0, -1, 0};
//  assign

int gcd(int a, int b);

signed main() {
  Int(p);
  Int(q);

  while (p && q) {
    int base = gcd(p, q);
    q /= base;
    for (int i = 2; i * i <= q; i++) {
      if (!(q % i)) {
        int ret = q / i;
        while (!(ret % i)) {
          q /= i;
          ret /= i;
        }
      }
    }
    cout << q << endl;
    cin >> p >> q;
  }

  return 0;
}

int gcd(int a, int b) {
  if (!b) {
    return a;
  } else {
    gcd(b, a % b);
  }
}
Problem p01810 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

using ll = long long;

int main() {
  ll N, k, now = 0;
  cin >> N >> k;
  ll step;
  ll mod, div;
  ll tmp;
  for (;;) {
    N--;
    if (N == 0)
      break;

    div = now / k;
    step = div + 1;
    tmp = step * k;
    mod = now % k;
    while (now + step >= tmp) {
      step += (now + step - tmp) / k + 1;
      tmp = step * k;
    }
    now += step;
  }
  cout << now << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

using ll = long long;

int main() {
  ll N, k, now = 0;
  cin >> N >> k;
  ll step;
  ll mod, div;
  ll tmp;
  for (;;) {
    N--;
    if (N == 0)
      break;

    div = now / k;
    step = div + 1;
    tmp = step * k;
    while (now + step >= tmp) {
      step++;
      tmp += k;
    }
    now += step;
  }
  cout << now << endl;
  return 0;
}
Problem p01810 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second
#define ll long long;

const int INF = 1e9;
const long long LLINF = 1e15;

using namespace std;

long long N, k, a[5] = {0, 0, 1, 2, 1};

int main() {
  cin >> N >> k;
  long long num = 0;
  REP(i, 0, N - 1) {
    if (a[0] * k - a[1] > num || num > a[2] * k - a[3]) {
      long long inc = (k * a[0] - a[1] - num) / (1 - k);
      REP(j, 0, 5) a[j] += inc;
    }
    num += a[4];
  }
  cout << num << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second
#define ll long long;

const int INF = 1e9;
const long long LLINF = 1e15;

using namespace std;

long long N, k, a[5] = {0, 0, 1, 2, 1};

int main() {
  cin >> N >> k;
  long long num = 0;
  REP(i, 0, N - 1) {
    while (a[0] * k - a[1] > num || num > a[2] * k - a[3]) {
      REP(j, 0, 5) a[j]++;
    }
    num += a[4];
  }
  cout << num << endl;
  return 0;
}
Problem p01810 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll next(ll n, ll k) {
  ll num = n + n / (k - 1);
  for (ll i = 1;; i++) {
    if (num - (num / k) - 1 == n) {
      return num;
    }
    num++;
  }
}

int main() {
  ll N, k;
  cin >> N >> k;
  ll num = 0;
  for (ll i = 2; i <= N; i++) {
    num = next(num, k);
  }
  cout << num << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll next(ll n, ll k) {
  for (ll i = 1; i <= k; i++) {
    const ll num = n + (n + i) / (k - 1);
    if (num - num / k - 1 == n) {
      return num;
    }
  }
}

int main() {
  ll N, k;
  cin >> N >> k;
  ll num = 0;
  for (ll i = 2; i <= N; i++) {
    num = next(num, k);
  }
  cout << num << endl;
  return 0;
}
Problem p01810 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define llint long long int
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int N;
int k;

int main(void) {
  cin >> N >> k;

  llint x = 0;
  rep(i, N - 1) { x += x / (k - 1) + 1; }

  cout << x << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define llint long long int
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int N;
int k;

int check(llint x) {
  rep(i, N - 1) {
    if (x <= 0)
      return -1;
    if (x % k == 0)
      return -2;
    x -= (x / k) + 1;
  }
  if (x == 0)
    return 0;
  else
    return 1;
}

int main(void) {
  cin >> N >> k;

  int res;
  llint mid;
  llint left = 0, right = 1000000000000000001;

  while (right > left + 1) {
    mid = (left + right) / 2;
    res = check(mid);

    if (res == 0) {
      left = mid;
      break;
    } else if (res == 1)
      right = mid;
    else if (res == -1)
      left = mid + 1;
    else if (res == -2)
      right++;

    // cout << left << " " << right << endl;
  }

  cout << left << endl;
  return 0;
}
Problem p01810 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

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

  ll l = -1, r = 1000000000000000000LL;
  while (r - l > 1) {
    ll m = (l + r) / 2;

    ll rem = m;
    rep(i, n - 1) {
      ll sub = rem / k + 1;
      rem -= sub;
    }

    if (rem >= 0)
      r = m;
    else
      l = m;
  }

  cout << r << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

// 0:??????(?????????), 1:??????(?????£??????), 2:??????, 3:????????????
int check(int n, int k, ll m) {
  rep(i, n - 1) {
    if (m == 0)
      return 0;
    if (m % k == 0)
      return 2;

    m -= m / k + 1;
  }

  if (m == 0)
    return 1;
  if (m % k == 0)
    return 2;
  return 3;
}

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

  ll l = 0, r = 1000000000000000001LL;
  while (r - l > 1) {
    ll m = (l + r) / 2;
    int v = check(n, k, m);

    while (v == 2) {
      --m;
      v = check(n, k, m);
    }

    if (v == 1) {
      l = m;
      break;
    } else if (v == 0) {
      l = m;
      continue;
    } else if (v == 3) {
      r = m;
      continue;
    }
  }

  cout << l << endl;
  return 0;
}
Problem p01810 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

bool tsurai(long long s, long long id, long long d, long long k) {
  return (id + s - 1) / k > d + s - 2;
}

int main() {
  long long N, k;
  scanf("%lld %lld", &N, &k);

  long long d = 1, id = 0;

  for (long long i = 1; i < N; ++i) {
    id += d;
#if 0
        while (id / k > d-1) {
            ++d;
            ++id;
        }
#endif

    //        printf("id = %lld, d = %lld, k = %lld\\n", id, d, k);
    long long lb = 0, ub = 2e18;
    while (ub - lb > 1) {
      long long mid = (lb + ub) >> 1;
      (tsurai(mid, id, d, k) ? lb : ub) = mid;
    }

    //        printf("%lld\\n", lb);
    d += lb;
    id += lb;
  }

  printf("%lld\\n", id);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int main() {
  long long N, k;
  scanf("%lld %lld", &N, &k);

  long long d = 1, id = 0;

  for (long long i = 1; i < N; ++i) {
    id += d;
    while (id / k > d - 1) {
      ++d;
      ++id;
    }
  }

  printf("%lld\\n", id);
  return 0;
}
Problem p01810 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

using namespace std;

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  long long ans;
  if (n <= k) {
    ans = n - 1;
  } else {
    ans = 0;
    for (int i = 0; i < n - 1; i++) {
      ans += ans / (k - 1) + 1;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

using namespace std;

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  long long ans;
  if (n <= k) {
    ans = n - 1;
  } else {
    ans = 0;
    for (int i = 0; i < n; i++) {
      ans++;
      for (int j = k; j < ans; j += k) {
        ans++;
      }
      /*
      for (int j=1,t=k;j<ans;j++) {
        if (j == t) {
          ans++;
          t += k;
        }
      }
      */
      /*
      for (int j=1;j<ans;j++) {
        if (j%k == 0) ans++;
      }
      */
    }
    ans--;
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p01810 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, s = 0;
  cin >> a >> b;
  while (--a)
    s += s / (b - 1) + 1;
  cout << s << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, i, j, m[10000] = {0}, c = 0;
  cin >> a >> b;
  for (i = 0; i < a; i++) {
    while (m[c]) {
      c++;
    }
    for (j = c; j < 10000; j += b) {
      if (i == a - 1) {
        cout << j << endl;
        break;
      }
      m[j]++;
    }
    b *= b;
  }
}
Problem p01811 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

int main() {
  std::string s;
  std::cin >> s;

  if (s.size() % 2 == 0) {
    std::cout << "No" << std::endl;
    return 0;
  }

  std::unordered_set<std::string> searched;
  std::stack<std::string> st;
  st.push(s);

  while (!st.empty()) {
    std::string node = st.top();
    st.pop();
    if (node == "ABC") {
      std::cout << "Yes" << std::endl;
      return 0;
    }
    if (node.find("ABC") == -1)
      continue;

    for (int i = 0; i < 3; i++) {
      std::string ss("");
      bool isOk = true;
      for (int j = 0; j < node.size(); j++) {
        if (j + 2 < node.size() && node[j] == 'A' && node[j + 1] == 'B' &&
            node[j + 2] == 'C') {
          ss += 'A' + i;
          j += 2;
        } else if (node[j] == 'A' + i)
          isOk = false;
        else
          ss += node[j];
      }
      if (isOk && searched.find(ss) == searched.end()) {
        st.push(ss);
        searched.insert(ss);
      }
    }
  }
  std::cout << "No" << std::endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

int main() {
  std::string s;
  std::cin >> s;

  if (s.size() % 2 == 0) {
    std::cout << "No" << std::endl;
    return 0;
  }

  if (s == "ABC") {
    std::cout << "Yes" << std::endl;
    return 0;
  }

  int eachAlphabetCount[3] = {0};
  for (auto &x : s)
    eachAlphabetCount[x - 'A']++;

  std::unordered_set<std::string> searched;
  std::stack<std::string> st;
  st.push(std::string("ABC"));

  char d[] = {'A', 'B', 'C'};
  while (!st.empty()) {
    std::string node = st.top();
    st.pop();

    //????????????
    for (int i = 0; i < 3; i++) {
      std::string next = "";
      int count[3] = {0};
      for (auto &x : node) {
        if (x == d[i])
          next += "ABC";
        else
          next += x;
        count[x - 'A']++;
      }

      //????????°?¶??????§??????????????????
      if (next.size() > s.size())
        continue;

      if (next == s) { //??´????????????
        std::cout << "Yes" << std::endl;
        return 0;
      }

      //???????????????????????????????????¶???
      if (eachAlphabetCount[0] < count[0] || eachAlphabetCount[1] < count[1] ||
          eachAlphabetCount[2] < count[2])
        continue;

      auto it = searched.find(next);
      if (it == searched.end()) { //?????¢?´¢???????????¶????????§??????

        st.push(next);
        searched.insert(next);
      }
    }
  }
  std::cout << "No" << std::endl;
  return 0;
}