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

#include <iostream>
#include <vector>

using namespace std;

int rec(int n, int k) {

  if (n == 1)
    return 0;
  else
    return (rec(n - 1, k) + k) % n;
}

void hoge(int n, int k, int m) { cout << (rec(n - 1, k) + m) % n + 1 << endl; }

int main() {

  while (1) {
    int n, k, m;
    cin >> n >> k >> m;
    if (!(n || k || m))
      break;

    hoge(n, k, m);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
int a[10005];

void hoge(int n, int k, int m) {

  for (int i = 0; i < 10005; i++)
    a[i] = 0;
  int p = m - 1;
  for (int i = 0; i < n - 1; i++) {
    a[p] = 1;
    int _k = k;
    while (_k) {
      (p += 1) %= n;
      if (a[p] == 0)
        _k--;
    }
    while (a[p] == 1) {
      (p += 1) %= n;
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == 0) {
      cout << i + 1 << endl;
      break;
    }
  }
}

int main() {

  while (1) {
    int n, k, m;
    cin >> n >> k >> m;
    if (!(n || k || m))
      break;

    hoge(n, k, m);
  }

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

#include <stdio.h>
#include <string.h>

int mod(int m, int n) {
  while (m >= n)
    m -= n;
  return m;
}

int main(void) {
  int n, k, i, t, loc, stone[10000], count;
  while (1) {
    scanf("%d%d%d", &n, &k, &loc);
    if (n == 0 && k == 0 && loc == 0)
      break;
    loc--;
    count = 1;
    memset(stone, 0, sizeof(stone));
    stone[loc] = 1;
    while (count < n - 1) {
      t = k;
      if (n - count < t)
        t = (t - 1) % (n - count) + 1;
      for (i = 0; i < t; i++) {
        loc = mod(loc + 1, n);
        while (stone[loc])
          loc = mod(loc + 1, n);
      }
      stone[loc] = 1;
      count++;
    }
    for (i = 0; i < n; i++) {
      if (!stone[i])
        printf("%d\\n", i + 1);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

int main(void) {
  int n, k, i, t, loc, stone[10000], count;
  while (1) {
    scanf("%d%d%d", &n, &k, &loc);
    if (n == 0 && k == 0 && loc == 0)
      break;
    loc--;
    count = 1;
    memset(stone, 0, sizeof(stone));
    stone[loc] = 1;
    while (count < n - 1) {
      t = k;
      if (n - count < t)
        t = (t - 1) % (n - count) + 1;
      for (i = 0; i < t; i++) {
        loc = (loc + 1) % n;
        while (stone[loc])
          loc = (loc + 1) % n;
      }
      stone[loc] = 1;
      count++;
    }
    for (i = 0; i < n; i++) {
      if (!stone[i])
        printf("%d\\n", i + 1);
    }
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int n, k, m;

int main() {
  while (cin >> n >> k >> m, n) {
    vector<int> v;
    for (int i = 0; i < n; i++)
      v.push_back(i + 1);

    int p = m - 1;
    v.erase(v.begin() + p);

    while ((int)v.size() > 1) {
      int size = (int)v.size();
      p = (p + k - 1) % size;
      v.erase(v.begin() + p);
    }

    cout << (*v.begin()) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef set<int> Set;
typedef Set::iterator set_iter;
int n, k, m;

int main() {
  while (cin >> n >> k >> m, n) {
    Set s;
    for (int i = 0; i < n; i++)
      s.insert(i + 1);

    int p = m - 1;
    set_iter sit = s.begin();
    for (int i = 0; i < p; i++)
      sit++;
    s.erase(*sit);

    while ((int)s.size() > 1) {
      int size = (int)s.size();
      p = (p + k - 1) % size;
      sit = s.begin();
      for (int i = 0; i < p; i++)
        sit++;
      s.erase(*sit);
    }

    cout << (*s.begin()) << endl;
  }

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

#include <iostream>
using namespace std;

bool isExist[10000];
int n, k, m;

int main() {
  while (cin >> n >> k >> m) {
    if (!n)
      break;
    int i;

    for (i = 0; i < n; i++) {
      isExist[i] = true;
    }

    int cur = m - 1;
    isExist[cur] = false;
    for (i = 1; i < n; i++) {
      int step = (k - 1) % (n - i) + 1;
      while (true) {
        if (isExist[cur])
          step--;
        if (step <= 0)
          break;
        cur++;
        if (cur == n)
          cur = 0;
      }
      isExist[cur] = false;
    }
    cout << cur + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool isExist[10000];
int n, k, m;

int main() {
  while (cin >> n >> k >> m) {
    if (!n)
      break;
    int i;

    for (i = 0; i < n; i++) {
      isExist[i] = true;
    }

    int cur = m - 1;
    isExist[cur] = false;
    for (i = 1; i < n; i++) {
      int step = (k - 1) % (n - i) + 1;
      while (true) {
        if (isExist[cur])
          step--;
        if (step <= 0)
          break;
        cur = (cur + 1) % n;
      }
      isExist[cur] = false;
    }
    cout << cur + 1 << endl;
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;

int n, m, k;

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;

int st[10001];

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

    for (int i = 0; i <= n; i++) {
      st[i] = i;
    }

    int lest = n - 1;
    st[m] = 0;
    while (lest > 1) {
      for (int i = 0; i < k % lest;) {
        m = m % n + 1;
        if (st[m] != 0)
          ++i;
      }
      if (k % lest == 0) {
        for (int i = 0; i < n; i++) {
          m = m % n - 1;
          if (m < 1)
            m = n;
          if (st[m] != 0)
            break;
        }
      }
      --lest;
      st[m] = 0;
      // if(n<=100)printf("%d\\n",m);
    }
    // puts("");
    for (int i = 1; i <= n; i++) {
      if (st[i]) {
        printf("%d\\n", st[i]);
        break;
      }
    }
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

int main() {
  int dif, all, first;

  while (cin >> all >> dif >> first, all || dif || first) {
    vector<int> num(all);
    for (int i = 0; i < all; i++) {
      num[i] = i + 1;
    }
    vector<int>::iterator it = num.begin();
    int next = first - 1;
    num.erase(it + next);
    for (int i = 0; i < all - 1; i++) {
      next = (next + dif - 1) % num.size();
      // cout << num[next] << endl;
      num.erase(it + next);
    }
    cout << num[0] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

int main() {
  int dif, all, first, num[10001];

  while (cin >> all >> dif >> first, all || dif || first) {
    /*for( int i = 0; i < all; i++ ){
        num[i] = 1;
    }*/
    memset(num, 0, sizeof(num));
    int j = dif * (all - 1);
    int i = first;
    num[first - 1] = 1;
    while (j > 0) {
      if (i >= all) {
        i = 0;
      }
      if (num[i] == 0) {
        j--;
        if (j % dif == 0) {
          num[i] = 1;
        }
      }
      i++;
    }
    cout << i << endl;
  }
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
using namespace std;
bool x[10000];
int a, b, c, d, e, p;
int main() {
  while (true) {
    memset(x, true, 10000);
    cin >> a >> b >> c;
    if (a == 0) {
      break;
    }
    d = a - 1;
    x[c - 1] = false;
    p = c - 1;
    while (d >= 2) {
      e = b % d;
      if (e == 0) {
        e = d;
      }
      for (int i = 0; i < e; i++) {
        while (true) {
          p++;
          if (x[p % a] == true) {
            break;
          }
        }
      }
      x[p % a] = false;
      d--;
    }
    for (int i = 0; i < a; i++) {
      if (x[i] == true) {
        p = i;
      }
    }
    cout << p + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
bool x[10000];
int a, b, c, d, e, p;
int main() {
  while (true) {
    memset(x, true, 10000);
    cin >> a >> b >> c;
    if (a == 0) {
      break;
    }
    d = a - 1;
    x[c - 1] = false;
    p = c - 1;
    while (d >= 2) {
      e = b % d + d;
      for (int i = 0; i < e; i++) {
        while (true) {
          p++;
          if (x[p % a] == true) {
            break;
          }
        }
      }
      x[p % a] = false;
      d--;
    }
    for (int i = 0; i < a; i++) {
      if (x[i] == true) {
        p = i;
      }
    }
    cout << p + 1 << endl;
  }
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <list>

using std::cin;
using std::cout;
using std::endl;
using std::list;

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

  while (n != 0 || k != 0 || m != 0) {
    list<int> nums(n, 0);
    int num = 1;
    for (list<int>::iterator itr = nums.begin(); itr != nums.end(); ++itr) {
      *itr = num;
      num++;
    }

    int count_s = n;
    int rm = m;
    int l;
    bool first = true;

    for (list<int>::iterator itr = nums.begin(); count_s != 1;) {
      if (first) {
        for (int i = 0; i < rm - 1; i++) {
          ++itr;
          if (itr == nums.end()) {
            itr = nums.begin();
          }
        }
        first = false;
      } else {
        l = ((k - 1) % count_s) + 1;
        for (int i = 0; i < l; i++) {
          ++itr;
          if (itr == nums.end()) {
            itr = nums.begin();
          }
        }
      }

      itr = nums.erase(itr);
      if (count_s == n) {
        rm = k;
      }
      count_s--;
      if (count_s != 1) {
        --itr;
      }
    }
    cout << *nums.begin() << endl;
    cin >> n >> k >> m;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <list>

using std::cin;
using std::cout;
using std::endl;
using std::list;

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

  while (n != 0 || k != 0 || m != 0) {
    list<int> nums(n, 0);
    int num = 1;
    for (list<int>::iterator itr = nums.begin(); itr != nums.end(); ++itr) {
      *itr = num;
      num++;
    }

    int count_s = n;
    int rm = m;
    int l;
    bool first = true;

    for (list<int>::iterator itr = nums.begin(); count_s != 1;) {
      if (first) {
        for (int i = 0; i < rm - 1; i++) {
          ++itr;
          if (itr == nums.end()) {
            itr = nums.begin();
          }
        }
        first = false;
      } else {
        for (int i = 0; i < rm; i++) {
          ++itr;
          if (itr == nums.end()) {
            itr = nums.begin();
          }
        }
      }

      itr = nums.erase(itr);
      if (count_s == n) {
        rm = k;
      }
      count_s--;
      if (count_s != 1) {
        --itr;
      }
    }
    cout << *nums.begin() << endl;
    cin >> n >> k >> m;
  }

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

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

int main() {
  while (1) {
    vector<int> a;
    int n, k, m;
    cin >> n >> k >> m;
    if (n + k + m == 0)
      break;
    for (int i = 1; i <= n; i++)
      a.push_back(i);
    while (a.size() != 1)
      a.erase(a.begin() + m - 1), m = (m + k - 2) % a.size() + 1;
    cout << a[0] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int n, k, m, f[10001], ans;

void del(int x, int t) {
  queue<int> q;
  int k1 = k % (n - t);
  if (k1 == 0)
    k1 = k;
  int re = 0, cnt = 0, a = 0;
  if (t == (n - 1))
    return;

  while (cnt <= k1) {
    if (f[x % n] == 0)
      q.push(x % n), x++, cnt++;
    else
      x++;
  }
  cnt = 0;
  while (cnt < k1)
    a = q.front(), q.pop(), cnt++;
  f[a] = 1;
  del(a, t + 1);
}

int main() {
  while (1) {
    for (int i = 0; i < 10001; i++)
      f[i] = 0;
    cin >> n >> k >> m;
    if (n + k + m == 0)
      break;
    f[m % n] = 1;
    del(m % n, 1);
    for (int i = 0; i < n; i++)
      if (f[i] == 0)
        ans = i;
    if (ans == 0)
      ans = n;
    cout << ans << endl;
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) for (i = 0; i < n; ++i)

vector<int> ary;

int main() {
  int n, k, m, i, j;
  while (cin >> n >> k >> m, n | k | m) {
    ary.clear();
    ary.resize(n);
    rep(i, n) ary[i] = i;

    vector<int>::iterator it = ary.begin();
    int count = n - 1, pos = m - 1;
    ary.erase(it + m - 1);
    while (count > 1) {
      pos = (pos + k - 1) % count;
      ary.erase(ary.begin() + pos);
      --count;
    }
    cout << *ary.begin() + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define rep(i, n) for (i = 0; i < n; ++i)

int ary[10000];

int main() {
  int n, k, m, i, j;
  while (cin >> n >> k >> m, n | k | m) {
    rep(i, n) ary[i] = 1;
    ary[m - 1] = 0;
    int now = m;

    rep(i, n - 1) {
      rep(j, k) {
        while (ary[(now + 1) % n] == 0)
          now++;
        now = (now + 1) % n;
      }
      // cout<<i<<" : "<<now<<endl;
      ary[now] = 0;
    }
    cout << now << endl;
  }
}
Problem p00854 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

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

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

Error: -11

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

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

int main() {
  for (;;) {
    int n, k, m;
    cin >> n >> k >> m;
    cout << 1 + (m - 1 + f(n, k)) % n << endl;
  }
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
typedef long long ll;
typedef complex<int> P;
typedef pair<int, int> Pii;

bool solve() {
  int n, k, m;
  cin >> n >> k >> m;
  if (n == 0 && k == 0 && m == 0)
    return false;

  vector<int> a(n);
  for (int i = 0; i < n; i++)
    a[i] = (i + 1) % n;

  int p = m - 1;
  for (int j = 0; j < n - 1; j++) {
    // cout<< p<< endl;
    for (int i = 0; i < n; i++)
      if (a[i] == p)
        a[i] = a[p];
    // for(int i=0;i<n;i++) cout<< a[i]; cout<< endl;
    for (int i = 0; i < k; i++)
      p = a[p];
  }

  cout << p + 1 << endl;
  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);

  while (solve())
    ;

  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
typedef long long ll;
typedef complex<int> P;
typedef pair<int, int> Pii;

bool solve() {
  int n, k, m;
  cin >> n >> k >> m;
  if (n == 0 && k == 0 && m == 0)
    return false;
  vector<int> a(n), b;
  for (int i = 0; i < n; i++)
    a[i] = i + 1;
  for (int i = 0; i < n - 1; i++)
    b.pb(a[(m + i) % n]);
  a = b;

  for (int i = 0; i < n - 2; i++) {
    b.clear();
    for (int j = 0; j < a.size() - 1; j++) {
      b.pb(a[(k + j) % a.size()]);
    }
    a = b;
    // for(int j=0;j<a.size();j++) cout<< a[j]; cout<<endl;
  }

  cout << a[0] << endl;
  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);

  while (solve())
    ;

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
typedef long long ll;

#define INF 1000000000

int main() {
  int n, k, m;
  while (cin >> n >> k >> m, n) {
    vector<int> vec(n + 1);
    repl(i, 1, n) vec[i] = i + 1;
    vec[n] = 1;

    vec[(m - 1 + n - 1) % n + 1] = (m + 1 - 1) % n + 1;
    int now = (m - 1 + n - 1) % n + 1;

    rep(i, n - 2) {
      rep(j, k - 1) { now = vec[now]; }
      //            dbg(vec[now]);
      vec[now] = vec[vec[now]];
    }

    cout << vec[now] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
typedef long long ll;

#define INF 1000000000

int main() {
  int n, k, m;
  while (cin >> n >> k >> m, n) {
    vector<bool> vec(n, true);
    int current = m - 1;
    vec[current] = false;
    rep(i, n - 2) {
      int cnt = 0;
      while (cnt < k) {
        current++;
        current %= n;
        cnt += vec[current];
      }
      vec[current] = false;
    }
    rep(i, n) if (vec[i]) cout << i + 1 << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define INF 999999999

vector<int> a;

void tori(int n, int k, int m) {
  if (n > 1) {
    // for(int i=0;i<=n;i++)
    // cout << a[i] << " ";
    // cout << endl;
    int z = (m + k - 1) % n;
    if (z == 0)
      z = n;
    a.erase(a.begin() + z);
    tori(n - 1, k, z);
  }
}

int main() {
  int i, j, n, k, m, z;
  while (1) {
    cin >> n >> k >> m;
    if (!n)
      break;
    a.clear();
    for (i = 0; i <= n; i++)
      a.push_back(i);
    a.erase(a.begin() + m);
    tori(n - 1, k, m);
    cout << a[1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define INF 999999999

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
typedef long long ll;

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

    bool used[10005];
    memset(used, 0, sizeof(used));

    vector<int> v;
    rep(i, n) { v.push_back(i); }

    int id = m - 1;
    v.erase(v.begin() + id);
    used[id] = true;

    while (v.size() != 1) {
      id += k - 1;
      id %= v.size();

      used[v[id]] = true;
      v.erase(v.begin() + id);
    }

    cout << v[0] + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
typedef long long ll;

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

    bool used[10005];
    memset(used, 0, sizeof(used));

    int id = m - 1;
    used[id] = true;
    while (true) {
      int cnt = 0;
      rep(i, n) if (!used[i]) cnt++;

      if (cnt == 1) {
        break;
      }

      rep(i, k) {
        id++;
        id %= n;
        while (used[id]) {
          id++;
          id %= n;
        }
      }

      used[id] = true;
    }

    rep(i, n) {
      if (!used[i]) {
        cout << i + 1 << endl;
      }
    }
  }
  return 0;
}