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 <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[n-1]={0};
    int i=0;
    while(1){
      int k=i%(n-1);
      if(pc[k]==p){
	cout << k << endl;
	break;}
      if(kago>0){
	kago--;
	pc[k]++;}
      else{
	kago=pc[k];
	pc[k]=0;}
      i++;}
  }

  return 0;
}

C++: #include <cstdio>
#include <cassert>
#include <utility>
#include <vector>

int testcase_ends() {
  size_t n;
  int p;
  scanf("%zu %d", &n, &p);
  if (n == 0 && p == 0) return 1;

  int p0 = p;
  std::vector<int> temoto(n);
  for (size_t i = 0; i <= 1000000; ++i) {
    size_t j = i % n;
    if (p == 0) {
      std::swap(p, temoto[j]);
    } else {
      --p;
      ++temoto[j];
      if (temoto[j] == p0) return !printf("%zu\n", j);
    }
  }
  assert(false);
}

int main() {
  while (!testcase_ends()) {}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n, p;
	while (cin >> n >> p, n)
	{
		int w = p;
		int a[50] = {};
		int cnt = 0, pos = 0;
		while (w || cnt!=1)
		{
			if (w)
			{
				cnt += !a[pos%n];
				a[pos%n]++;
				w--;
			}
			else
			{
				if (a[pos%n])w += a[pos%n], a[pos%n] = 0, cnt--;
			}
			pos++;
		}
		for (int i = 0;i < n;i++)if (a[i])cout << i << endl;
	}
}

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

int main(void){
  while(1){
    int stone[50];
    for(int k = 0 ; k < 50 ; k++){stone[k] = 0;}

    int bowl;
    int n, p;  // n：候補者人数，p：小石の総数
    cin >> n;
    cin >> p;

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

    bowl = p;

    bool flg = true;
    int ans;

    for(int i = 0 ; flg ; i = (i+1) % n){

      if(bowl == 0){
        bowl = bowl + stone[i];
        stone[i] = 0;
      }else{
        stone[i]++;
        bowl--;
        if(bowl == 0 && stone[i] == p){ans = i; flg = false;}
      }
    }
    cout << ans << endl;
  }
}

あっはっは

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for (int i=(n)-1;i>=0;i--)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define REPS(i,f,n) for (int i=(f)-1;i>=(n);i--)
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};

void slove(int n, int p){
	int tyawan = p;//??¶?¢??????\??£???????????°
	vint person(n);//????£????????????£???????????°
	rep(i, n) person[i] = 0;//?????????
	int cnt = 0;
	do{
		if (tyawan >= 1){//????????¨???
			person[cnt % n]++;//?????????????????£??????????????¨???????????????mod??§
			tyawan--;
		}else{//???????????¨??¨??\??????
			tyawan += person[cnt % n];
			person[cnt % n] = 0;
		}
		cnt++;

	} while (person[cnt % n] + tyawan != p);

	printf("%d\n", cnt % n);
	return;
}

int main(void){
	int n, p;
	scanf("%d %d", &n, &p);
	while(n != 0 || p != 0){
		slove(n, p);
		scanf("%d %d", &n, &p);
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;

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

typedef long long ll;

void solve(){
}

int main(){
  int n,p;

  while(cin>>n>>p,n||p){
    int rem[n+2];
    memset(rem,0,sizeof(rem));
    int stone = p;
    int now = 0;
    int winner = -1;

    while(1){
      if(stone >= 1){
        rem[now]++;
        stone--;
        if(stone == 0 && rem[now] == p){
          winner = now;
          break;
        }
      }
      else{
        stone += rem[now];
        rem[now] = 0;
      }

      now = (now + 1) % n;
    }

    cout<<winner<<endl;
  }
}

C++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;


int main(){
 
	int n,p;
	while(cin>>n>>p,n||p){
		vector<int> v(n);
		int i = 0;
		int w = p;
		for(;i < 1000000+1; i++){
			if(p){
				v[i%n]++;
				p--;
			}else {
				p = v[i%n];
				v[i%n] = 0;
			}

			if(p==0&&v[i%n]==w) break;
		}

		cout << i%n << endl;
	}
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i=0;i<n;++i)
#define Rep(i,n) for(int i=1;i<=n;++i)
#define rrep(i,n) for(int i=n-1;0<=i;--i)
#define rRep(i,n) for(int i=n;0<i;--i)
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define scani(a) scanf("%d",&a)
#define scanr(a) scanf("%lf",&a)
#define scans(s) scanf("%s",s)
#define printi(a) printf("%d",a)
#define printr(a) printf("%lf",a)
#define prints(s) printf("%s",s)
#define is_odd(n) (n&1)
#define is_even(n) ((n+1)&1)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
#define all(x) x.begin(), x.end()
#define EPS 1e-8
#define INF 1e8
#define PI acos(-1.0)

typedef double Real;
typedef int Int;
typedef vector<Int> vi;
typedef vector<vector<Int> > vvi;
typedef pair<Int,Int> pii;

inline Real sr(Real a) {return sqrt(max(a, (Real)0));}
template <typename T> inline T sq(T a) {return a * a;}
const vi dy = {-1, 0, 1, 0}, dx = {0, -1, 0, 1};
vector<string> split(const string &s, char d) {vector<string> r;stringstream t(s);string x;while(getline(t,x,d))if(!x.empty())r.push_back(x);return r;}

void solve()
{
  for(;;)
  {
    Int n, p; scani(n); scani(p);
    if(n == 0 && p == 0) break;

    vi cs(n, 0);

    for(Int i = 0, count = p;;i=(i+1)%n)
    {
      if(count > 0)
      {
        ++cs[i];
        --count;
        if(count == 0)
        {
          bool ok = true;
          rep(j, n) if(i != j) ok &= cs[j] == 0;
          if(ok)
          {
            cout << i << endl;
            break;
          }
        }
      }
      else
      {
        count += cs[i];
        cs[i] = 0;
      }
    }
  }
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(9);

  solve();

  return 0;
}