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

#include <iostream>

using namespace std;

int josephus(int n, int k) {
  if (n == 1) {
    return (0);
  }
  return ((josephus(n - 1, k) + k) % n);
}

void solve() {
  int n, k, m;
  while (cin >> n >> k >> m, n || k || m) {
    m = (m - k + 1 + n) % n;
    cout << (josephus(n, k) + m - 1 + n) % n + 1 << endl;
  }
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string.h>

using namespace std;

void solve() {
  int n, k, m;
  while (cin >> n >> k >> m, n || k || m) {
    bool used[10001];
    memset(used, 0, sizeof(used));
    int pos = m - 1;
    pos %= n;
    used[pos] = true;
    int count = 1;
    while (count < n - 1) {
      int step = k;
      while (step > 0) {
        if (!used[pos]) {
          --step;
        }
        ++pos;
        pos %= n;
      }
      used[(pos - 1 + n) % n] = true;
      ++count;
    }
    for (int i = 0; i < n; ++i) {
      if (!used[i]) {
        cout << i + 1 << endl;
      }
    }
  }
}

int main() {
  solve();
  return (0);
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;

int main() {
  int n, k, m, k2;
  int next[10001];
  while (cin >> n >> k >> m && (n || k || m)) {
    for (int i = 1; i < n; i++)
      next[i] = i + 1;
    next[n] = 1;
    if (m != 1)
      next[m - 1] = next[m];
    else
      next[n] = next[1];
    n--;
    while (next[m] != m) {
      k2 = k % n;
      while (k2 < 2)
        k2 += n;
      for (int i = 1; i < k2; i++) {
        m = next[m];
      }
      next[m] = next[next[m]];
      n--;
    }
    cout << m << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;

int main() {
  int n, k, m, k2;
  int next[10001];
  while (cin >> n >> k >> m && (n || k || m)) {
    for (int i = 1; i < n; i++)
      next[i] = i + 1;
    next[n] = 1;
    next[m - 1] = next[m];
    n--;
    while (next[m] != m) {
      for (int i = 1; i < k; i++) {
        m = next[m];
      }
      next[m] = next[next[m]];
    }
    cout << m << endl;
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999

int n, k, m;

int main() {
  while (cin >> n >> k >> m && (n || k || m)) {
    vector<int> data(n);
    rep(i, n) data[i] = i;
    int now = m - 1;
    rep(loop, n - 1) {
      data.erase(data.begin() + now);
      now = (now - 1 + k) % data.size();
    }
    cout << data[0] + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

int main() {
  int n, k, m;
  while (cin >> n >> k >> m && n && k && m) {
    bool used[10001];
    int num = n;
    rep(i, 10001) used[i] = false;
    int now = m - 1;
    while (1) {
      used[now] = true;
      num--;
      if (num == 1)
        break;
      int t = k;
      while (t) {
        now++;
        if (now == n)
          now = 0;
        if (!used[now])
          t--;
      }
    }
    rep(i, n) {
      if (used[i] == false)
        cout << i + 1 << endl;
    }
  }
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;

int stones[10000][2];

int snext(int n, int k) {
  if (k == 0) {
    return n;
  }
  return snext(stones[n][0], k - 1);
}

void srm(int n) {
  int aft = stones[n][0];
  int bef = stones[n][1];
  stones[bef][0] = aft;
  stones[aft][1] = bef;
  stones[n][1] = 0;
}

int main() {
  for (;;) {
    int n, k, m;
    cin >> n >> k >> m;
    if (!n and !k and !m)
      break;
    for (int i = 1; i <= n; i++) {
      stones[i][0] = i + 1;
      stones[i][1] = i - 1;
    }
    stones[1][1] = n;
    stones[n][0] = 1;
    srm(m);
    int flag = m;
    for (int i = 0; i < n - 2; i++) {
      flag = snext(flag, k);
      srm(flag);
    }
    for (int i = 1; i <= n; i++) {
      if (stones[i][1] != 0) {
        cout << i << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;

int main() {
  for (;;) {
    int n, k, m;
    cin >> n >> k >> m;
    if (!n and !k and !m)
      break;
    vector<int> stones(n + 1);
    for (int i = 0; i <= n; i++)
      stones[i] = 1;
    int flag = m;
    int num = 0;
    stones[m] = 0;
    while (true) {
      int j = 0;
      while (true) {
        flag++;
        if (flag == n + 1)
          flag = 1;
        if (stones[flag] == 1) {
          j++;
        }
        if (j == k)
          break;
      }
      stones[flag] = 0;
      num++;
      if (num == n - 2)
        break;
    }
    for (int i = 1; i <= n; i++) {
      if (stones[i] == 1) {
        cout << i << endl;
        break;
      }
    }
  }
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container)                                                      \\
  for (typeof(container.begin()) it = container.begin();                       \\
       it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))

typedef long long ll;
typedef complex<double> point;

// up right down left
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};

const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
  while (true) {
    int n, k, m;
    cin >> n >> k >> m;
    if (n == 0)
      break;

    vector<int> nums;
    for (int i = 1; i <= n; i++) {
      nums.push_back(i);
    }

    int pos = m - 1;
    while (nums.size() > 1) {
      nums.erase(nums.begin() + pos);
      pos--;
      pos = (pos + k) % nums.size();
    }
    cout << nums.front() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container)                                                      \\
  for (typeof(container.begin()) it = container.begin();                       \\
       it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))

typedef long long ll;
typedef complex<double> point;

// up right down left
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};

const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
  while (true) {
    int n, k, m;
    cin >> n >> k >> m;
    if (n == 0)
      break;
    // 0 start.
    m--;
    list<int> lis;
    rep(i, n) lis.push_back(i + 1);
    list<int>::iterator it = lis.begin();
    advance(it, m);
    while (lis.size() != 1) {
      it = lis.erase(it);
      if (it == lis.end())
        it = lis.begin();
      int st = k - 1 % lis.size();
      for (int i = 0; i < st; i++) {
        ++it;
        if (it == lis.end())
          it = lis.begin();
      }
    }
    cout << lis.front() << endl;
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int J(int n, int k) {
  if (n <= 1)
    return 0;
  return (J(n - 1, k) + k) % n;
}
int main() {
  int n, k, m;
  while (cin >> n >> k >> m && n != 0 || k != 0 || m != 0) {
    long ans = (J(n - 1, k) + m) % n + 1;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k, m;
  while (cin >> n >> k >> m && n != 0 || k != 0 || m != 0) {
    int array[10000 + 1];
    int it = m - 1;
    for (int i = 0; i < n; i++) {
      array[i] = i + 1;
    }

    int removed = 0;
    while (removed < n - 1) {
      array[it] = -1;
      removed++;
      int plus = 0;
      while (plus < k) {
        it = (it + 1) % n;
        if (array[it] != -1)
          plus++;
      }
    }
    cout << array[it] << endl;
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

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

int n, k, m;

int main(void) {
  while (cin >> n >> k >> m, n | k | m) {
    m--;
    vector<int> rem;
    rep(i, n) rem.push_back(i);

    int cnt = 0;
    for (;;) {
      vector<int>::iterator it;
      it = remove(ALL(rem), m);
      rem.erase(it, rem.end());

      if (++cnt == n)
        break;

      int idx = lower_bound(ALL(rem), m) - rem.begin();
      m = rem[(idx + k - 1) % rem.size()];
    }
    printf("%d\\n", m + 1);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

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

int n, k, m;

#define MAX 10001
int rem[MAX];

int main(void) {
  while (cin >> n >> k >> m, n | k | m) {
    m--;
    memset(rem, 0, sizeof(rem));

    int cnt = 0;
    for (;;) {
      rem[m] = 1;
      if (++cnt == n)
        break;

      int rest = n - cnt;
      for (int i = (m + 1) % n, kk = 0;; i = (i + 1) % n) {
        if (!rem[i] && kk++ == (k - 1) % rest) {
          m = i;
          break;
        }
      }
    }
    printf("%d\\n", m + 1);
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <iostream>

using namespace std;

struct list {
  int data;
  struct list *next;
};

list *make_list(int n, list *p) {
  list *z = (list *)malloc(sizeof(list));
  z->data = n;
  z->next = p;
  return z;
}

int main() {
  int n, k, m;
  while (cin >> n >> k >> m) {
    if (n == 0 && k == 0 && m == 0) {
      break;
    }
    list *z = make_list(n, NULL);
    list **p = &z;
    for (int i = n - 1; i >= 1; i--) {
      z = make_list(i, z);
    }
    while ((*p)->next != NULL) {
      p = &((*p)->next);
    }
    if (m != 1) {
      (*p)->next = z;
    } else {
      (*p)->next = z->next;
      z = z->next;
    }
    if (m != 1) {
      p = &z;
      for (int i = 0; i < m - 2; i++) {
        p = &((*p)->next);
      }
      (*p)->next = ((*p)->next)->next;
    }
    while (((*p)->next)->data != (*p)->data) {
      for (int i = 0; i < k - 1; i++) {
        p = &((*p)->next);
      }
      (*p)->next = ((*p)->next)->next;
    }
    cout << (*p)->data << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k, m;
  while (cin >> n >> k >> m) {
    if (n == 0 && k == 0 && m == 0) {
      break;
    }
    vector<int> hyou(n + 1, 0);
    int counter = 1;
    int counter2 = 0;
    hyou[m] = 1;
    m++;
    if (m > n) {
      m = 1;
    }
    while (counter < n - 1) {
      if (hyou[m] == 0) {
        counter2++;
      }
      if (counter2 == k) {
        hyou[m] = 1;
        counter++;
        counter2 = 0;
      }
      m++;
      if (m > n) {
        m = 1;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (hyou[i] == 0) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

//??¨??????????????????

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

int f(int n, int k) {
  if (n == 1)
    return 0;
  return (f(n - 1, k) + k) % n;
}

int main() {
  while (1) {
    int n, k, m;
    scanf(" %d %d %d", &n, &k, &m);
    if (n == 0)
      break;

    printf("%d\\n", (f(n - 1, k) + m) % n + 1);
  }
}

Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int n, k, m;
    int a[10001] = {0}, result[10001]; // 0~n-1????????¨

    scanf(" %d %d %d", &n, &k, &m);
    if (n == 0)
      break;

    // initialize
    for (int i = 0; i < n; ++i)
      a[i] = i;

    // first step
    for (int i = 0; i < n - 1; ++i)
      result[i] = a[(i + m) % n];
    n--;
    for (int i = 0; i < n; ++i)
      a[i] = result[i]; // copy

    while (n > 0) {
      for (int i = 0; i < n - 1; ++i)
        result[i] = a[(i + k) % n];
      n--;
      for (int i = 0; i < n; ++i)
        a[i] = result[i]; // copy
    }

    //?????????1?¶????????????§??????
    printf("%d\\n", result[0] + 1);
  }
}
Problem p00854 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n, k, m;
  while (scanf("%d%d%d", &n, &k, &m), n) {
    m--;
    vector<int> v(n);
    iota(v.begin(), v.end(), 1);
    while (v.size() > 1) {
      v.erase(v.begin() + m);
      m = (m + k - 1) % v.size();
    }
    cout << v[0] << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n, k, m;
  while (scanf("%d%d%d", &n, &k, &m)) {
    m--;
    vector<int> v(n);
    iota(v.begin(), v.end(), 1);
    while (v.size() > 1) {
      v.erase(v.begin() + m);
      m = (m + k - 1) % v.size();
    }
    cout << v[0] << endl;
  }
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int solve(int n, int k) {
  if (n == 1)
    return 0;
  return (solve(n - 1, k) + k) % n;
}

int main() {
  int n, k, m;
  while (cin >> n >> k >> m) {
    if (n == 0)
      break;
    cout << (solve(n - 1, k) + m) % n + 1 << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vd = vector<double>;
using vvd = vector<vd>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using vs = vector<string>;
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define LINF ((ll)1ll < 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007)
#define fcout(a) cout << setprecision(a) << fixed
#define fs first
#define sc second
#define PI 3.141592653589793

template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool a = 1;
  for (auto s : t) {
    os << (a ? "" : " ") << s;
    a = 0;
  }
  return os;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k, m;
  while (cin >> n >> k >> m, n) {
    list<int> r(n);
    iota(all(r), 1);
    auto p = next(r.begin(), m - 1);
    while (r.size() > 1) {
      p = r.erase(p);
      int pos = distance(r.begin(), p);
      p = next(r.begin(), (pos + k - 1) % r.size());
    }
    cout << *r.begin() << endl;
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main() {
  while (1) {
    int n, k, m;
    cin >> n >> k >> m;
    if (n == 0)
      break;
    list<int> a;
    for (int i = 1; i <= n; i++) {
      if (i != m)
        a.push_back(i);
    }
    auto itr = a.begin(), itr2 = a.begin();
    for (int i = 0; i < m - 1; i++) {
      itr++;
      if (itr == a.end())
        itr = a.begin();
    }
    for (int i = 0; i < n - 2; i++) {
      for (int j = 0; j < (k - 1) % (n - i - 1); j++) {
        // cout << *itr << endl;
        itr++;
        if (itr == a.end())
          itr = a.begin();
      }
      itr2 = itr;
      // cout << "!" SP *itr << endl;
      itr++;
      if (itr == a.end())
        itr = a.begin();
      a.erase(itr2);
    }
    cout << a.front() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main() {
  while (1) {
    int n, k, m;
    cin >> n >> k >> m;
    if (n == 0)
      break;
    vector<int> a(n, 1);
    a[m - 1] = 0;
    int now = m % n;
    int count;
    for (int i = 0; i < n - 2; i++) {
      count = 0;
      while (1) {
        if (a[now]) {
          count++;
          if (count % (n - i - 1) == k % (n - i - 1)) {
            a[now] = 0;
            // cout << now << endl;
            now = (now + 1) % n;
            break;
          }
        }
        now = (now + 1) % n;
      }
    }
    for (int i = 0; i < n; i++) {
      if (a[i]) {
        cout << i + 1 << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF 1e9

using namespace std;

struct node {
  int id, fr, to;
  node(int i, int f, int t) : id(i), fr(f), to(t) {}
};

int main() {
  int n, m, k;
  while (cin >> n >> k >> m and n + m + k) {
    vector<node> left;
    REP(i, n) left.emplace_back(i, (i + n - 1) % n, (i + 1) % n);
    left[left[m - 1].fr].to = left[m - 1].to;
    left[left[m - 1].to].fr = left[m - 1].fr;
    int size = n - 1;
    int target = m % n;
    int counter = 0;
    if (size == 1) {
      cout << left[m - 1].to + 1 << endl;
      continue;
    }
    while (size > 1) {
      counter++;
      if (counter == k) {
        size--;
        if (size == 1) {
          cout << left[target].to + 1 << endl;
          break;
        }
        left[left[target].fr].to = left[target].to;
        left[left[target].to].fr = left[target].fr;
        counter = 0;
      }
      target = left[target].to;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF 1e9

using namespace std;

int main() {
  int n, m, k;
  while (cin >> n >> k >> m and n + m + k) {
    bool left[n];
    int count = n;
    int target = 0;
    REP(i, n) left[i] = true;
    int i = m;
    left[m - 1] = false;
    while (count > 2) {
      if (i == n)
        i = 0;
      if (left[i])
        target++;
      if (target == k) {
        left[i] = false;
        target = 0;
        count--;
      }
      i++;
    }
    int res;
    REP(i, n) if (left[i] == true) res = i + 1;
    cout << res << endl;
  }
  return 0;
}