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 <algorithm>
#include <iostream>
// 18:45 -

int main(void)
{
    while(1){
        int n, p;
        scanf("%d %d", &n, &p);
        if(n == 0 & p == 0)
            break;
        int mine[n] = {0};
        int all = 0;
        int count = 0;
        for(count = 0;; count++){
            all = 0;
            if(p > 0){
                p--;
                mine[count % n]++;
                for(int i = 0; i < n; i++) all += mine[i];
                all -= mine[count % n];
                if(all == 0 && p == 0){
                    break;
                }
            } else if(p == 0){
                p += mine[count % n];
                mine[count % n] = 0;
            }
        }
        printf("%d\n", count % n);
    }

    return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<climits>
#include<ctime>
#include<cstring>
#define REP(i,p,n) for(int i=p;i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define rep_split(tok,a_str,re) for(char *tok = strtok((char *)a_str.c_str(),re); tok != NULL; tok = strtok(NULL,re))
#define all(c) (c).begin(), (c).end()

using namespace std;
template<class T> inline void chmax(T& a, T b) { if(b > a) a = b; }
template<class T> inline void chmin(T& a, T b) { if(b < a) a = b; }

int stones[51];

int main() {
	int N,P;
	while(true) {
		cin >> N >> P;

		int wan = P;
		if (N == 0 && P == 0) { break; }
		rep(i,N) {
			stones[i] = 0;
		}

		int i = 0;
		while(true) {
			if (i >= N) { i = 0; }
			if (wan > 0) {
				wan--;
				stones[i]++;
			}
			else { //空なら
				wan = stones[i];
				stones[i] = 0;
			}
			//cout << i << ": " << stones[i] << " | ";
			if (stones[i] >= P) { 
				//cout << endl;
				cout << i << endl;
				break; 
			} 
			i++;
		}

	}

	return 0;
}

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

int main() {

  long long int n, p;

  while( true ) {

    cin >> n >> p;

    if ( n == 0 ) break;

    long long int num[50] = {0};

    long long int cnt = 0;

    long long int m = p;

    while( true ) {

      if ( m > 0 ) {
	m--;
	num[cnt]++;
	if ( num[cnt] == p ) break;
      }else {
	m = num[cnt];
	num[cnt] = 0;
      }
      cnt = ( cnt + 1 ) % n;

    }

    cout << cnt << endl;

  }

  return 0;

}

C++: #include "bits/stdc++.h"
using namespace std;
#define rep(i,a,n) for(ll i=a;i<n;i++)
#define ALL(s) s.begin(),s.end()
#define P pair<ll,ll>
#define vl vector<ll>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvl vector<vector<ll>>
#define print(n) cout<<n<<endl
const int M=100010;
const int MOD=1000000007;
const int inf=1000000007;
const long long INF=1000000000000000007;
using ll=long long;


int main(){
	while(1){
		int n,p,cnt=0,ans=0;
		cin>>n>>p;
		int res[50]={};
		if(n==0)break;
		cnt=p;
		bool ok=false;
		int x=-1;
		while(1){
			x++;
			x=x%n;
			if(cnt!=0){
				cnt--;
				res[x]++;
			}else{
				cnt=res[x];
				res[x]=0;
				if(cnt==p){
					ans=x;
					break;
				}
			}
		}
		print(ans);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main() {
    while (true) {
        int n, p; cin >> n >> p;
        if (n == 0 and p == 0) break;
        vector<int> a(n);
        int b = p;
        int i = 0;
        while (true) {
            if (b) {
                -- b;
                ++ a[i];
                if (a[i] == p) break;
            } else {
                b = a[i];
                a[i] = 0;
            }
            i = (i + 1) % n;
        }
        cout << i << endl;
    }
    return 0;
}

C++: # include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <stack>
# include <bitset>
# include <algorithm>
# include <functional>
# include <numeric>
# include <utility>
# include <sstream>
# include <iostream>
# include <iomanip>
# include <cstdio>
# include <cmath>
# include <cstdlib>
# include <cctype>
# include <string>
# include <cstring>
# include <ctime>
# include <queue>


# define FOR(i, a, b) for(int i{a}; i < (b); ++i)
# define REP(i, n)    FOR(i, 0, n)

using namespace std;

using ll = long long;

template<typename T>
using Vec = vector<T>;



int main()
{


    while (true)
    {
        int n, p;
        cin >> n >> p;

        if (n == 0 && p == 0) {
            break;
        }

        int owan = p;


        Vec<int> counts(n, 0);
        int index = 0;

        while (true) {
            if (owan > 0) {
                counts[index]++;
                owan--;
                if (counts[index] == p) {
                    break;
                }
                index++;
                index %= n;
            }
            else {
                if (counts[index] > 0) {
                    owan += counts[index];
                    counts[index] = 0;
                }
                index++;
                index %= n;
            }
        }

        cout << index << endl;

    }
}

C++: #include <stdio.h>
int main(void)
{
  int n,p,i,j;
  int sum=0;
  int m=0;
  for(;;)
    {
      scanf("%d %d",&n,&p);
      int entry[n];  
      for(i=0;i<n;i++) {entry[i]=0; /*printf("entry[%d]=%d\n",i,entry[i]);*/}
      if(n==0)  {if(p==0){break;}} 
      i=0;
      int pk;
      pk=p;
      for(;;)
	{
	  for(i=0;;i++)
	    {
	      if(p>1)
		{
		  p=p-1;
		  entry[i]=entry[i]+1;
		}
	      else if(p==1)
		{
		  /*printf("---------------------------------");*/
		  if(entry[i]==pk-1){m=1;/*printf("............."); */break;}
		  else{p=0; entry[i]=entry[i]+1;}
		}	
	      else if(p==0)
		{
		  p=entry[i];
		  entry[i]=0;	    
		}
	      /*printf("entry[%d]=%d p=%d\n",i,entry[i],p);*/
	      if(i==(n-1)){i=(-1);}
	    }
	  if(m==1){printf("%d\n",i); break;}
	}
    }	  
  return 0;
}

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<50;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 <vector>
#include <iostream>
using namespace std;
int n, m;
int main() {
	while (cin >> n >> m, n) {
		int s = m;
		vector<int> v(n, 0);
		for (int i = 0; ; i = (i + 1 == n ? 0 : i + 1)) {
			if (m != 0) v[i]++, m--;
			else m = v[i], v[i] = 0;
			if (m == 0 && s - m - v[i] == 0) {
				cout << i << endl;
				break;
			}
		}
	}
	return 0;
}