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(int argc, char *argv[]) {

  int n, p;
  int count;
  int c;
  vector<int> num;
  int i;
  bool flag, f;

  while(true) {
    cin >> n >> p;
    if(n == 0) break;
  count = p;
  num.resize(n);
  for(int j = 0; j < n; j++)
    num[j] = 0;
  flag = false;
  f = false;
  i = 0;
  while(true){
    c = 0;
    if(i != 0)
      f = true;
    //cout << count << endl;
    if(i >= n)
      i = 0;
    if(count > 0) {
      num[i]++;
      count--;
    }
    else if(count == 0){
      count = num[i];
      num[i] = 0;
    }
    for(int j = 0; j < n; j++)
      if(i != j)
	if((num[j] == 0) && f)
	  c++;
    //cout << "c::" << c << endl;
    if(c == n-1)
      flag = true;
    if((count == 0) && flag)
      break;
    i++;
  }
  
  cout << i << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<iomanip>
#include<sstream>
#include<vector>
#include<stack>
#include<math.h>
#include<queue>
#include<complex>
#include<random>
#include<ctime>
#include<set>
using namespace std;


const long long int mod=1000000007;
const long long int INF=99999999999999999;



int main() {
	cout << fixed << setprecision(18);
	long long int n,p,c,now=0,sum=0,kosuu[100]={};
	cin>>n>>p;
	while(n!=0){
		now=0;
		sum=p;
		for(int i=0;i<80;i++){
			kosuu[i]=0;
		}

		while(1){
			if(sum!=0){
				kosuu[now]++;
				sum--;
			}else{
				sum+=kosuu[now];
				kosuu[now]=0;
				
			}
			if(kosuu[now]==p){
				cout<<now<<endl;
				break;
			}
			now=(now+1)%n;
		}
		
		cin>>n>>p;
	}
}

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

using namespace std;

int main(){
  int n,p;
  
  while(1)
    {
      cin >> n >> p;
      if(n + p == 0) break;
      
      int man[n];
      for(int i=0;i < n;i++)
	{
	  man[i] = 0;
	}
      int stone = p;
      int i=0;

      while(1){
	
	if(stone == 0 && man[i] == 0)
	  {
	    i++;
	  }
	else if(stone == 0 && man[i] != 0)
	  {
	    stone = man[i];
	    man[i] = 0;
	    i++;
	  }
	else if(stone != 0)
	  {
	    man[i]++;
	    stone--;
	    i++;
	  }
	
	if(i == n) i = 0;

	if(man[i] == p)
	  {
	    cout << i << endl;
	    break;
	  }
      }
    }
  return 0;
}

C++: #include<iostream>
#include<cstdio>

using namespace std;
int N[50];
int main(){
  int n,p,p1;
 
  int i=0;
  while(true){
    cin>>n>>p;
    //ishi
    p1=p;
    if(n==0&&p==0)break;
    i=0;
    while(true){
      if(i==n)i=0;
      if(p1==0){
	p1=N[i];
	N[i]=0;
      }
      else {
	p1--;
	N[i]++;
	if(N[i]==p)break;
      }
     i++;
    }
    cout<<i<<endl; 
    N[i]=0;
    /*
    for(int i=0;i<n;i++){
      cout<<"person"<<i<<" "<<N[i]<<endl;
      }*/
  }
  return 0;
  
}

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

int main()
{
	int n,p;
	while(cin >> n >> p, n||p)
	{
		int s=p;
		int snum[64] = {0};

		for(int i=0; true; i=(i+1)%n)
		{
			if(s)
			{
				snum[i]++;
				if(snum[i]==p)
				{
					cout << i << endl;
					break;
				}
			}
			else
			{
				s=snum[i];
				snum[i]=0;
			}
		}
		
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a); i<(b); i++)
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define sort(v,n) sort(v,v+n);
#define vsort(v) sort(v.begin(),v.end());
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
typedef pair<int,int> p;
typedef pair<ll,ll> lp;
bool is_uruu(int y) {
        return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
}
const ll MOD=1e9+7;
const double PI=acos(-1.0);
//---------------------------------------------------------------------------//
int n,b;
int main(){
        while(1) {
                cin>>n>>b;
                if(n==0) return 0;
                int index=0;
                int s[n];
                int stone=b;
                while(1) {
                        if(stone!=0) {
                                stone--;
                                s[index]+=1;
                        }else if(stone==0) {
                                stone+=s[index];
                                s[index]=0;
                        }
                        if(s[index]==b) {
                                break;
                        }
                        if(index==n-1) {
                                index=0;
                        }else{
                                index++;
                        }
                }
                cout<<index<<endl;
        }
}

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

bool check (vector<int> n) {
    int count = 0;
    for (int i = 0; i < n.size(); i++) {
        if (n[i] != 0) {
            count++;
        }
    }
    return count == 1 ? false : true;
}

int main ()
{
    int a, b;
    while (cin >> a >> b, (a || b)) {
        vector<int> n(a);
        int s  = 0;
        while (b || check(n)) {
            if (b > 0) {
                b--, n[s]++;
            } else {
                b += n[s], n[s] = 0;
            }
            if (!check(n) && !b) break;
            s = (s+1) % a;
        }
        cout << s  << endl;
    }

    return 0;
}

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

int main(){
    ios_base::sync_with_stdio(false);
	vector<int> ansvec;

	for(;;){
		int n, p;
		cin >> n >> p;
		if (n == 0) break;
		
		int bowlp = p;
		int turn = 0;
		int mayors[50]{0};
		for(;;){
			if (bowlp == 1 && mayors[turn] == p - 1) break;
			else if (bowlp == 0){
				bowlp += mayors[turn];
				mayors[turn] = 0;
			}
			else{
				mayors[turn]++;
				bowlp--;
			}

			if (turn == n - 1) turn = 0;
			else turn++;
		}
		ansvec.push_back(turn);
	}

	for (int ans : ansvec){
		cout << ans << endl;
	}
	return 0;
}

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

#define REP(i,n) for(long long i=0;i<n;++i)
#define REPR(i,n) for(long long i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair
#define SV(v) {for(long long sitr=0;sitr<v.size();++sitr){cin>>v[sitr];}}

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

typedef pair<int,int> pii;

int main(){

  while(true){
    int n, p;
    cin >> n >> p;
    int num = p;
  
    if(n == 0 && p == 0) break;

    vector<int> v(n);
    REP(i,n){
      v[i] = 0;
    }

    bool is_end = false;
    while(!is_end){
      REP(i,n){
        if(p > 0){
          v[i]++;
          p--;
          if(p == 0){ 
            if(v[i] == num){
              cout << i << endl;
              is_end = true;
              break;
            }
          }
        }else{
          p += v[i];
          v[i] = 0;
        }
      }  
    }

  }
  return 0;
}