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 <bitset>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <functional>
#include <cctype>
#include <list>
#include <limits>
//#include <boost/multiprecision/cpp_int.hpp>

const double EPS = (1e-10);


using namespace std;
using Int = long long;
//using namespace boost::multiprecision;

const Int MOD = 1000000007;

Int mod_pow(Int x, Int n) {
    Int res = 1;
    while(n > 0) {
        if(n & 1) res = (res * x) % MOD; //ビット演算(最下位ビットが1のとき)
        x = (x * x) % MOD;
        n >>= 1; //右シフト(n = n >> 1)
    }
    return res;
}

//最大公約数
template<typename T>
T gcd(T a, T b) {
    return b != 0 ? gcd(b, a % b) : a;
}

//最小公倍数
template<typename T>
T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

int solve(int N, int P){
    vector<int> cans(N);
    int p = P;
    for (int i = 0;; i++){
        if (p){
            cans[i%N]++;
            p--;
        }else{
            p = cans[i%N];
            cans[i%N] = 0;
        }
        if (cans[i%N] == P) return i%N;
    }
    return -1;
}

int main(){
    cin.tie(0);

    int N, P; 
    vector<int> ans;
    while (1){
        cin >> N >> P;
        if (!N && !P) break;
        ans.push_back(solve(N, P));
    }
    for (auto itr = ans.begin(); itr != ans.end(); itr++){
        cout << *itr << endl;
    }

}

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

int main(){
    while(1){
        int n, p; cin>>n>>p;
        if(n == 0) return 0;

        vector<int> cnt(n, 0);
        int sum = p;
        int step = 0;
        while(1){
            if(0 < p){
                cnt[step%n]++;
                p--;
            }
            else if(0 < cnt[step % n]){
                p += cnt[step%n];
                cnt[step%n] = 0;
            }

            if(cnt[step%n] == sum){
                cout << step % n << endl;
                break;
            }

            step++;
        }
    }
}

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

using ll = long long;
using ull = unsigned long long;

const double PI = acos(-1);
const int inf = 2e9;
const long long INF = 2e18;
const long long MOD = 1e9+7;

#define sx(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i,n) for (int i = 0; i < n; i++)
#define ALL(a) begin(a),end(a)

int main(void) {
    int n,p;
    while (cin >> n >> p, n) {
        vector<int> st(n,0);
        int wan = p;
        int pl = 0;
    
        while (true) {
            if (wan == 0) {
                wan += st[pl];
                st[pl] = 0;
            }
            else {
                st[pl]++;
                if (st[pl] == p) break;
                wan--;
            }
            pl = (pl + 1) % n;
        }
        cout << pl << endl;
    }

    return 0;
}

C++: #include <iostream>
#define REP(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;

int N, P, a[50];

int main(void) {
  while(cin >> N >> P, N && P) {
    REP(i, 0, N) a[i] = 0;

    for(int i = 0; true; i = (i + 1) % N) {
      if(P == 0) {
        P = a[i];
        a[i] = 0;
      } else {
        P--;
        a[i]++;
        if(P == 0) {
          bool fin = true;
          REP(j, 0, N) if(j != i && a[j] != 0) fin = false;
          if(fin) {
            cout << i << endl;
            break;
          }
        }
      }
    }
  }

  return 0;
}

C++: #include <stdio.h>

int main(void){
	int n, p, c[50], i, count, sum;
	scanf("%d%d",&n,&p);
	while(n != 0 || p != 0){
		for(i = 0;i < n;i++)
			c[i] = 0;
		sum = p; count = 2;
		while(count != 1 || sum != 0){
			for(i = 0;i < n;i++){
				if(sum == 0){
					sum = c[i];
					c[i] = 0;
				}
				else{
					sum--;
					c[i]++;
				}
			}
			for(i = 0, count = 0;i < n;i++)
				if(c[i] != 0) count++;
		}
		for(i = 0;i < n;i++)
			if(c[i] != 0) printf("%d\n",i);
		scanf("%d%d",&n,&p);
	}
	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstdint>
//#include<>
using namespace std;

typedef int64_t i64;typedef int32_t i32;
typedef int16_t i16;typedef int8_t i8;
typedef uint64_t ui64;typedef uint32_t ui32;
typedef uint16_t ui16;typedef uint8_t ui8;

typedef i32 int__;
#define rep(i,j) for(int__ i=0;i<j;i++)
#define repeat(i,j,k) for(int__ i=(j);i<(k);i++)
#define repeat_eq(i,j,k) for(int__ i=(j);i<=(k);i++)
#define all(v) begin(v),end(v)

const i32 INF=1<<30;//=10E10

int main()
{
  while(true){
    i32 n,p;cin>>n>>p;
    if(!n&&!p)return 0;
    vector<i32> hold(n,0);
    i32 cup=p;
    int now=0;
    
    while(true){
      if(cup>0){
	hold[now]+=1;
	--cup;
	if(hold[now]==p){
	  cout<<now<<endl;
	  break;
	}
      }
      else{
	cup+=hold[now];
	hold[now]=0;
      }
      if(++now>=n)now=0;
    }

  }
  
  return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
#include<deque>
using namespace std;
bool check(int *a,int n,int p){
	for(int i = 0; i < n; i++){
		if(a[i] == p){
			printf("%d\n",i);
			return true;
		}
	}
	return false;
}
int main(){
	int n,p;
	while(scanf("%d%d",&n,&p),n){
		int koisi[55]={0};
		int index = 0;
		int cha = p;
		while(!check(koisi,n,p)){
			if(cha > 0){
				koisi[index]++;
				cha--;
			}
			else if(cha == 0){
				cha = koisi[index];
				koisi[index] = 0;
			}
			++index%=n;
		}
	}
	return 0;
}

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

bool check(vector<int> &motigoma) {
    int cnt = 0;
    for (int i : motigoma)
        if (i != 0) cnt++;
    if (cnt == 1)
        return true;
    else
        return false;
}

int solve(int N, int P) {
    vector<int> motigoma(N, 0);
    int nokori = P;
    int sum    = P;
    int i      = 0;
    while (1) {
        if (nokori != 0) {
            motigoma[i]++;
            nokori--;
        } else if (sum - motigoma[i] == 0) {
            return i;
        } else {
            nokori += motigoma[i];
            motigoma[i] = 0;
        }
        i++;
        i %= N;
    }
}

int main() {
    int N, P;
    vector<int> ans;
    while (cin >> N >> P) {
        if (!N && !P) break;
        ans.push_back(solve(N, P));
    }
    for (int a : ans) cout << a << endl;
}

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

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

C++: #include <stdio.h>
int main()
{
  int t[50],n,s,storn,i,j;
  while(1){
    scanf("%d %d",&n,&storn);
    //printf("%d %d",n,storn);///
    if(n==0&&storn==0)break;
    for(i=0;i<n;i++)t[i]=0;
    j=0;
    s=storn;
    while(1){
      //printf("%d %d %d %d\n",j,s,t[j],storn);///
      if(s!=0){
	t[j]++;
	s--;
      }else if(s==0){
	s=t[j];
	t[j]=0;
      }
      if(t[j]==storn)break;
      j++;
      if(j==n)j=0;
    }
    printf("%d\n",j);
  }
  return 0;
}