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 n, p, r, t;
	bool f;
	
	while(cin >> n >> p, n) {
		t = p;
		f = 0;
		vector<int> v(n);
		for (int i = 0; i < n; i++) v[i] = 0;
		
		while (1) {
			for (int i = 0; i < n; i++) {
				if (!t) {
					t += v[i];
					v[i] = 0;
				} else {
					v[i] ++;
					t--;
					if (v[i] == p) {
						f = 1;
						r = i;
						break;
					}
				}
			}
			if (f) break;
		}
		
		cout << r << endl;
	}
	
	return 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];
                for(int i=0; i<n; i++) {
                        s[i]=0;
                }
                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 <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define each(x, y) for (auto &x : y)
#define all(a) (a).begin(), (a).end()
#define bit(b) (1ll << (b))

using namespace std;

using i32 = int;
using i64 = long long;
using u64 = unsigned long long;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;
using vu64 = vector<u64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

inline void yes() { cout << "Yes" << '\n'; exit(0); }
inline void no() { cout << "No" << '\n'; exit(0); }
inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); }
inline i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; }
inline u64 xorshift() { static u64 x = 88172645463325252ull; x = x ^ (x << 7); return x = x ^ (x >> 9); }
void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; }
template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> inline void amax(T &x, T y) { if (x < y) x = y; }
template <typename T> inline void amin(T &x, T y) { if (x > y) x = y; }
template <typename T> inline T power(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }
template <typename T> istream& operator>>(istream &is, vector<T> &v) { each(x, v) is >> x; return is; }
template <typename T> ostream& operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; }
template <typename T, typename S> istream& operator>>(istream &is, pair<T, S> &p) { is >> p.first >> p.second; return is; }
template <typename T, typename S> ostream& operator<<(ostream &os, pair<T, S> &p) { os << p.first << ' ' << p.second; return os; }

void solve() {
  while (true) {
    int n, p; cin >> n >> p;
    if (n == 0) return;
    vi32 v(n);
    int idx = 0;
    while (true) {
      if (p) v[idx]++, p--;
      else {
        p += v[idx], v[idx] = 0;
      }
      if (p == 0) {
        bool f = false;
        rep(i, n) if (idx != i && v[i] > 0) f = true;
        if (!f) {
          cout << idx << '\n';
          break;
        }
      }
      idx++;
      if (idx >= n) idx -= n;
    }
  }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define mod 1000000007 //1e9+7
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 51

void calc(int n,int p){
  int stone[SIZE] = {};
  int q = p;

  while(1){

    for(int i=0;i<n;i++){
      if(q > 0){
	stone[i]++;
	q--;
      }else{
	q += stone[i];
	stone[i] = 0;
      }

      if(stone[i]==p){
	printf("%d\n",i);
	return;
      }
    }

  }
  
}

int main(){
  int n,p;

  while(1){
    scanf("%d%d",&n,&p);
    if(n==0) break;
    calc(n,p);
  }
  
  return 0;
}

C++: #include<iostream>
#include<string>
#include<cmath>

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

using namespace std;

int main()
{
	int n,p;
	while(cin>>n>>p && n && p)
	{
		int hand[100000]={};
		int i=0,number=0;
		int hako=p;
		
		while(1)
		{
			number=i%n;
			
			if(hand[number]==p)break;
			
			if(hako!=0)
			{
				hako--;
				hand[number]++;
			}
			else
			{
				hako=hand[number];
				hand[number]=0;
			}
			i++;
		}
		cout<<number<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#define INF 999999999
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define MOD(x) (x%(mod))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

int n,p;
int people[55];

void init(){
  rep(i,55)people[i]=0;
}

void solve(){
  int stone=p;
  for(int i=0;;i++){
    
    if(people[i%n]==p){
      
      cout<<i%n<<endl;
      break;
    }else if(stone==0){
      stone=people[i%n];
      people[i%n]=0;
    }else{
      people[i%n]++;
      stone--;
    }
  }
}

int main(){
  while(cin>>n>>p){
    if(n==0&&p==0)break;
    init();
    solve();
  }
}