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

#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 20
#define MAX_K 1355
long long dp[MAX_N + 1][MAX_K][220];
long long sum[MAX_N + 1][MAX_K];
int prime[MAX_K];
int main() {
  prime[0] = 1;
  prime[1] = 1;
  for (int i = 2; i * i < MAX_K; i++) {
    for (int j = i * i; j < MAX_K; j += i) {
      prime[j] = 1;
    }
  }
  for (int i = 0; i < MAX_K; i++)
    prime[i] = 1 - prime[i];
  vector<int> p;
  p.push_back(0);
  for (int i = 0; i < MAX_K; i++) {
    if (prime[i] == 1) {
      p.push_back(i);
    }
  }
  dp[0][0][0] = 1;
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_K; j++) {
      for (int k = 0; k < p.size(); k++) {
        for (int l = k + 1; l < p.size(); l++) {
          if (j + p[l] >= MAX_K) {
            continue;
          }
          dp[i + 1][j + p[l]][l] += dp[i][j][k];
        }
      }
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_K; j++) {
      for (int k = 0; k < p.size(); k++) {
        sum[i][j] += dp[i][j][k];
      }
    }
  }
  int n, k;
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    cout << sum[k][n] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 20
#define MAX_K 1355
long long dp[MAX_N + 1][MAX_K][300];
long long sum[MAX_N + 1][MAX_K];
int prime[MAX_K];
int main() {
  prime[0] = 1;
  prime[1] = 1;
  for (int i = 2; i * i < MAX_K; i++) {
    for (int j = i * i; j < MAX_K; j += i) {
      prime[j] = 1;
    }
  }
  for (int i = 0; i < MAX_K; i++)
    prime[i] = 1 - prime[i];
  vector<int> p;
  p.push_back(0);
  for (int i = 0; i < MAX_K; i++) {
    if (prime[i] == 1) {
      p.push_back(i);
    }
  }
  dp[0][0][0] = 1;
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_K; j++) {
      for (int k = 0; k < p.size(); k++) {
        for (int l = k + 1; l < p.size(); l++) {
          if (j + p[l] >= MAX_K) {
            continue;
          }
          dp[i + 1][j + p[l]][l] += dp[i][j][k];
        }
      }
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_K; j++) {
      for (int k = 0; k < p.size(); k++) {
        sum[i][j] += dp[i][j][k];
      }
    }
  }
  int n, k;
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    cout << sum[k][n] << endl;
  }
  return 0;
}
Problem p00848 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define MAX_N 1120 + 1
#define MAX_K 14 + 1
#define MAX_L 187 + 1

vector<int> p;
int dp[MAX_N][MAX_K][MAX_L];

void init() {
  memset(dp, -1, sizeof(dp));
  bool f[MAX_N];
  memset(f, 1, sizeof(f));
  f[0] = f[1] = false;
  for (int i = 2; i <= MAX_N; i++)
    if (f[i]) {
      p.push_back(i);
      for (int j = i + i; j <= MAX_N; j += i)
        f[j] = false;
    }
}

int F(int n, int k, int l) {
  // cout << n << ' ' << k << ' ' << l << endl;
  if (dp[n][k][l] >= 0)
    return dp[n][k][l];
  if (k == 0) {
    if (n == 0)
      return 1;
    return 0;
  }
  if (n < 2)
    return 0;
  int c = 0;
  for (int i = 0; i < p.size() && p[i] <= n && i < l; i++) {
    c += F(n - p[i], k - 1, i);
  }
  return dp[n][k][l] = c;
}

