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(){
  int P,N,ndata[55],wan,lop,a;
  while(1){
    cin>>N;
    cin>>P;
    if(N==0&&P==0){
      break;
    }
    for(lop=0;lop<N;lop++){
      ndata[lop]=0;
    }
    lop=0;
    wan=P;
    while(1){
      /*if(N==3&&P==3){
        lop=0;
        cout<<lop<<endl;
        break;
      }*/
      for(lop=0;lop<N;lop++){
        if(wan!=0){
          wan--;
          ndata[lop]++;
        }else{
          wan=wan+ndata[lop];
          ndata[lop]=0;
        }
        /*cout<<"wan="<<wan<<endl;
        for(int lop2=0;lop2<N;lop2++){
          cout<<lop2<<"  "<<ndata[lop2]<<endl;
        }
        cout<<endl;
        cin>>a;*/
        if(ndata[lop]==P&&wan==0){
          break;
        }
      }
      if(ndata[lop]==P&&wan==0){
        cout<<lop<<endl;
        break;
      }
    }

  }
  return 0;
}

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

int main(int argc, char const *argv[])
{
    int n, p;
    int candidate[MAX];

    while (cin>>n>>p, n&&p){
        int remains = p;
        int turn = 0;
        int winner = -1;
        for (int i = 0; i < MAX; i++){
            candidate[i] = 0;
        }
        while (1){
            if (remains > 0){
                candidate[turn]++;
                remains--;
            }
            else{
                remains += candidate[turn];
                candidate[turn] = 0;
            }

            if (candidate[turn] == p){
                winner = turn;
                break;
            }
            turn++;
            turn%=n;
        }
        cout<<winner<<endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for (int i=(n)-1;i>=0;i--)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define REPS(i,f,n) for (int i=(f)-1;i>=(n);i--)
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};

void slove(int n, int p){
	int tyawan = p;//??¶?¢??????\??£???????????°
	vint person(n);//????£????????????£???????????°
	rep(i, n) person[i] = 0;//?????????
	int cnt = 0;
	do{
		if (tyawan >= 1){//????????¨???
			person[cnt % n]++;//?????????????????£??????????????¨???????????????mod??§
			tyawan--;
		}else{//???????????¨??¨??\??????
			tyawan = person[cnt % n];
			person[cnt % n] = 0;
		}
		cnt++;

	} while (person[cnt % n] + tyawan != p);//?¬???????????????±??§??????

	printf("%d\n", cnt % n);
	return;
}

int main(void){
	int n, p;
	scanf("%d %d", &n, &p);
	while(n != 0){
		slove(n, p);
		scanf("%d %d", &n, &p);
	}
	return 0;
}

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

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, P;
int c[64];

int main(){
  while(scanf("%d%d", &N, &P) && N+P){
    memset(c, 0, sizeof(c));
 
    int n = 0, w = P;
    while(1){
      if(w){
	w--;
	c[n]++;
      }else{
	w = c[n];
	c[n] = 0;
      }

      if(c[n] == P){
	printf("%d\n", n);
	break;
      }
      n++;
      n %= N;
    }
  }
  return 0;
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cassert>

using namespace std;


bool solve(){
	while(1){
		int n,p;
		cin>> n>> p;
		if(n==0 && p==0) break;
		
		vector<int> a(n,0);
		int k=0;
		while(1){
			if(p){
				a[k]++;
				p--;
				if(p==0){
					int flag=1;
					for(int i=0;i<n;i++){
						if(i==k) continue;
						if(a[i]){
							flag=0;
							break;
						}
					}
					if(flag) break;
				}
			}else{
				p += a[k];
				a[k]=0;
			}
			k++;
			k %= n;
		}
		cout<< k<< endl;
	}
	
	return true;
}

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

	return 0;
}

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

int main(){
	int n, p;
	while(cin >>n >>p){
		if(n == 0 && p == 0) break;
		int utuwa = p, i = 0, kouho[51] = {0};
		while(1){
			if(utuwa != 0){
				utuwa--;
				kouho[i]++;
			} else{
				utuwa += kouho[i];
				kouho[i] = 0;
			}
			if(utuwa == 0 && kouho[i] == p) break;
			if(i >= (n - 1)) i = 0;
			else i++;
		}
		cout <<i <<endl;
	}
	return 0;
}

C++: //Next Mayor
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
  int n, p;
  while(true){
    cin>>n>>p;
    if(n==0 && p==0)break;
    ll turn=0;
    int a[n], pn=p;
    memset(a, 0, sizeof(a));
    while(true){
      if(pn!=0){
        a[turn%n]++;
        pn--;
      }
      else{
        pn=a[turn%n];
        a[turn%n]=0;
      }
      if(a[turn%n]==p)break;
      turn++;
    }
    cout<<turn%n<<endl;
  }
  return 0;
}

C++: #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 && cntt==1)break;
}
printf("%d\n",pos);
}
}

C++: #include<iostream>
#include<stdio.h>
#include<cmath>
#include<vector>
using namespace std;
#define loop(i,a,b) for(int i=a,i<b;i++)
#define rep(i,a) loop(i,0,a)
int main(){
  int n,p;
  while(cin>>n>>p,n||p){
    int wan=p;
    vector<int>in(n);
    int to=0;
    while(1){
      if(wan){
	wan--;
	in[to]++;
      }else{
	wan+=in[to];
	in[to]=0;
      }
      if(in[to]==p)break;
      to++;
      if(to==n)to=0;
    }
    cout<<to<<endl;
  }
}

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

int n, p, mayor[50], wan, basyo;

int main()
{
  while(true){
    
    cin >> n >> p;
    if(n == 0 && p == 0) break;
    
    for(int i = 0 ; i < n ; i++){
      mayor[i] = 0;
    }
    wan = p;
    basyo = 0;

    while(true){
      if(wan > 0){
	mayor[basyo]++;
	wan--;
	if(mayor[basyo] == p){
	  cout << basyo << '\n';
	  break;
	}
      }else{
	wan = mayor[basyo];
	mayor[basyo] = 0;
      }
	basyo++;
	if(basyo == n) basyo = 0;
    }
  }
}