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

#include <iostream>

using namespace std;

bool search(int p[], int N) {
  int count = 0;
  for (int i = 0; i < N; ++i) {
    if (p[i] != 0) {
      count++;
    }
  }
  if (count == 1) {
    return true;
  } else {
    return false;
  }
}

int main(void) {
  int n, p;
  while (cin >> n >> p) {
    if ((n == 0) && (p == 0)) {
      return 0;
    }
    int *people = new int[n];
    for (int i = 0; i < n; ++i) {
      people[i] = 0;
    }
    // finish initialization
    int turn = 0;
    while (1) {
      if (p >= 1) {
        ++people[turn];
        --p;
        if (p == 0) {
          if (search(people, n) == true) {
            break;
          }
        }
      } else { // p<=0]
        p = people[turn];
        people[turn] = 0;
      }
      if (turn == (n - 1)) {
        turn = 0;
      } else {
        ++turn;
      }
    }
    cout << turn << endl;
    delete[] people;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

bool search(int p[], int N) {
  int count = 0;
  for (int i = 0; i < N; ++i) {
    if (p[i] != 0) {
      count++;
    }
  }
  if (count == 1) {
    return true;
  } else {
    return false;
  }
}

int main(void) {
  int n, p;
  while (cin >> n >> p) {
    int *people = new int[n];
    for (int i = 0; i < n; ++i) {
      people[i] = 0;
    }
    // finish initialization
    int turn = 0;
    while (1) {
      if (p >= 1) {
        ++people[turn];
        --p;
        if (p == 0) {
          if (search(people, n) == true) {
            break;
          }
        }
      } else { // p<=0]
        p = people[turn];
        people[turn] = 0;
      }
      if (turn == (n - 1)) {
        turn = 0;
      } else {
        ++turn;
      }
    }
    cout << turn << endl;
    delete[] people;
  }
  return 0;
}
Problem p00740 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int n, p;
  while (1) {
    cin >> n >> p;
    if (n == 0 && p == 0)
      break;
    int pmax = p;
    int s[50] = {0};
    int i = 0;
    while (1) {
      if (p > 0) {
        s[i]++;
        p--;
      } else if (p == 0) {
        p = s[i];
        s[i] = 0;
      }

      if (s[i] == pmax)
        break;

      if (i == n - 1) {
        i = 0;
      } else {
        i++;
      }
    }
    cout << i << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, p;
  while (1) {
    cin >> n >> p;
    if (n == 0 && p == 0)
      break;
    int pmax = p;
    int s[n];
    for (int i = 0; i < n; i++) {
      s[i] = 0;
    }
    int i = 0;
    while (1) {
      if (p > 0) {
        s[i]++;
        p--;
      } else if (p == 0) {
        p = s[i];
        s[i] = 0;
      } else if (s[i] == pmax) {
        break;
      }

      if (i = n - 1)
        i = 0;
      else
        i++;
    }
    cout << i << endl;
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
#define ll long long
#define all(a) a.begin(), a.end()
#define ull unsigned long long
#define pb(x) push_back(x)
#define dbg(x) cout << #x << " : " << (x) << endl;
using namespace std;
const int INF = 1500000000;
const double PI = 3.14159265359;
const int mod = 1000000000 + 7;

const int MAX_N = 50, MAX_P = 50;
int n, p, res, v[MAX_N];

void solve() {
  rep(i, n) v[i] = 0;
  int c = 0;
  while (true) {
    if (p == 0) {
      p = v[c];
      v[c] = 0;
    } else {
      p--;
      v[c]++;
      if (p == 0) {
        bool flg = true;
        rep(i, n) if (i != c && v[i] > 0) flg = false;
        if (flg)
          break;
      }
    }
    c = (c == n - 1) ? 0 : c + 1;
    res = c;
  }
}

int main() {
  while (true) {
    cin >> n >> p;
    if (!n && !p)
      break;
    solve();
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
#define ll long long
#define all(a) a.begin(), a.end()
#define ull unsigned long long
#define pb(x) push_back(x)
#define dbg(x) cout << #x << " : " << (x) << endl;
using namespace std;
const int INF = 1500000000;
const double PI = 3.14159265359;
const int mod = 1000000000 + 7;

const int MAX_N = 50, MAX_P = 50;
int n, p, res, v[MAX_N];

void solve() {
  memset(v, 0, n);
  int c = 0;
  while (true) {
    if (p == 0) {
      p = v[c];
      v[c] = 0;
    } else {
      p--;
      v[c]++;
      if (p == 0) {
        bool flg = true;
        rep(i, n) if (i != c && v[i] > 0) flg = false;
        if (flg)
          break;
      }
    }
    c = (c == n - 1) ? 0 : c + 1;
    res = c;
  }
}

int main() {
  while (true) {
    cin >> n >> p;
    if (!n && !p)
      break;
    solve();
    cout << res << endl;
  }
  return 0;
}
Problem p00740 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, p;
  int c[50];
  for (;;) {
    cin >> n >> p;
    if (!n && !p)
      break;
    for (int i = 0; i < n; i++)
      c[i] = 0;

    int pos = -1;
    for (;;) {
      pos = (pos + 1) % n;
      if (p) {
        c[pos]++;
        p--;
      } else {
        p = c[pos];
        c[pos] = 0;
      }
      if (!p) {
        int cnt = 0;
        for (int i = 0; i < n; i++)
          if (c[i])
            cnt++;
        if (cnt == 1)
          break;
      }
    }
    cout << pos << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, p;
  int c[50];
  for (;;) {
    cin >> n >> p;
    if (!n && !p)
      break;
    for (int i = 0; i < n; i++)
      c[i] = 0;

    int cnt = 0;
    int pos = -1;
    while (cnt != 1 || p) {
      pos = (pos + 1) % n;
      if (p) {
        if (!c[pos])
          cnt++;
        c[pos]++;
        p--;
      } else {
        if (!c[pos])
          cnt--;
        p = c[pos];
        c[pos] = 0;
      }
      pos = (pos + 1) % n;
    }
    cout << pos << endl;
  }
}
Problem p00740 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sort(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
typedef pair<int, int> p;
typedef pair<ll, ll> lp;
bool is_uruu(int y) { return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); }
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------//
int n, b;
int main() {
  while (1) {
    cin >> n >> b;
    if (n == 0)
      return 0;
    int index = 0;
    int s[n];
    for (int i = 0; i < n; i++) {
      s[i] = 0;
    }
    int stone = b;
    while (1) {
      if (stone != 0) {
        stone--;
        s[index] += 1;
      } else if (stone == 0) {
        stone += s[index];
        s[index] = 0;
      }
      if (s[index] == b) {
        break;
      }
      if (index == n - 1) {
        index = 0;
      } else {
        index++;
      }
    }
    cout << index << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sort(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
typedef pair<int, int> p;
typedef pair<ll, ll> lp;
bool is_uruu(int y) { return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); }
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------//
int n, s, q, now = 0;
int main() {
  while (1) {
    cin >> n >> s;
    if (n == 0)
      return 0;
    deque<int> r(n, 0);
  out:
    while (1) {
      if (s != 0) {
        q = r.front() + 1;
        r.pop_front();
        r.push_back(q);
        s--;
        now++;
        if (now % n == 0) {
          now = 0;
        }
      } else if (s == 0) {
        int cunt = 0;
        for (int i = 0; i < n; i++) {
          if (r[i] != 0) {
            cunt++;
          }
        }
        if (cunt == 1) {
          cout << now << endl;
          goto out;
        }
        q = r.front();
        r.pop_front();
        r.push_back(0);
        s += q;
        now++;
        if (now % n == 0) {
          now = 0;
        }
      }
    }
  }
}
Problem p00740 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define RFOR(i, m, n) for (int i = m - 1; i >= n; --i)
#define ALL(v) v.begin(), v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    int n, p;
    cin >> n >> p;
    if (n == 0)
      break;
    int *m = new int[n];
    REP(i, n) m[i] = 0;
    for (int i = 0;; ++i) {
      int j = i % n;
      if (p == 0) {
        p += m[j];
        m[j] = 0;
      } else {
        p--;
        m[j]++;
        if (p == 0) {
          int flag = 1;
          for (int k = 0; k < n; ++k) {
            if (m[k] != 0 && k != j)
              flag = 0;
          }
          if (flag) {
            cout << j << endl;
            break;
          }
        }
      }
    }
    delete[] m;
  }
  return 0;
}

Error: -8

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define RFOR(i, m, n) for (int i = m - 1; i >= n; --i)
#define ALL(v) v.begin(), v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, p;
  while (1) {
    cin >> n >> p;
    int *m = new int[n];
    for (int i = 0;; ++i) {
      int j = i % n;
      if (p == 0) {
        p += m[j];
        m[j] = 0;
      } else {
        p--;
        m[j]++;
        if (p == 0) {
          int flag = 1;
          for (int k = 0; k < n; ++k) {
            if (m[k] != 0 && k != j)
              flag = 0;
          }
          if (flag) {
            cout << j << endl;
            break;
          }
        }
      }
    }
  }
  return 0;
}
Problem p00740 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
#define N 50

int main() {
  while (1) {
    int n, m;
    int ca[N];
    cin >> n >> m;
    if (!n && !m)
      break;
    fill(ca, ca + n, 0);

    int wan = m;
    int k = 0;
    while (1) {
      while (wan) {
        ca[k]++;
        k++;
        k = k % n;
        wan--;
      }
      if ((k == 0 && ca[n - 1] == m) || (ca[k - 1] == m)) {
        break;
      } else {
        wan = ca[k];
        ca[k] = 0;
        k++;
        k = k % n;
      }
    }
    if (!k) {
      cout << n - 1 << endl;
    } else {
      cout << k - 1 << endl;
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define N 50

int main() {
  while (1) {
    int n, m;
    int ca[N];
    cin >> n >> m;
    if (!n && !m)
      break;
    fill(ca, ca + n, 0);

    int wan = m;
    int k = 0;
    while (1) {
      while (wan) {
        ca[k]++;
        k++;
        wan--;
      }
      if (ca[k - 1] == m) {
        break;
      } else {
        wan = ca[k - 1];
        ca[k - 1] = 0;
      }
    }
    cout << k - 1 << endl;
  }
  return 0;
}
Problem p00740 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int n, r;
  while (cin >> n >> r, n) {
    int p[n];
    fill(p, p + n, 0);
    int s = r;
    for (int i = 0;; i++, i %= n) {
      if (p[i] == r) {
        cout << i << endl;
        break;
      } else {
        if (s == 0) {
          s += p[i];
          p[i] = 0;
        } else {
          p[i]++;
          s--;
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int n, r;
  while (cin >> n >> r, n) {
    int p[n];
    fill(p, p + n, 0);
    int s = n;
    for (int i = 0;; i++, i %= n) {
      if (p[i] == r) {
        cout << i << endl;
        break;
      } else {
        if (s == 0) {
          s += p[i];
          p[i] = 0;
        } else {
          p[i]++;
          s--;
        }
      }
    }
  }
}
Problem p00740 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, p;
  int mayor[50], id, num;

  for (;;) {
    cin >> n >> p;
    if (n == 0 && p == 0)
      break;

    for (int i = 0; i < n; i++)
      mayor[i] = 0;
    id = 0;
    num = p;

    for (;;) {
      if (num) {
        mayor[id]++;
        num--;
      } else {
        num += mayor[id];
        mayor[id] = 0;
      }

      if (num == 0 && mayor[id] == p) {
        cout << id << endl;
        break;
      }

      id = (id + 1) % n;
    }
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;

int main() {
  int n, p;
  int c[50];
  for (;;) {
    scanf("%d,%d", &n, &p);
    if (!n && !p)
      break;
    for (int i = 0; i < n; i++)
      c[i] = 0;

    int cnt = 0;
    int pos = -1;
    for (;;) {
      pos = (pos + 1) % n;
      if (p) {
        if (!c[pos])
          cnt++;
        c[pos]++;
        p--;
      } else {
        if (c[pos])
          cnt--;
        p = c[pos];
        c[pos] = 0;
      }
      if (!p && cnt == 1)
        break;
    }
    printf("%d\\n", pos);
  }
}
Problem p00740 (C++) — buggy submission, verdict: Runtime Error


#include <iostream>

using namespace std;

int main() {
  int n, p;
  while (cin >> n >> p, (n || p)) {
    int a[n] = {0};
    int i = 0;
    while (true) {
      if (p == 0) {
        p = a[i];
        a[i] = 0;
      } else {
        p--;
        a[i]++;
        bool isZero = 0;
        if (p == 0) {
          for (int j = 0; j < n; j++) {
            if (a[j] != 0 && i != j) {
              isZero = 1;
            }
          }
          if (isZero == 0) {
            cout << i << endl;
            break;
          }
        }
      }
      i += 1;
      i %= n;
    }
  }
}


Error: -8

Fixed submission:


#include <cstdio>

int main() {
  int n, p;
  while (true) {
    scanf("%d%d", &n, &p);
    if (n == 0 && p == 0) {
      break;
    }
    int can[n];
    int q = p;
    for (int i = 0;; i++) {
      int k = n % i;
      if (q != 0) {
        can[k]++;
        q--;
        if (can[k] == p) {
          printf("%d", k);
          break;
        }
      } else {
        q = can[k];
        can[k] = 0;
      }
    }
  }
}
Problem p00740 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main(void) {
  while (1) {
    int stone[50];
    for (int k = 0; k < 50; k++) {
      stone[k] = 0;
    }

    int bowl;
    int n, p; // n：候補者人数，p：小石の総数
    cin >> n;
    cin >> p;

    if (n == 0 && p == 0) {
      return 0;
    }

    bowl = p;

    bool flg = true;
    int ans;

    for (int i = 0; flg; i = (i + 1) % n) {

      if (bowl == 0) {
        bowl = bowl + stone[i];
        stone[i] = 0;
      } else {
        stone[i]++;
        bowl--;
        if (bowl == 0 && stone[i] == p) {
          ans = i;
          flg = false;
        }
      }
    }

    for (int i = 0; i < 1000000; i++) {
      for (int j = 0; j < 1000000; j++) {
        int k = i + j;
      }
    }

    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <unistd.h>
using namespace std;

int main(void) {
  while (1) {
    int stone[50];
    for (int k = 0; k < 50; k++) {
      stone[k] = 0;
    }

    int bowl;
    int n, p; // n：候補者人数，p：小石の総数
    cin >> n;
    cin >> p;

    if (n == 0 && p == 0) {
      return 0;
    }

    bowl = p;

    bool flg = true;
    int ans;

    for (int i = 0; flg; i = (i + 1) % n) {

      if (bowl == 0) {
        bowl = bowl + stone[i];
        stone[i] = 0;
      } else {
        stone[i]++;
        bowl--;
        if (bowl == 0 && stone[i] == p) {
          ans = i;
          flg = false;
        }
      }
    }

    usleep(1000000);

    cout << ans << endl;
  }
}
Problem p00740 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int main() {

  int player[51] = {0};
  int n, p, pool = 0, i;
  for (;;) {
    scanf("%d %d", &n, &p);

    if (n == 0 && p == 0) {
      break;
    }
    pool = p;
    i = 0;
    for (;;) {
      for (i = 0; i <= n - 1; i++) {
        if (pool == 0) {
          pool = player[i];
          player[i] = 0;
          continue;
        }
        if (pool == 1 && player[i] == p - 1) {
          break;
        }

        if (pool != 0) {
          pool = pool - 1;
          player[i] += 1;
        }
      }
      if (pool == 1 && player[i] == p - 1) {
        break;
      }
      i++;
    }
    printf("%d\\n", i);
    pool = 0;
    player[i] = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {

  int player[51];
  int n, p, pool = 0, i;
  for (;;) {
    scanf("%d %d", &n, &p);

    if (n == 0 && p == 0) {
      break;
    }
    pool = p;

    for (;;) {

      for (i = 0; i <= n - 1; i++) {
        if (pool == 0) {
          pool = player[i];
          player[i] = 0;
          continue;
        }
        if (pool == 1 && player[i] == p - 1) {
          break;
        }

        if (pool != 0) {
          pool = pool - 1;
          player[i] += 1;
        }
      }
      if (pool == 1 && player[i] == p - 1) {
        break;
      }
    }
    printf("%d\\n", i);
    pool = 0;
    player[i] = 0;
  }
  return 0;
}
Problem p00740 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[100];
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m), (n + m)) {
    memset(a, 0, sizeof(a));
    int ans = m, k = 0, flag = 0;
    while (1) {
      for (int i = 0; i < n; i++) {
        if (ans > 0) {
          ans--;
          a[i]++;
        } else {
          if (a[i] > 0) {
            ans = a[i];
            a[i] = 0;
          }
        }
        if (a[i] == m) {
          k = i;
          flag = 1;
          break;
        }
      }
      if (flag == 1)
        break;
    }
    printf("%d\\n", k);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[100];
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    memset(a, 0, sizeof(a));
    int ans = m, k = 0, flag = 0;
    while (1) {
      for (int i = 0; i < n; i++) {
        if (ans > 0) {
          ans--;
          a[i]++;
        } else {
          if (a[i] > 0) {
            ans = a[i];
            a[i] = 0;
          }
        }
      }
      for (int i = 0; i < n; i++) {
        if (a[i] == m) {
          k = i;
          flag = 1;
          break;
        }
      }
      if (flag == 1)
        break;
    }
    printf("%d\\n", k);
  }
  return 0;
}
Problem p00740 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;
static const int MAXV = 1000;

int main() {
  while (1) {
    int n, m, p, M[MAXV] = {}, i = 0;
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    p = m;
    while (1) {
      // printf("%d   %d   %d\\n", M[0], M[1], M[2]);
      if (p == 0) {
        p = M[i];
        M[i] = 0;
      } else {
        M[i]++;
        p--;
      }
      if (M[i] == m)
        break;
      i++;
      if (i == n)
        i = 0;
    }
    printf("%d\\n", i);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;
static const int MAXV = 1000;

int main() {
  while (1) {
    int n, m, p, M[MAXV] = {}, i = 0;
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    p = m;
    while (1) {
      printf("%d   %d   %d\\n", M[0], M[1], M[2]);
      if (p == 0) {
        p = M[i];
        M[i] = 0;
      } else {
        M[i]++;
        p--;
      }
      if (M[i] == m)
        break;
      i++;
      if (i == n)
        i = 0;
    }
    printf("%d\\n", i);
  }
}
Problem p00740 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
// #include <algorithm>
// #include <vector>
// #include <stack>
// #include <queue>
// #include <map>
// #include <cmath>
// #include <string>
// #include <sstream>
// #include <iomanip>
// #include <complex>
using namespace std;

// #define ll long long
// #define vvi vector< vector<int> >
// #define All(X) X.begin(),X.end()
// #define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
// #define pb push_back
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int main() {
  while (1) {
    int n, p;
    cin >> n >> p;
    if (n == 0 && p == 0)
      break;
    int kago = p;
    int pc[100] = {0};
    int i = 0;
    while (1) {
      int nowc = i % n;
      if (pc[nowc] == p) {
        cout << nowc << endl;
        break;
      }
      if (kago > 0) {
        kago--;
        pc[nowc]++;
      } else {
        kago = pc[nowc];
        pc[nowc] = 0;
      }
      i++;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
// #include <algorithm>
// #include <vector>
// #include <stack>
// #include <queue>
// #include <map>
// #include <cmath>
// #include <string>
// #include <sstream>
// #include <iomanip>
// #include <complex>
using namespace std;

// #define ll long long
// #define vvi vector< vector<int> >
// #define All(X) X.begin(),X.end()
// #define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
// #define pb push_back
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int main() {
  while (1) {
    int n, p;
    cin >> n >> p;
    if (n == 0 && p == 0)
      break;
    int kago = p;
    int pc[100] = {0};
    int i = 0;
    while (1) {
      int k = i % (n - 1);
      if (pc[k] == p) {
        cout << k << endl;
        break;
      }
      if (kago > 0) {
        kago--;
        pc[k]++;
      } else {
        kago = pc[k];
        pc[k] = 0;
      }
      i++;
    }
  }

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

#include <iostream>
using namespace std;
int main() {
  while (1) {
    int n, p;
    cin >> n >> p;
    if (n == 0 && p == 0)
      break;
    int k = p;
    int ch[50] = {}, f = 0;
    while (1) {
      if (k == 0) {
        k = ch[f % n];
        ch[f % n] = 0;
      } else {
        ch[f % n]++;
        k--;
      }
      if (ch[f % n] == p)
        break;
      f++;
    }
    cout << f % n << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  while (1) {
    int n, p;
    if (n == 0 && p == 0)
      break;
    cin >> n >> p;
    int k = p;
    int ch[51] = {}, f = 0;
    while (1) {
      if (k == 0) {
        k = ch[f % n];
        ch[f % n] = 0;
      } else {
        ch[f % n]++;
        k--;
      }
      if (ch[f % n] == p)
        break;
      f++;
    }
    cout << f % n << endl;
  }
  return 0;
}