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 <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cstdio>
#define F(i,n) for(int i=0;i<(int)(n);i++)
#define INF 100000000
#define ll long long

using namespace std;
typedef pair<int, int> P;

int main() {
	int n, p;
	int a;
	int b[51];
	int all;
	int ans;
	while (1) {
		cin >> n >> p;
		if (n == 0 && p == 0)break;
		a = 0;
		all = p;
		F(i, n)b[i] = 0;
		while (1) {
			if (p == 0) {
				p = b[a];
				b[a] = 0;
			}
			else {
				b[a]++;
				p--;
				if (p == 0 && b[a] == all) {
					ans = a;
					break;
				}
			}
			a = (a + 1) % n;
		}
		cout << ans << endl;
	}
	return 0;
}

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

typedef int64_t ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define pb            push_back
template<class T, class S> inline pair<T, S> mp(T x, S y){ return make_pair(x, y); }
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

static const int    INF = 1000000000;
static const ll     MOD = 1000000007LL;
static const double EPS = 1E-12;

int n, p;

int main()
{
  while(cin >> n >> p, n){
    ll mask = 0LL;
    int cnt = p;
    vi d(n, 0);
    bool ok = true;
    while(ok){
      rep(i, n){
        if(cnt){
          cnt -= 1;
          if((mask | 1LL << i) == (1LL << n) - 1LL){
            cout << i << endl;
            ok = false;
            break;
          }
          d[i] += 1;
          mask &= ~(1LL << i);
        }
        else{
          swap(cnt, d[i]);
          mask |= 1LL << i;
        }
      }
    }
  }

  return 0;
}

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

int a[50],n,p;
int main(){
	while(scanf("%d%d",&p,&n),n*p){
		memset(a,0,sizeof(int)*50);
		int t=n,z=0;
		for(;!(a[z]==n-1&&t==1);z=(z+1)%p){
			if(t){
				t--;
				a[z]++;
			}else{
				t=a[z];
				a[z]=0;
			}
		}
		printf("%d\n",z);
	}
	return 0;
}

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

int main(){
  
  while(1){
    int n,p;
    int May_wan[50];
    int i,ans;

  cin >> n >> p;
  int p_max = p;
  if(n == 0&&p == 0)break;
  for(int i = 0; i < 50; i++)May_wan[i] = 0;
   i =0,ans = 0;
  while(1){
    if(p_max == May_wan[i]){
      ans = i;
      break;
    }    
    if(p > 0){
      May_wan[i]++;
      p--;
    }

    else if(p == 0){
      p+=May_wan[i];
      May_wan[i] = 0;
    }
    

    if(i == n-1)i = 0;
    else i++;
  }
  cout << i << endl;
  }
  return 0;
}

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

int main(){
	int n,p;
	bool flag;
	while(cin >> n >> p && n || p){
		int owan = p;
		vector<int> temoto(n,0);
		int i=0;
		while(1){
			if(owan == 1){
				owan--;
				temoto[i%n]++;
				flag = true;
				for(int j=0;j<n;j++){
					if(j == i%n)continue;
					if(temoto[j])flag = false;
				}
				if(flag)break;
			}else if(owan){
				owan--;
				temoto[i%n]++;
			}else{
				owan = temoto[i%n];
				temoto[i%n]=0;
			}
			i++;
		}
		cout << i%n << endl;
		
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

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

C++: #include <iostream>

using namespace std;

int main(){
	int n, p;
	while(cin >> n >> p, n){
		int num = p, pos = 0;
		int s[50] = {};
		while(true){
			pos %= n;
			if(num==0) swap(num, s[pos]);
			else       num--, s[pos]++;
			if(s[pos]==p){
				cout << pos << endl;
				break;
			}
			pos++;
		}
	}
}

C++: #include <iostream> 
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <deque>  
#include <iomanip>

using namespace std;

using ll = long long;
#define INF 1e9
#define MOD 1e9 + 7
#define rep(i, n) for(int i = 0; i < n; i++)
#define loop(i, a, n) for(int i = a; i < n; i++)
#define all(in) in.begin(), in.end()
int main(){
    int n, r;
    while(true){
        cin >> n >> r;
        if(n == 0 && r == 0) break;
        int ans  = 0;
        int box = r;
        map<int, int> mp;
        rep(i, n) mp[i]  = 0;
       bool flag = false; 
        while(true){
           rep(i, n){
               if(box > 0){
                   mp[i]++;
                   box--;
               }else{
                   box = mp[i];
                   mp[i] = 0;
               }
               if(mp[i] == r){
                   ans = i;
                   flag = true;
                   break;
               }
           } 
           if(flag) break;
        }
        cout << ans << endl;
    }
    return 0;
}

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

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

C++: #include<iostream>

using namespace std;

int main(){
  int P,N,ndata[50],wan,lop;
  while(1){
    cin>>N;
    cin>>P;
    if(N==0&&P==0){
      break;
    }
    for(lop=0;lop<N;lop++){
      ndata[lop]=0;
    }
    wan=P;
    while(1){
      for(lop=0;lop<N;lop++){
        if(wan!=0){
          wan--;
          ndata[lop]++;
        }else{
          wan=wan+ndata[lop];
          ndata[lop]=0;
        }
        if(ndata[lop]==P&&wan==0){
          break;
        }
      }
      if(ndata[lop]==P&&wan==0){
        cout<<lop<<endl;
        break;
      }
    }

  }
  return 0;
}