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;

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;
}

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

int st[60];

int solve(int n,int p){
  memset(st,0,sizeof(st));
  int t = 0;
  int all = p;
  while(true){
    if(p){
      st[t]++;
      p--;
    }
    else{
      p+=st[t];
      st[t]=0;
      if(p==all) break;
    }
    t=(t+1)%n;
  }
  return t;
}
int main(){
  int n,p;
  while(cin>>n>>p,n||p){
    cout << solve(n,p) << endl;
  }
  return 0;
}

C++: #include <stdio.h> 

int main() {
    for(int i=0;i<1000;i++){
	int n = 0;
	int p = 0;
	int x[100] = {};
	int y = 0;
	int z = 0;
	int a = 0;

	scanf("%d %d", &n, &p);

	y = p;

	while (1) {
		while (y>0) {
			if (z<n) {
				x[z]++;
				y--;
				z++;
			}
			else if (z == n) {
				z = 0;
				x[z]++;
				y--;
				z++;
			}
		}


		while (y == 0) {
			if (z == n) {
				z = 0;
			}

			a = x[z];
			if (a == p) {
				break;
			}
			y = a;
			x[z] = 0;
			a = 0;
			z++;
		}
		
		if (n == 0 || p == 0) {
			break;
		}

		if (a == p) {
			printf("%d\n", z);
			break;
		}
	}
    }
	return 0;
}

C++: #include <iostream>

using namespace std;

const int nMax = 51;

int continueJudge(int stones[], int nAll) {
  int flag = 99;

  for(int i=0; i<nAll; i++) {
    if(flag==99 && stones[i]>0) {
      flag = i;
    }
    else if(flag!=99 && stones[i]>0) {
      return 99;
    }
  }

  return flag; // 継続ならば99を返す
}

void printArray(int array[], int n) {
  for(int i=0; i<n; i++) {
    cout<<"array["<<i<<"] = "<<array[i]<<endl;
  }
  cout<<endl;
}

int main() {

  int n, p;
  int stones[nMax];

  while(cin>>n>>p && n!=0 && p!=0) {
    for(int i=0; i<n; i++) {
      stones[i] = 0;
    }

    int flag = 99;
    int i = 0;
    while(flag == 99) {
      if(p > 0) {
	stones[i]++;
	p--;
	if(p == 0) {
	  flag = continueJudge(stones, n);
	}
      }
      else { // p == 0
	p += stones[i];
	stones[i] = 0;
      }
      i++;
      if(i == n) {
	i = 0;
      }
      //printArray(stones, n);
    }
    cout<<flag<<endl;
  }
  
}

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<bits/stdc++.h>
using namespace std;

vector<int> v;
int n, p, last, ans;

int main(){

while(1){

    ans = 0;
    cin >> n >> p;
    if(n == 0 and p == 0) break;
    last = p;
    for(int i=0; i < n; i++) v.push_back(0);

    while(1){
        for(int i=0; i < n; i++){
            //pが残ってるとき
            if(p != 0){
                //最後の一個か
                if(p - 1 == 0){
                    v[i] += 1;
                    p -= 1;
                    //終了確認
                    if(v[i] == last){
                        ans = i;
                        goto end;
                    }
                }
                //そうでないとき
                else{
                    v[i] += 1;
                    p -= 1;
                }
            }
            //椀の中に何もない
            else {
                p += v[i];
                v[i] = 0;
            }
        }
    }

    end:
    cout << ans << endl;
    v.clear();
}//LOOP
    return 0;
}

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

int n, p;
vector<int> stones_candidates;
int stones_cup;
int current_player;

int game_set()
{
    if (stones_cup > 0) return false;
    bool cond = true;
    for (int i = 0; i < n; i++) {
        if (i != current_player && stones_candidates[i] != 0) {
            cond = false;
        }
    }
    return cond;
}

inline int next_player()
{
    return current_player == n - 1
        ? 0
        : current_player + 1;
}

int main()
{
    while (cin >> n >> p, n && p) {
        stones_candidates = vector<int>(n);
        stones_cup = p;

        current_player = 0;
        while (true) {
            if (stones_cup > 0) {
                stones_cup--;
                stones_candidates[current_player]++;
            } else if (stones_cup == 0) {
                stones_cup += stones_candidates[current_player];
                stones_candidates[current_player] = 0;
            } else {
                throw "Something wrong!";
            }
            
            if (game_set()) {
                break;
            }

            current_player = next_player();
        }

        cout << current_player << endl;
    }
}

C++: #define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e9
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
long long gcd(long long a, long long b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int main() {
	while (true) {
		int n, p;
		cin >> n >> p;
		if (n == 0) return 0;
		int geko[51] = {};
		int now = p;
		for (int i = 0;; i++) {
			if (now == 0) {
				now = geko[i%n];
				geko[i%n] = 0;
			}
			else {
				geko[i%n] += 1;
				now--;
				if (geko[i%n] == p) {
					cout << i%n << endl;
					break;
				}
			}
		}
	}
}