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 <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
	int n,p;
	int stone[50];
	while(scanf("%d %d",&n,&p),n) {
		for(int i=0;i<n;i++) stone[i]=0;
		int k=0,x=p;
		while(stone[k]<p) {
			if(x>0) {
				x--;
				stone[k]++;
			}else {
				x+=stone[k];
				stone[k]=0;
			}
			k=(k+1)%n;
		}
		printf("%d\n",k);
	}
}

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

int main(){
  int n, p;
  int player[50];

  while(1){
    cin >> n >> p;
    if(n == 0 || p == 0) break;
    for(int i = 0; i < n; i++) player[i] = 0;

    //ゲーム開始
    int i = 0;
    int win = p;
    while(1){
      if(p > 0){
	p--;
	player[i]++;
	//勝利条件
	if(p == 0 && player[i] == win){
	  cout << i << endl;
	  break;
	}
      }else{
	p += player[i];
	player[i] = 0;
      }
      i++;
      if(i == n) i = 0;
    }
  }

  return 0;
}

C++: 
// Next Mayor
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1159&lang=jp

#include<iostream>
#include<vector>

using namespace std;

const int NONE = -1;

int N, P;
vector<int> player;
int bowl;
int winner;

inline bool is_win(int i) {
    for (int nth = 0; nth < N; ++nth) {
        if (nth == i) continue;
        if (player[nth] != 0) return false;
    }
    return true;
}

inline void pick_up(int i) {
    if (bowl == 0) {
        bowl += player[i];
        player[i] = 0;
        return;
    }
    else if (bowl == 1) {
        bowl--;
        player[i]++;
        if (is_win(i)) winner = i;
        return;
    }
    bowl--;
    player[i]++;
    return;
}

int main() {
    while(cin >> N >> P && N) {
        player.assign(N, 0); 
        bowl = P;
        winner = NONE;
        int i = 0;
        while (true) {
            if (winner != NONE) break;
            pick_up(i);
            i = (i + 1) % N;
        }
        cout << winner << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long int lld;
int main(){
  int n,p;
  while(1){
    cin >> n >> p;
    if(n == 0 && p == 0) break;
    int stone[55]={0};
    int now = 0,np = p;
    while(1){
      if(np == 0){
	int *idx = max_element(stone,stone+n);
	if((*idx) == p){
	  cout << idx - stone << endl;
	  break;
	}
	np = stone[now];
	stone[now] = 0;
      } else {
	np--;
	stone[now]++;
      }
      now = (now + 1)%n;
    }
  }
}

C++: #include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#define FOR(i, a, b)  for(int i=a; i<b; ++i)
#define rFOR(i, a, b) for(int i=a-1; i>=b; --i)
#define REP(i, n) for(int i=0; i<n; ++i)
#define rREP(i, n) for(int i=n-1; i>=0; --i)
#define all(a)  a.begin(), a.end()
using namespace std;

struct POINT{
  int x, y;

  bool operator<(const POINT& right) const {
    return x < right.x;
  }
};

int main(){
  int n, p;
  while(true){
    cin >> n >> p;
    int ans = p;
    bool flag = false;
    if(n == 0 && p == 0) break;
    int have[n];
    REP(i, n) have[i] = 0;

    while(true){
      REP(i, n){
        if(p > 0){
          have[i]++;
          p--;
          if((p == 0) && (have[i] == ans)){
            cout << i << endl;
            flag = true;
          }
        }else if(p == 0){
          p += have[i];
          have[i] = 0;
        }
      }
      if(flag == true)  break;
    }
  }

  return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#define INF 1000000000
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;

int main(){
    for(;;){
        int n,p;
        cin >> n >> p;
        if(n==0) break;
        int ch = 0;
        int i = p,j=0;
        vector<int> g(n,0);
        int ans;
        while (true) {
            if(i>0){
                i--;
                if(g[j]==0)ch++;
                g[j]++;
            }else{
                i+=g[j];
                if(g[j]!=0)ch--;
                g[j]=0;
            }
            if(ch == 1 and i == 0){
                ans = j;
                break;
            }
            j++;
            if(j>=n) j-=n;
        }
        cout << ans << endl;
    }
}

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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

int popCount(ll n){
    n = (n & 0x5555555555555555) + (n >>  1 & 0x5555555555555555);
    n = (n & 0x3333333333333333) + (n >>  2 & 0x3333333333333333);
    n = (n & 0x0f0f0f0f0f0f0f0f) + (n >>  4 & 0x0f0f0f0f0f0f0f0f);
    n = (n & 0x00ff00ff00ff00ff) + (n >>  8 & 0x00ff00ff00ff00ff);
    n = (n & 0x0000ffff0000ffff) + (n >> 16 & 0x0000ffff0000ffff);
    n = (n & 0x00000000ffffffff) + (n >> 32 & 0x00000000ffffffff);
    return n;
}

int main(){
    int P, N;
    while(std::cin >> N >> P, P || N){
        ll isEmpty = 0;
        int stones[50], pos = 0;
        std::fill(stones, stones+50, 0);

        while(!(popCount(isEmpty) == N - 1 && P == 0)){
            if(P > 0){
                P -= 1;
                stones[pos] += 1;
                isEmpty &= ~(1LL << pos);
            }else{
                P += stones[pos];
                stones[pos] = 0;
                isEmpty |= 1LL << pos;
            }
            pos = (pos + 1) % N;
        }
        // REP(i, N){
        //     printf("%d ", stones[i]);
        // }
        // puts("");

        REP(i, N){
            if(isEmpty >> i & 1){continue;}
            printf("%d\n", i);
            break;
        }
    }
}

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

intmain(){

int N,M;
int n[51]={};
int i=0,p=0;
while(1){
scanf("%d %d",&N,&M);
if(N==0&&M==0)break;
p=M;i=0;
while(n[i]!=M)
{
if(p==0&&n[i]>0){p=n[i];n[i]=0;}
//else if(p==0&&n[i]==0)
else if(p>0){p--;n[i]++;}
i++;
if(i==N-1)i=0;
}
cout<<i<<endl;
}
return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int n,p,c,i;
  int a[51];
  while(1){
    //skk
    for(int j=0;j<51;j++){
      a[j]=0;
    }
    i=0;

    cin >> n >> p;
    if(n==0&&p==0)break;
    c=p;
    while(1){
      if(c!=0){
	a[i]++;
	c--;
	if(a[i]==p){
	  cout << i << endl;
	  break;
	}
      }
      else{
	c+=a[i];
	a[i]=0;
      }
      i++;
      if(i==n)i=0;
    }
  }
  return 0;
}

C++: //include
//------------------------------------------
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>

using namespace std;

int main() {
	int n, p, cup;
	while (cin >> n >> p, n | p)
	{
		cup = p;
		vector<int> v(n, 0);
		int i = 0;
		while (true)
		{
			if (cup) {
				v[i]++;
				cup--;
			}else{
				cup = v[i];
				v[i] = 0;
			}
			if (v[i] == p)break;
			if (i++ == n - 1) i = 0;
		}
		cout << i << endl;
	}
	return 0;
}