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>
#include<vector>
using namespace std;

int main(){
    while(true){
    int n=0;
    cin>>n;
    int p;cin>>p;
    if(n==0&&p==0)break;
    vector<int> v(1100000,0);
    bool flag=false;
    int win;
    int P=p;
    while(1){
        for(int i=0;i<n;i++){
            if(p){
                v[i]++;
                p--;
                if(p==0&&P==v[i]){
                    win=i;
                    flag=true;break;
                }
            }
            else{
                p+=v[i];
                v[i]=0;
            }
        }
        if(flag)break;
    }
    cout<<win<<endl;
    }
}

C++: #include "bits/stdc++.h"

using namespace std;

#define ll long long
#define vi  vector<int>
#define vvi vector<vi>
#define pi  pair<int,int>
#define mp  make_pair
#define pb  push_back
#define MOD 1e9 + 7
#define PAI  3.1415
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
#define pr(x) cout << x << endl
#define Endl endl
#define rep(i,n) for(int i = 0 ; i < n; i++)

const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
const int ddx[8] = {-1,0,1,-1,1,-1,0,1};
const int ddy[8] = {-1,-1,-1,0,0,1,1,1};
const int inf = 99999999;
const ll linf = 1LL << 62;

ll gcd(ll a,ll b){
  if(a < b)swap(a , b);
  if(a % b == 0) return b;
  else gcd(b, a%b);
}

ll lcm(ll a,ll b){
  if(a < b)swap(a , b);
  return (a/gcd(a , b))*b;
}

struct UnionFind {
  vi par; //par[i] : iの親番号（例）par[3] = 2 : 3の親は2

  UnionFind(int N) : par(N) { //最初は全てが根として初期化
    rep(i,N) par[i] = i;
  }

  int root(int x){ //データxが属する木の根を再帰で得る : root(x) = {xの木の根}
    if(par[x] == x)return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y){ //xとyの木を併合
    int rx = root(x); //xの根をrx
    int ry = root(y); //yの根をry
    if(rx == ry)return; //xとyの根が同じ
    par[rx] = ry; //xとyの根が同じでない時、xの根rxをyの根にryにつける。
  }

  bool same(int x, int y){//2つのデータx，yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(){

  int n,p;

  while(cin >> n,n){
    cin >> p;
    vi v(n,0);
    int pos = 0;

    while(1){
      if(p > 0){
        v[pos]++;
        p--;
      }
      else{
        p += v[pos];
        v[pos] = 0;
      }
      if(v[pos] == n){
        cout << pos << endl;
        break;
      }
      pos = (pos+1) % n;
    }
  }
  return 0;

}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

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

  int n,p;
  while(true){
    cin >> n >> p;
    if(n==0 && p==0) break;
    int stone=p;
    int kouho[n];
    for(int i=0;i<n;i++) kouho[i]=0;
    int pos=0;
    while(true){
      if(stone>0){
        stone--;
        kouho[pos]+=1;
        if(stone==0 && kouho[pos]==p) break;
      }
      else if(stone==0){
        stone+=kouho[pos];
        kouho[pos]=0;
      }
      pos++;
      pos %= n;
    }
    cout << pos << endl;
  }
}

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

int solve(int n,int p){
  int stones[100];
  fill(stones,stones+100,0);
  int cnt = 0;
  int haveStoneMan = 0;
  while(true){
    if (p > 0){
      if(stones[cnt] == 0)haveStoneMan++;
      stones[cnt] += 1;
      p--;
      if(p == 0 && haveStoneMan == 1){
        return cnt;
      }
    }else{
      if(stones[cnt] > 0)haveStoneMan--;
      p = stones[cnt];
      stones[cnt] = 0;
    }
    cnt = (cnt+1) % n;
  }
}


int main(){
  int a,b;
  while(true){
    cin >> a >> b;
    if(a == 0 && b == 0)break;
    cout << solve(a,b) << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

typedef long long LL;
#define rep(i ,n) for(int i=0;i<n;i++)
#define MOD 1000000007
#define all(v) (v).begin(),(v).end()
#define arycpy(ary1,ary2,N) memcpy(ary2,ary1,sizeof(int)*N) //ary1:???????????? ary2:????????????
typedef vector<int> vi;


int main(){
	int n, p;
	while (cin >> n >> p){
		if (n == 0&&p == 0) break;
		vi num(n, 0);
		int i = 0;
		int pp = p;
		while (1){

			if (p > 0){
				p--;
				num[i]++;
				if (p == 0 && num[i] == pp) break;
			} else{
				if (num[i] > 0){
					p += num[i];
					num[i] = 0;
				}
			}
			i++;
			i = i%n;
		}
		

		for (int j = 0; j < n; j++){
			if (num[j]==p){
				cout << i  << endl;
				break;
			}
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <algorithm>
#include <numeric>
#include <utility>
#include <complex>
#include <memory>
#include <functional>
using namespace std;
#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < (int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define INF 1<<25
#define pb push_back
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef pair<long,long> pll;
typedef long long ll;

int main(int argc, char* argv[])
{
    
    while(1){
        int n,p;
        int cnt=0,ans;
        cin >> n >> p;
        
        if(n == 0 && p == 0){
            return 0;
        }
        int stone[50] = {0};
        
        while(1){
            
            for (int i=0; i<n; i++) {
                if(p != 0){
                    p--;
                    stone[i]++;
                }else{
                    p = stone[i];
                    stone[i] =0;
                }
                if(stone[i] == 0){
                    cnt++;
                }else{
                    ans=i;
                }
            }
            if(cnt == n-1 && p==0){
                cout << ans << endl;
                break;
            }
            cnt =0;
            ans = 0;
                
        }
    }
    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
using namespace std;

void solve(int n, int p)
{
	vector<int> num(n, 0);
	int bowl = p;

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

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

		solve(n, p);
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int main(void){
int n,p;
while(1){
	cin>>n>>p;
	if(n==0)break;
	vector<int> a(n,0);
	int stock=p;
	bool flag=false;
	while(1){
		for(int i=0;i<n;i++){
			if(stock==0){
				stock=a[i];
				a[i]=0;
			}else{
				a[i]++;
				stock--;
			}
			if(a[i]==p){
				cout<<i<<endl;
				flag=true;
				break;
			}
		}
		if(flag)break;
	}
}

	return 0;
}