int main() {
  init();
  int n, k;
  while (cin >> n >> k) {
    if (n == 0 && k == 0)
      break;
    cout << F(n, k, p.size()) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

#define MAX_N 1120 + 1
#define MAX_K 14 + 1

vector<int> p;
int dp[MAX_N][MAX_K][MAX_N];

void init() {
  memset(dp, -1, sizeof(dp));
  bool f[MAX_N];
  memset(f, 1, sizeof(f));
  f[0] = f[1] = false;
  for (int i = 2; i <= MAX_N; i++)
    if (f[i]) {
      p.push_back(i);
      for (int j = i + i; j <= MAX_N; j += i)
        f[j] = false;
    }
}

int F(int n, int k, int l) {
  // cout << n << ' ' << k << ' ' << l << endl;
  if (dp[n][k][l] >= 0)
    return dp[n][k][l];
  if (k == 0) {
    if (n == 0)
      return 1;
    return 0;
  }
  if (n < 2)
    return 0;
  int c = 0;
  for (int i = 0; i < p.size() && p[i] <= n && p[i] <= l; i++) {
    c += F(n - p[i], k - 1, p[i] - 1);
  }
  return dp[n][k][l] = c;
}

int main() {
  init();
  int n, k;
  while (cin >> n >> k) {
    if (n == 0 && k == 0)
      break;
    cout << F(n, k, n) << endl;
  }
  return 0;
}
Problem p00848 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;
const int MAX_PRIME = 2000;
int prime[MAX_PRIME + 1];
bool isPrime[MAX_PRIME + 1];
int p;
void erats() {
  // primeツδ環スツトツづーツ渉可甘コツ可サ
  fill(isPrime, isPrime + MAX_PRIME, true);
  p = 0;
  isPrime[0] = isPrime[1] = false;

  for (int i = 2; i <= MAX_PRIME; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
    } else
      continue;

    // ツづ督づゥツつ「ツづ可つゥツつッツづゥ
    for (int j = 2 * i; j <= MAX_PRIME; j += i) {
      isPrime[j] = false;
    }
    cout << flush;
  }
}

int n, k;
int dp[15][1200][305];
int dfs(int cnt, int left, int tail) {
  if (dp[cnt][left][tail] != -1)
    return dp[cnt][left][tail];
  int res = 0;
  if (cnt == k) {
    if (left == 0)
      return 1;
    else
      return 0;
  }
  for (int i = tail; prime[i] <= left; i++) {
    res += dfs(cnt + 1, left - prime[i], i + 1);
  }
  return dp[cnt][left][tail] = res;
}

int main() {
  erats();
  while (cin >> n >> k && !(n == 0 && k == 0)) {
    memset(dp, -1, sizeof(dp));
    int res = dfs(0, n, 0);
    cout << res << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;
const int MAX_PRIME = 10000;
int prime[MAX_PRIME + 1];
bool isPrime[MAX_PRIME + 1];
int p;
void erats() {
  // primeツδ環スツトツづーツ渉可甘コツ可サ
  fill(isPrime, isPrime + MAX_PRIME, true);
  p = 0;
  isPrime[0] = isPrime[1] = false;

  for (int i = 2; i <= MAX_PRIME; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
    } else
      continue;

    // ツづ督づゥツつ「ツづ可つゥツつッツづゥ
    for (int j = 2 * i; j <= MAX_PRIME; j += i) {
      isPrime[j] = false;
    }
    cout << flush;
  }
}

int n, k;
int dp[15][1200][1200];
int dfs(int cnt, int left, int tail) {
  if (dp[cnt][left][tail] != -1)
    return dp[cnt][left][tail];
  int res = 0;
  if (cnt == k) {
    if (left == 0)
      return 1;
    else
      return 0;
  }
  for (int i = tail; prime[i] <= left; i++) {
    res += dfs(cnt + 1, left - prime[i], i + 1);
  }
  return dp[cnt][left][tail] = res;
}

int main() {
  erats();
  while (cin >> n >> k && !(n == 0 && k == 0)) {
    memset(dp, -1, sizeof(dp));
    int res = dfs(0, n, 0);
    cout << res << endl;
  }
  return 0;
}
Problem p00848 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

#define MAXN 1120
#define MAXP 187
#define MAXK 14

using namespace std;

int dp[MAXN + 1][MAXP + 1][MAXK + 1];

int Prime(int p) {
  int i, f;
  f = 0;
  if (p == 2)
    return 1;
  if (p % 2 == 0)
    return 0;
  for (i = 3; i * i <= p; i += 2) {
    if (p % i == 0)
      f = 1;
  }

  if (f == 0)
    return 1;
  else
    return 0;
}

int main() {
  int prime[MAXP + 1], cnt, n, kk;
  cnt = 0;
  for (int i = 2; i <= MAXN; i++) {
    if (Prime(i))
      prime[cnt++] = i;
  }

  for (int i = 0; i <= MAXN; i++) {
    for (int j = 0; j < MAXP; j++) {
      for (int k = 0; k <= MAXK; k++) {
        dp[i][j][k] = 0;
        if (prime[j] == i)
          dp[i][j][1] = 1;
      }
    }
  }
  for (int j = 1; j < MAXP; j++) {
    for (int i = 0; i <= MAXN; i++) {
      for (int k = 1; k <= MAXK; k++) {
        dp[i][j][k] += dp[i][j - 1][k];
        if (i >= prime[j])
          dp[i][j][k] += dp[i - prime[j]][j - 1][k - 1];
      }
    }
  }

  while (cin >> n >> kk) {
    if (!(n | kk))
      break;
    cout << dp[n][MAXP - 1][kk] << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

int Prime(int p) {
  int i, f;
  f = 0;
  if (p == 2)
    return 1;
  if (p % 2 == 0)
    return 0;
  for (i = 3; i * i <= p; i += 2) {
    if (p % i == 0)
      f = 1;
  }

  if (f == 0)
    return 1;
  else
    return 0;
}

int main() {
  int prime[188], cnt, n, k, dp[1121][188][15];
  prime[0] = -1;
  cnt = 1;
  for (int i = 2; i <= 1120; i++) {
    if (Prime(i))
      prime[cnt++] = i;
  }
  for (int i = 0; i < cnt; i++)
    cout << prime[i] << endl;
  cout << cnt << endl;

  for (int i = 0; i <= 1120; i++) {
    for (int j = 0; j < cnt; j++) {
      for (int k = 0; k <= 14; k++) {
        dp[i][j][k] = 0;
        if (prime[j] == i)
          dp[i][j][1] = 1;
      }
    }
  }
  for (int j = 1; j < cnt; j++) {
    for (int i = 1; i <= 1120; i++) {
      for (int k = 1; k <= 14; k++) {
        dp[i][j][k] += dp[i][j - 1][k];
        if (i >= prime[j])
          dp[i][j][k] += dp[i - prime[j]][j - 1][k - 1];
      }
    }
  }

  while (cin >> n >> k) {
    if (!(n | k))
      break;
    cout << dp[n][cnt - 1][k] << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX 1120

bool isprime[MAX + 1];
vector<int> prime;

void setprime() {
  for (int i = 2; i < MAX + 1; i++) {
    if (!isprime[i]) {
      for (int j = 2; i * j < MAX + 1; j++) {
        isprime[i * j] = true;
      }
      prime.push_back(i);
    }
  }
}

int dp[MAX + 1][15];
void solve() { // dpかきたくないよ
  dp[0][0] = 1;
  for (int i = 0; i < prime.size(); i++) {
    for (int j = 13; j >= 0; j--) {
      for (int k = 0; k <= MAX - prime[i]; k++) {
        dp[k + prime[i]][j + 1] += dp[k][j];
      }
    }
  }
}

int main() {
  setprime();
  solve();
  int n, k;
  while (cin >> n >> k, n | k) {
    cout << dp[n][k] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX 1120

bool isprime[MAX + 1];
vector<int> prime;

int setprime() {
  for (int i = 2; i < MAX + 1; i++) {
    if (!isprime[i]) {
      for (int j = 2; i * j < MAX + 1; j++) {
        isprime[i * j] = true;
      }
      prime.push_back(i);
    }
  }
}

int dp[MAX + 1][15][188];

int rec(int mul, int k, int idx) {
  if (dp[mul][k][idx])
    return dp[mul][k][idx];
  if (k == 0)
    return mul == 0;
  if (mul == 0 || k == 0)
    return 0;
  int ret = 0;
  for (int i = idx; i < prime.size() && mul >= prime[i]; i++) {
    ret += rec(mul - prime[i], k - 1, i + 1);
  }
  return dp[mul][k][idx] = ret;
}

int main() {
  setprime();
  int n, k;
  while (cin >> n >> k, n | k) {
    cout << rec(n, k, 0) << endl;
  }
}
Problem p00848 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

vector<int> pn;
vector<bool> is_p;

void enum_prime() {
  is_p.resize(1515, true);
  is_p[0] = is_p[1] = false;
  for (int i = 2; i <= 1515; i++) {
    if (!is_p[i])
      continue;
    pn.push_back(i);
    for (int j = i + i; j <= 1515; j += i)
      is_p[j] = false;
  }
}

int dp[1200][300][15];

int solve(int n, int p, int k) {
  if (k == 0 && n == 0)
    return 1;
  if (k < 0 || n < 0 || p < 0)
    return 0;

  int &ret = dp[n][p][k];
  if (~ret)
    return ret;

  ret = solve(n, p - 1, k);
  if (n >= pn[p])
    ret += solve(n - pn[p], p - 1, k - 1);

  return ret;
}

int main() {
  enum_prime();
  int n, k;
  while (cin >> n >> k, n || k) {
    memset(dp, -1, sizeof(dp));
    int p = upper_bound(pn.begin(), pn.end(), n) - pn.begin() - 1;
    cout << solve(n, p, k) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

vector<int> pn;
vector<bool> is_p;

void enum_prime() {
  is_p.resize(1515, true);
  is_p[0] = is_p[1] = false;
  for (int i = 2; i <= 1515; i++) {
    if (!is_p[i])
      continue;
    pn.push_back(i);
    for (int j = i + i; j <= 1515; j += i)
      is_p[j] = false;
  }
}

int dp[1515][1515][15];

int solve(int n, int p, int k) {
  if (k == 0 && n == 0)
    return 1;
  if (k < 0 || n < 0 || p < 0)
    return 0;

  int &ret = dp[n][p][k];
  if (~ret)
    return ret;

  ret = solve(n, p - 1, k);
  if (n >= pn[p])
    ret += solve(n - pn[p], p - 1, k - 1);

  return ret;
}

int main() {
  enum_prime();
  int n, k;
  while (cin >> n >> k, n || k) {
    memset(dp, -1, sizeof(dp));
    int p = upper_bound(pn.begin(), pn.end(), n) - pn.begin() - 1;
    cout << solve(n, p, k) << endl;
  }
  return 0;
}
Problem p00850 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

const int INF = 1000000000;

int mincost;
bool usable[1100];
int N;

void dfs(int nowNum, int ccost, int maxv) {
  if (ccost >= mincost)
    return;
  else if (nowNum == N) {
    mincost = ccost;
    return;
  } else if (1024 < nowNum)
    return;
  int cnt = 0;
  int tmp = nowNum;
  while (tmp < N) {
    cnt++;
    tmp *= 2;
  }
  if (cnt + ccost >= mincost)
    return;
  for (int i = 1; i <= maxv; i++) {
    if (nowNum + i >= 1024 && nowNum - i <= 1)
      break;
    if (usable[i]) {
      int nxtNum = nowNum + i;
      if (nxtNum <= 1024 && !usable[nxtNum]) {
        usable[nxtNum] = true;
        dfs(nxtNum, ccost + 1, max(nxtNum, maxv));
        usable[nxtNum] = false;
      }
      nxtNum = nowNum - i;
      if (nxtNum >= 2 && !usable[nxtNum]) {
        usable[nxtNum] = true;
        dfs(nxtNum, ccost + 1, max(nxtNum, maxv));
        usable[nxtNum] = false;
      }
    }
  }
}

int main() {
  while (cin >> N && N) {
    memset(usable, 0, sizeof(usable));
    usable[1] = true;
    int popCnt = 0;
    int maxBitPos = 0;
    for (int i = 0; i < 12; i++) {
      if ((N >> i) & 1) {
        popCnt++;
        maxBitPos = i;
      }
    }
    // initialized with upper bound value
    mincost = maxBitPos + popCnt - 1;
    dfs(1, 0, 1);
    cout << mincost << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

const int INF = 1000000000;

int mincost;
bool usable[2100];
int N;
int dp[1001];

void dfs(int nowNum, int ccost, int maxv) {
  if (ccost >= mincost)
    return;
  else if (nowNum == N) {
    mincost = ccost;
    return;
  } else if (2048 < nowNum)
    return;
  int cnt = 0;
  int tmp = nowNum;
  while (tmp < N) {
    cnt++;
    tmp *= 2;
  }
  if (cnt + ccost >= mincost)
    return;
  for (int i = 1; i <= maxv; i++) {
    if (nowNum + i >= 2048 && nowNum - i <= 1)
      break;
    if (usable[i]) {
      int nxtNum = nowNum + i;
      if (nxtNum <= 2048 && !usable[nxtNum]) {
        usable[nxtNum] = true;
        dfs(nxtNum, ccost + 1, max(nxtNum, maxv));
        usable[nxtNum] = false;
      }
      nxtNum = nowNum - i;
      if (nxtNum >= 2 && !usable[nxtNum]) {
        usable[nxtNum] = true;
        dfs(nxtNum, ccost + 1, max(nxtNum, maxv));
        usable[nxtNum] = false;
      }
    }
  }
}

int main() {
  memset(dp, -1, sizeof(dp));
  while (cin >> N && N) {
    if (dp[N] == -1) {
      memset(usable, 0, sizeof(usable));
      usable[1] = true;
      int popCnt = 0;
      int maxBitPos = 0;
      for (int i = 0; i < 12; i++) {
        if ((N >> i) & 1) {
          popCnt++;
          maxBitPos = i;
        }
      }
      // initialized with upper bound value
      mincost = maxBitPos + popCnt - 1;
      dfs(1, 0, 1);
      dp[N] = mincost;
    } else
      mincost = dp[N];
    cout << mincost << endl;
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

int N, M, K;

void solve() {
  vector<int> next_stones(N);
  for (int i = 0; i < N; i++)
    next_stones[i] = (i + 1) % N;

  // first-remove
  int cur = M - 1;
  int pcur = (cur - 1 + N) % N;
  int remained_stone = N;

  while (remained_stone-- > 1) {
    next_stones[pcur] = next_stones[cur];
    for (int i = 0; i < K; i++) {
      pcur = cur;
      cur = next_stones[cur];
    }
  }
  cout << cur + 1 << endl;
}

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> N >> K >> M, N | K | M)
    solve();

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

int N, M, K;

void solve() {
  vector<int> next_stones(N);
  for (int i = 0; i < N; i++)
    next_stones[i] = (i + 1) % N;

  // first-remove
  int cur = M - 1;
  int pcur = (cur - 1 + N) % N;
  int remained_stone = N;

  while (remained_stone-- > 1) {
    next_stones[pcur] = next_stones[cur];
    for (int i = 0; i < K; i++) {
      pcur = cur;
      cur = next_stones[cur];
    }
  }
  cout << cur + 1 << endl;
}

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> N >> M >> K, N | M | K)
    solve();

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

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-12;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

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

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

  while (cin >> N >> M >> K, N) {
    cout << (f(N - 1, M) + K) % N + 1 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

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

  cin >> N >> M >> K;
  while (N) {
    list<int> l;
    for (int i = K; i <= N; i++) {
      l.push_back(i);
    }
    for (int i = 1; i < K; i++) {
      l.push_back(i);
    }
    auto it = l.begin();
    while (l.size() > 1) {
      auto nx = it;
      // cout << *nx << endl;
      nx++;
      if (nx == l.end())
        nx = l.begin();
      l.erase(it);
      int box = M % l.size();
      if (!box) {
        if (nx == l.begin())
          nx = l.end();
        nx--;
      } else if (box * 2 <= l.size()) {
        for (int i = 1; i < box; i++) {
          nx++;
          if (nx == l.end())
            nx = l.begin();
        }
      } else {
        if (nx == l.begin())
          nx = l.end();
        nx--;
        for (int i = l.size(); i > box; i--) {
          if (nx == l.begin())
            nx = l.end();
          nx--;
        }
      }
      it = nx;
    }
    cout << *it << endl;
    cin >> N >> M >> K;
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k, m;

  while (cin >> n >> k >> m, n || k || m) {
    vector<int> stone(n);
    int num = n;
    int idx = m - 1;

    for (int i = 0; i < n; i++)
      stone[i] = i + 1;
    stone.erase(stone.begin() + idx);
    num--;

    while (num > 1) {
      idx = (idx + k - 1) % stone.size();
      stone.erase(stone.begin() + idx);
      num--;
    }
    cout << stone[0] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, k, m;
  bool stone[10010];

  while (cin >> n >> k >> m, n || k || m) {
    int idx = --m;
    int num = n;

    for (int i = 0; i < n; i++)
      stone[i] = true;

    stone[m] = false;
    num--;

    while (num > 1) {
      int cnt = 0;

      while (cnt < k) {
        idx = (idx + 1) % n;
        if (stone[idx])
          cnt++;
      }
      stone[idx] = false;
      num--;
    }

    for (int i = 0; i < n; i++)
      if (stone[i]) {
        cout << i + 1 << endl;
        break;
      }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int solve(vector<int> &v, int k, int start, int last) {
  int n = v.size();
  if (n == 0)
    return last;
  int next = (start + k + n - 1) % n;
  last = v[next];
  v.erase(v.begin() + next);
  return solve(v, k, next, last);
}
int main() {
  int n, k, m;
  int numarray[10001];
  while (1) {
    cin >> n >> k >> m;
    if (n == 0 && k == 0 && m == 0)
      break;
    vector<int> v;
    for (int i = 0; i < n; i++) {
      if (i != m - 1)
        v.push_back(i + 1);
    }
    // numarray = {0, 1, 2, 3, 4, 5, ...}
    int ans;
    ans = solve(v, k, m - 1, m);
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int solve(int array[], int n, int k, int start, int rest);

int main() {
  int n, k, m;
  int numarray[10001];
  while (1) {
    cin >> n >> k >> m;
    if (n == 0 && k == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      if (i == m - 1)
        numarray[i] = -1;
      else
        numarray[i] = i;
    }
    // numarray = {0, 1, 2, 3, 4, 5, ...}
    int ans;
    ans = solve(numarray, n, k, m - 1, n - 1);
    cout << ans + 1 << endl;
  }
}

int solve(int array[], int n, int k, int start, int rest) {
  if (rest == 0)
    return start;

  int nextnum, cnt = 0;
  nextnum = (start + 1) % n;
  int K = k % rest;
  if (K == 0)
    K = rest;
  while (1) {
    if (array[nextnum] != -1)
      cnt++;
    if (cnt == K)
      break;
    nextnum = (nextnum + 1) % n;
  }
  array[nextnum] = -1;
  if (nextnum == start)
    return start;
  else
    return solve(array, n, k, nextnum, rest - 1);
}