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>

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

using namespace std;

int n, p;
int x[50];

int main(){
	while(scanf("%d%d", &n, &p), n){
		int c = 0;
		int q = p;
		while(1){
			if(q > 0){
				++x[c];
				--q;
				if(x[c] == p){
					printf("%d\n", c);
					break;
				}
			}
			else{
				q += x[c];
				x[c] = 0;
			}
			c = (c + 1) % n;
		}

		rep(i, n){
			x[i] = 0;
		}
	}
	return 0;
}

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

int main(){

  for(int n, p;cin >> n >> p, n|p;){
    int a[55] = {}, i = 0, v = p;
    while(a[i] != p){
      if(!v){ v = a[i], a[i] = 0; }
      else{ --v; ++a[i]; }
      i = ++i%n;
    }
    cout << i << endl;
  }

}

C++: // Author :
// Date :
// Problem:
// Solution:
// Comment:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cstring>

#define pp push
#define pb push_back
#define mp make_pair
#define xx first
#define yy second

using namespace std;

int play(int n,int m){
    int me[55]={0};
    int cur=0;
    int left = m;
    
    while(1){
        if(cur == n) cur = 0;
        
        if(left == 1 && me[cur] == (m-1)) return cur;
        
        if(left == 0){
            left = me[cur];
            me[cur] = 0;
        }
        else{
            left--;
            me[cur]++;
        }
        
        cur++;
    }
}
main(){
    int n,m;
    
    while(cin >> n >> m){
        if(n == 0) break;
        cout << play(n,m) << endl;
    }
    #ifdef GEREL
        for(;;);
    #endif

    return 0;
}

C++: #include <stdio.h>
#include <string.h>
int main(void) {
  while(1) {
	int n, p;
	scanf("%d%d", &n, &p);
	if (n == p && n == 0) break;
	int man[444] = {0};
	int glass = 0;
	int i = 0;
	glass = p;
	while (1) {
	  //  printf("i = %d  glass = %d  man[i] == %d\n",i,glass,man[i]);
	  if (glass != 0) {
		glass--; man[i]++;
	  }
	  else {
		glass = man[i];
		man[i] = 0;
	  }
	  int flag = 0;
	  for (int j = 0; j < n; j++) {
		if (j != i) {
		  if (man[j] != 0) {
			flag = 1;
			break;
		  }
		}
	  }
		
	  if (flag == 0 && glass == 0) break;
	  i = (1+i)%n;
	}
	printf("%d\n",i);
  }
  return 0;
}

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

using namespace std;

int n,p;
vector<int> ans;
int main(){
	while(1){
		cin >> n >> p;
		if(n + p == 0) break;
		int sp = p;
		int np[50]={0};
		int win = -1;
		while(1){
			for(int i = 0;i < n;i++){
				if(p>0){
					p--;
					np[i]++;
					if(np[i] == sp) win = i;
				}else{
					p+=np[i];
					np[i] = 0;
				}
				if(win >= 0) break;
			}
			if(win >= 0) break;
		}
		ans.push_back(win);
	}
	for(int i = 0;i < ans.size();i++){
		cout << ans[i] << endl;
	}
}

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

struct Candidate{
	int num = 0;
};

int main() {
	for (;;) {
		int n, p;
		cin >> n >> p;
		if (n == 0 && p == 0)
			break;
		Candidate candidate[50];
		
		int winner;
		int t = p;
		for (int i = 0; i <= 1000000; i=(i+1)%n) {
			if (p != 0) {
				candidate[i].num++;
				p--;
			}
			else if (p == 0) {
				p+=candidate[i].num;
				candidate[i].num = 0;
			}
			if (candidate[i].num == t) {
				winner = i;
				break;
			}
		}
		printf("%d\n", winner);
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

int main(){
	int n,p;
	int i=0;
	int data[50];
	int allp;
	while(cin >> n >> p, n+p){
		for(int j=0; j < n; j++){
			data[j]=0;	
		}
		i = 0;
		allp = p;
		while(1){
			
			if(p > 0){
				p--;
				data[i]++;
			}
			else{
				p = data[i];
				data[i] = 0;
			}
			if(allp == data[i]) break;
			i++;
			if(i == n) i = 0;
		}
		cout << i << endl;
	}	
}

C++: #include <iostream>
//#include <algorithm>
//#include <vector>
//#include <stack>
//#include <queue>
//#include <map>
//#include <cmath>
//#include <string>
//#include <sstream>
//#include <iomanip>
//#include <complex>
using namespace std;
 
//#define ll long long
//#define vvi vector< vector<int> >
//#define All(X) X.begin(),X.end()
//#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
//#define pb push_back 
//ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int main(){
  while(1){
    int n, p;
    cin >> n >> p;
    if(n==0&&p==0) break;
    int kago=p;
  	int pc[100]={0};
    int i=0;
    while(1){
      int nowc=i%n;
      if(pc[nowc]==p){
	cout << nowc << endl;
	break;
      }
      if(kago>0){
	kago--;
	pc[nowc]++;
      }
      else{
	kago=pc[nowc];
	pc[nowc]=0;}
      i++;
    }
  }
  return 0;
}

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

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define sz size()
#define pb push_back
#define mp make_pair
#define ALL(X) (X).begin(),(X).end()

using namespace std;

const int INF = 1000000000;
const double eps = 1e-8;

int main(void) {
  while(1){
    int n,p;
    cin>>n>>p;
    if(!(n||p))break;
    vector<int> a(n,0);
    int op=p;
    int i=0;
    while(1){
      if(p>0){
        a[i]++;p--;
        if(p==0&&a[i]==op){
          cout<<i<<endl;
          break;
        }
      }else{
        p=a[i];a[i]=0;
      }
      i++;
      if(i==n)i=0;
    }
  }
  return 0;
}

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

int main(){
    int N, P;
    while(true){
        scanf("%d%d", &N, &P);
        if(N == 0 && P == 0) break;
        int c[50], left = P;
        fill(c, c + N, 0);
        int turn = 0;
        while(true){
            if(left == 0){
                left += c[turn];
                c[turn] = 0;
            }else{
                --left;
                c[turn] += 1;
            }
            if(c[turn] == P){
                break;
            }
            turn = (turn + 1) % N;
        }
        printf("%d\n", turn);
    }
    return 0;
}

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

int main() {
  int n,p;
  
  while(1) {
  cin >> n >> p;
  if(n == 0 && p == 0) break;
  int a[51]={};

  int ima = 0;
  int stn=p;
  while(1) {
 
    if(stn > 0) {
    a[ima%n] += 1;
    stn--;
    if(a[ima%n] == p) break;
    }
    else if(stn == 0){
      stn+=a[ima%n];
      a[ima%n] = 0;
    }
    ima++;
  }

  cout << (ima)%n<<endl;
  }

  return 0;

}

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

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

int main()
{
	int n, p;
	while (cin >> n >> p, n || p) {
		int save_p = p;
		vector<int> keeps(n, 0);
		for (int i = 0; true; ++i) {
			i %= n;
			if (p) {
				++keeps[i];
				--p;
				if (keeps[i] == save_p) {
					cout << i << endl;
					break;
				}
			} else {
				p = keeps[i];
				keeps[i] = 0;
			}
		}
	}
}