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

int main() {
    while(true) {
        cin>>n>>p;
        if(!n) return 0;
        int now=0;
        int cnt[51]={};
        while(true) {
            if(p) {
                cnt[now]++;
                p--;
                if(!p) {
                    bool end=true;
                    for(int i=0; i<n; ++i) {
                        if(i!=now && cnt[i]) end=false;
                    }
                    if(end) {
                        cout<<now<<endl;
                        break;
                    }
                }
                ++now%=n;
            } else {
                p=cnt[now];
                cnt[now++]=0;
                if(now==n) now-=n;
            }
        }
    }
}

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

int main(){
  int stones[51];
  int n,p,i,j,stone;
  cin >> n >> p;
  while(n!=0||p!=0){
    for(i=0;i<51;i++) stones[i]=0;
    i=0;
    stone=p-1;
    stones[0]=1;
    while(stones[i]!=p){
      i=(i+1)%n;
      if(stone > 0) {
	stones[i]++;
	stone--;
      }else if(stones[i]!=0){
	stone+=stones[i];
	stones[i]=0;
      }
    }
    cout << i << endl;
    cin >> n >> p;
  }
}

C++: #include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define Repr(i, n, a) for (int i = (n); i >= (a); i--)
#define rep(i, n) Rep(i, 0, n)
#define repr(i, n) Repr(i, n, 0)
#define all(a) a.begin(), a.end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, p;
    while (1)
    {
        cin >> n >> p;
        if (n == 0)
            break;
        vector<int> cnt(n);
        int pos = 0, x = p;
        while (1)
        {
            if (p > 0)
            {
                cnt[pos++]++;
                if (cnt[pos - 1] == x)
                {
                    cout << pos - 1 << endl;
                    break;
                }
                p--;
                pos %= n;
            }
            else
            {
                p += cnt[pos++];
                cnt[pos - 1] = 0;
                pos %= n;
            }
        }
    }
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<functional>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<utility>
#include<sstream>
#include<complex>
#include<fstream>
#include<bitset>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> P;

#define rep(i,N) for(int i=0;i<(N);i++)

int n, p;

int main() {
	while (cin >> n >> p&&n + p) {
		int cnt[100] = {};
		int now = 0;
		int k = p;
		while (1) {
			if (k > 0) {
				cnt[now]++;
				k--;
				if (k == 0 && cnt[now] == p) {
					cout << now << endl;
					break;
				}
			}
			else {
				k += cnt[now];
				cnt[now] = 0;
			}
			now = (now + 1) % n;
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long L;
L brute(L n,L p){
    L pep[n],bowl=p,id=0;
    memset(pep,0,sizeof(pep));
    while(1){
        if(bowl){
            pep[id]++;
            bowl--;
        }
        else{
            bowl+=pep[id];
            pep[id]=0;
        }
        if(pep[id]==p)return id;
        id++;
        if(id==n)id=0;
    }
}
int main()
{
    L n,p;
    while(scanf("%lld%lld",&n,&p)&& n||p ){
        n=brute(n,p);
        printf("%lld\n",n);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=1<<17,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        int N,rem;cin>>N>>rem;
        int fi=rem;
        if(N==0&&rem==0) return 0;
        
        vector<int> state(N,0);
        int now=-1;
        
        while(1){
            now++;
            if(now==N) now=0;
            
            if(rem==0){
                rem+=state[now];
                state[now]=0;
            }else{
                state[now]++;
                rem--;
            }
            
            if(state[now]==fi){
                cout<<now<<endl;
                break;
            }
        }
    }
}

C++: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	int n,p;
	while(1){
		cin>>n>>p;
		if(n==0 && p==0)break;
		int dt[n];
		int cop=p;
		int counta=0;
		rep(i,n)dt[i]=0;
		int ans;
		while(1){
			counta=counta%n;
			if(cop!=0){
				dt[counta]++;
				cop--;
			}else{
				int judge=0;
				rep(i,n){
					if(i!=counta)if(dt[i]==0)judge++;
				}
				if(judge==n-1){
					ans=counta;
					break;
				}else{
					cop=dt[counta];
					dt[counta]=0;
				}
			}
			counta++;
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
  int n,p,data[50],i,d,result;
  while(1){
    cin >> n >> p;
    d=p;
    if(n==0&&p==0) break;
    for(i=0;i<n;i++) data[i]=0;
    i=0;
    while(1){
      if(p>0){
	p--;
        data[i]++;
	if(data[i]==d){
	  result=i;
	  break;
	}
      }else if(p==0){
        p=data[i];
        data[i]=0;
      } 
      if(i+1==n) i=0;
      else i++;
    }
    cout << result << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
    int N, P;
    while (cin >> N >> P, N || P) {
        vector<int> v(N);
        int ans = -1;
        int now = 0;
        while (1) {
            if (P == 0) {
                P += v[now];
                v[now] = 0;
            } else {
                P--;
                v[now]++;
            }

            if (P == 0) {
                int count = 0;
                for (auto i : v) if (i > 0) count++;
                if (count == 1) {
                    cout << now << endl;
                    break;
                }
            }

            now = (now + 1) % N;
        }
    }

    return 0;
}

C++: #include<iostream>
#include<cmath>
#include<vector>
#include<string>
typedef unsigned long long ull;
#define rep(i,a) for(int i=0;i<a;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

void solve(){
  int N,P;
  while(cin>>N>>P,N+P){
    int to=0;
    int w=P;
    vector<int> in(N);
    while(true){
      if(w){
	w--;
	in[to]++;
      }else{
	w+=in[to];
	in[to]=0;
      }      
      if(in[to]==P)break;
      to++;
      if(to==N)to=0;
    }						
    cout<<to<<endl;
  }
}

int main(void){
  solve();
  return 0;
}

C++: #include<bits/stdc++.h>
#include<vector>
using namespace std;
int main(void){

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

    vector<int> koishi(n , 0);
    int ans = 0, remain = p;
    while(true){

      if(remain == 0){
        remain += koishi[ans];
        koishi[ans] = 0;
      }else{
        koishi[ans]++;
        remain--;
        if(koishi[ans] == p)   break;
      }

      ans = (n == ++ans) ? 0 : ans;
    }

    cout << ans << endl;

   }

  return 0;
}