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>
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;
    cout << endl;
  }
}

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

using namespace std;

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

        vector<int> player(n);

        int stone = p;
        bool flag = false;
        int ans = 0;
        while (1){
            for (int i = 0; i < n; i++){
                if (stone > 0){
                    --stone;
                    ++player[i];
                }
                else{
                    stone = player[i];
                    player[i] = 0;
                }

                if (player[i] == p){
                    flag = true;
                    ans = i;
                    break;
                }
            }
            if (flag) break;
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i, n) for(int i = 0;i < n;i++)
#define repb(i, n) for(int i = n;i >= 0;i--)
#define reps(i, m, n) for(int i = m;i < n;i++)
#define repsb(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef long long ll;
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};
using namespace std;
int n,p,i=0,temp,pos;
bool flg=true;
int pre[50]={};

void cal(void){
  while(true){
    if(temp){
      pre[pos]++;
      temp--;
      if(pre[pos]==p){
        cout<<pos<<endl;
        return;
      }
    }
    else{
      temp=pre[pos];
      pre[pos]=0;
    }
    pos++;
    if(pos==n) pos=0;
  }
}
int main(int argc, char const *argv[]) {

 while (true) {
   cin>>n>>p;
   if(n==0&&p==0) break;
   rep(i,n) pre[i]=0;
   temp=p;
   pos=0;
   cal();
 }
 return 0;
}

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

int main(){
    int n, p;
    while(cin >> n >> p, n+p){
        int a[50] = {}, wan = p, step = 0;
        while(1){
            if(wan > 0) wan--, a[step%n]++;
            else if(a[step%n])  wan += a[step%n], a[step%n] = 0;
            if(a[step%n] == p)  break;
            step++;
        }
        cout << step%n << endl;
    }
    return 0;
}

C++: import numpy as np

while True:
    try:
        l = list(map(int, input().split()))
        S = l[0]
        p = np.zeros(S)
        M = l[1]
        i = 0
        N = M

        while True:
            if N == M and i != 0:
                ans = i % S - 1
                if ans < 0:
                    ans = S + ans
                print(ans)
                break

            if N == 0:
                N = p[i % S]
                p[i % S] = 0
                i += 1

            else:
                N -= 1
                p[i % S] += 1
                i += 1

    except:
        break

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

using namespace std;

int st[51];
int n,p;

bool isended(int arg){
    bool ret = true;
    for(int i=0;i<n;i++){
        if(i == arg) continue;
        if(st[i] != 0){
            ret = false;
            break;
        }
    }
    return ret;
}

int main(){
    while(cin >> n >> p){
        if((n|p) == 0) break;
        int turn = 0;

        memset(st, 0, sizeof(st));
        
        while(1){
            if(p > 0){
                p--;
                st[turn]++;
                if(p == 0 &&
                   isended(turn)){
                    break;
                }
            }else{
                p += st[turn];
                st[turn] = 0;
            }
            turn++;
            turn %= n;
        }
        cout << turn << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  long long int n=0,p=0,count=0,answer=0;



while(1){
cin >> n >> p;
vector<int> v(n,0);
if(n==0&&p==0)
  return 0;
  while(1){
    for(int i=0;i<n;i++){
      if(p==0){

        if(v[i]!=0){
          p+=v[i];
          v[i]=0;
          count=0;
        }else{
          count++;
        }

      }else{
        v[i]++;
        p--;
        count=0;
      }
      if(count==n-1){
      //  cout << count << endl;
        break;
      }
    }if(count==n-1){
    //  cout << count << endl;
      break;
    }


  }


  for(int i=0;i<n;i++){
    if(v[i]!=0){
      answer = i;
    }
  }

  cout<<answer<<endl;
}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int n, p;
    while (cin >> n >> p, n) {
        vector<int> v(n, 0);
        int pot = p;
        rep(r, 1000000) {
            int i = r % n;
            if (pot > 0) {
                pot--;
                v[i]++;
            } else {
                pot += v[i];
                v[i] = 0;
            }

            if (v[i] == p) {
                cout << i << endl;
                break;
            }
        }
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,p;
  while(cin>>n>>p,n+p!=0){
    int winner=0;
    int stone=p;
    int candidate[n]={0};
    int now=0;
    while(1){
      if(stone==0){
	stone+=candidate[now];
	candidate[now]=0;
      }else{
      candidate[now]++;
      stone--;
      }
     if(candidate[now]==p){
	  winner=now;
	  break;
	}
      if(now==n-1)
	now=0;
      else
	now++;
    }
    cout<<winner<<endl;
  }
  return 0;
}

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

int main() {
    int n, p; cin>>n>>p;
    while(n != 0 && p != 0){
        int mp = p;
        vector<int> stone(n, 0);
        bool flag = false;
        while(!flag){
            for(int i=0; i<n; i++){
                if(mp != 0){
                    stone[i]++;
                    mp--;
                    if(stone[i] == p){
                        cout << i << endl;
                        flag = true;
                        break;
                    }
                }else{
                    mp += stone[i];
                    stone[i] = 0;
                }
            }
        }
        cin >> n >> p;
    }
}