Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

func main() {
	for sc.Scan() {
		if sc.Text() == "0 0" {
			break
		}
		chunks := strings.Split(sc.Text(), " ")
		n, _ := strconv.Atoi(chunks[0])
		p, _ := strconv.Atoi(chunks[1])
		cnts := make([]int, n, n)
		for i := 0; true; i++ {
			j := i % n
			if p == 0 {
				p, cnts[j] = cnts[j], 0
			} else {
				cnts[j]++
				p--
			}

			if p == 0 {
				noneCnt := 0
				winner := 0
				for j, c := range cnts {
					if c == 0 {
						noneCnt++
					} else {
						winner = j
					}
				}
				if noneCnt == n-1 {
					fmt.Println(winner)
					break
				}
			}
		}
	}
}

C++: #include<iostream>

using namespace std;

int main() {
    while(1) {
        int n, p;
        cin >> n >> p;
        if (n * p == 0) break;

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

C++: #include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF (1<<29)
	
int main(){
	int n,p;
	int a[50];
	while(cin>>n>>p,p|n){
		int m = p;
		memset(a,0,sizeof(a));
		int i;
		for(i=0;;i=(i+1)%n){
			if(m){
				a[i]++;
				m--;
				if(a[i]==p)break;
			}else{
				if(a[i]){
					m += a[i];
					a[i]=0;
				}
			}
		}
		cout<<i<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define LOOP(i,x,n) for(int i=x;i<n;i++)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define MP make_pair
#define FR first
#define SC second
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=9000000009;


signed main(){
  int n,p;
  while(cin>>n>>p,n!=0){
    int sum=p;
    vector<int> x(n);
    for(int i=0;;){
      if(p){
        x[i%n]++;
        p--;
      }else{
        p+=x[i%n];
        x[i%n]=0;
      }
      if(x[i%n]==sum){
        cout<<i%n<<endl;
        break;
      }
      i++;
    }
  }
  return 0;
}

C++: #include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

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

	for(int n, p; cin >> n >> p, n;) {
		vector<int> num(n, 0);
		int stone = p;

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

	return EXIT_SUCCESS;
}

C++: #include <iostream>
using namespace std;

void main() {
	int n, p;

	while (1) {
		int flag = 0;
		int player[50] = { 0 };
		cin >> n >> p;
		if (n == 0 && p == 0) break;
		int p_max = p;

		while (1)
		{
			for (int j = 0; j < n; j++) {
				if (p != 0) player[j] += 1, p--;
				else p = player[j], player[j] = 0;
				if (player[j] == p_max) {
					flag = -1;
					cout << j << endl;
					break;
				}
			}
			if (flag == -1) break;
		}
	}
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
 
using namespace std;

int main()
{
    int n, p;
    while(1){
        cin >> n >> p;
        if(n == 0 && p == 0) break;
        int candidate[50] = {0};
        int start = 0;
        int bowlin = p;
        while(1){
            for(int i = 0; i < n; i++){
                candidate[i] += bowlin / n;
                if(i < bowlin % n) candidate[(start + i) % n]++;
            }
            int end = (start + bowlin % n) % n;
            while(candidate[end] == 0) end = (end + 1) % n;
            bowlin = candidate[end];
            candidate[end] = 0;
            start = (end + 1) % n;
            if(candidate[start] == p - 1){
                cout << start << endl;
                break;
            }
        }
    }
   return 0;
}

C++: #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;}
      }
    }

    cout << ans << endl;
  }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>

using namespace std;

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

#define reps(i,f,n) for(int i = int(f); i <= int(n); i++)
#define rep(i,n) reps(i,0,int(n)-1)
#define rrep(i,n) for(int i = n-1; i >= 0; i--)

#define all(x) (x).begin(),(x).end()
#define X first
#define Y second

#define sz size()
#define eb emplace_back



int memo[64][64];

int main(void){
  while(1){
    int n,p;
    int pl[64] = {0};
    int ans = -1;
    int bowl;
    cin >> n >> p;
    bowl = n;
    if(n+p==0)break;
    if(memo[n][p] == 0){
      bowl = p;
      rep(i,1000000){
        if(bowl){
          bowl--;
          pl[(i%n)]++;
          if(pl[i%n] == p){ans = i%n;break;}
        }else{
          bowl = pl[i%n];
          pl[i%n] = 0;
        }
      }
      cout << ans << endl;
      memo[n][p] = ans;
    }else{
      cout << memo[n][p] << endl;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-11;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int f(int i,int x){
	return i*(100+x)/100;
}

int main(){
	while(1){
		int n,m;
		cin>>n>>m;
		if(n==0) break;
		vi a(n);
		int t=m,id=0;
		while(1){
			if(t>0){
				a[id]++,t--;
				if(a[id]==m){
					cout<<id<<endl;
					break;
				}
			}
			else{
				t+=a[id];
				a[id]=0;
			}
			id=(id+1)%n;
		}
	}
}

C++: // AOJ 1159
#include<iostream>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;

const int NMAX = 60;

int solve(int n, int p) {
  int candidates[NMAX];
  REP(i, n) { candidates[i] = 0; }
  int cup = p;
  int i = 0;
  while (true) {
    if (cup > 0) {
      cup--;
      candidates[i]++;
      if (candidates[i] == p) return i;
    } else {
      cup += candidates[i];
      candidates[i] = 0;
    }
    i = (i + 1) % n;
  }
}


int main() {
  int n, p;
  while (cin >> n >> p and n) {
    cout << solve(n, p) << endl;
  }
}