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 <iostream>
using namespace std;

struct Candidate{
	int num = 0;
};

int main(void) {
	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>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;

int main(){
  while(1){
    int N;
    cin >> N;
    if(N==0) break;
    int x,y,map[22][22]={};
    rep(i,N){
      cin >> x >> y;
      map[y][x] = 1;
    }
    int Rx = 10,Ry = 10,tr = 0;
    int M;
    cin >> M;
    rep(i,M){
      string v;
      int d;
      cin >> v >> d;
      if(v == "N"){
	rep(j,d){ 
	  Ry++;
	  tr += map[Ry][Rx];
	}
      }
      else if(v == "E"){
	rep(j,d){ 
	  Rx++;
	  tr += map[Ry][Rx];
	}
      }
      else if(v == "S"){
	rep(j,d){ 
	  Ry--;
	  tr += map[Ry][Rx];
	}
      }
      else if(v == "W"){
	rep(j,d){ 
	  Rx--;
	  tr += map[Ry][Rx];
	}
      }
    }
    if(tr == N) cout << "Yes\n";
    else cout << "No\n";
  }
  return 0;
}

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

int main(){
	int people;
	int stone;
	int hand[55];
	while(1){
		int j=0;
		for(int i=0; i < 54; i++) hand[i] = 0;

		cin >> people >> stone;

		if(people == 0 && stone == 0) break;

		int box = stone;

		while(1){
			if(box == 1){
				int cont = 0;
				for(int i=0; i < people; i++){
					if(hand[i % people] == 0) cont++;
				}
				if(people == cont+1) break;
				else{
					hand[j % people]++;
					box--;
				}
			}
			else if(box != 0){
				hand[j % people]++;
				box--;
			}
			else if(box == 0){
				box += hand[j % people];
				hand[j % people] = 0;
			}
			j++;
		}
		cout << j % people << endl;
	}
	return 0;
}

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

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TEMP_T T    cums(T& v){ T r(v.size()); partial_sum(v.begin(), v.end(), r.begin()); return r; }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }
TEMP_T T    acum(vector<T>& v, T b=0){ return accumulate(v.begin(), v.end(), b); }
TEMP_T vector<T> inpt(int n){ vector<T> v(n); for (int i=0; i<n; i++) cin >> v[i]; return v; }

static inline int in(){ int x; scanf("%d", &x); return x; }

int main()
{
  int n, p;
  while (n = in(), p = in(), n || p){
    int id = 0;
    int stone[64] = {};
    int cnt = p;
    int have_user = 0;
    while (1){
      if (cnt == 1 && have_user == 1){
        break;
      }
      if (cnt == 0){
        if (stone[id]) have_user--;
        cnt = stone[id];
        stone[id] = 0;
      }
      else {
        if (stone[id] == 0) have_user++;
        cnt--;
        stone[id]++;
      }
      id = (id + 1) % n;
    }
    printf("%d\n", id);
  }
  return (0);
}

C++: #include <stdio.h>
#include <string.h>

int n, p, a[64];

int main() {
    for (;;) {
        scanf("%d%d", &n, &p);
        if (n == 0) return 0;
        memset(a, 0, sizeof(a));
        int i = 0, x = p;
        for (;;) {
            if (x) {
                x--, a[i]++;
                if (a[i] == p) {
                    printf("%d\n", i);
                    break;
                }
            }
            else {
                x = a[i];
                a[i] = 0;
            }
            i = (i+1) % n;
        }
    }
}

C++: #include<iostream>
using namespace std;
int main()
{
	int n,p;
	while(cin>>n>>p,n) 
	{
		int nums[n];
		for(int i = 0; i < n; ++i)
		{
			nums[i]=0;
		}
		int zero=n;
		int pos=0;
		while(1)
		{
			if(p>=1)
			{
				p--;
				if(nums[pos]==0) zero--;
				nums[pos]++;
			}
			else
			{
				p=nums[pos];
				if(nums[pos]!=0) zero++;
				nums[pos]=0;
			}	
			if(p==0&&zero==n-1) break;
			pos=(pos+1)%n;
		}
		cout<<pos<<endl;
	}
  return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n||p){
		int a[51]={0},ans=0,now=0;
		while(true){
			int cnt=0;
			for(int i=0;i<n;i++)if(a[i]!=0)cnt++;
			if(cnt==1&&p==0)break;
			
			if(p!=0){
			a[now]++;
			p--;
			}
			else {
			p+=a[now];
			a[now]=0;
			}
		if(now==n-1)now=0;
		else now++;
		//for(int x=0;x<n;x++)cout<<a[x]<<" ";cout<<endl;
		}
	for(int i=0;i<n;i++)if(a[i]!=0)cout<<i<<endl;
	for(int i=0;i<51;i++)a[i]=0;
	}
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <tuple>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define DECIM8  fixed<<setprecision(8) 
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

using namespace std;
int main(void)
{
  for(;;){
    int n,p;
    int stone[64]={};
    cin >> n >> p;
    stone[n]=p;
    if(!n&&!p) return 0;
    for(int pos=0;;pos=(pos+1)%n){
      if(stone[n]){
	stone[n]--;
	stone[pos]++;
	if(stone[pos]==p){
	  cout << pos << endl;
	  break;
	}
      }else{
	stone[n]+=stone[pos];
	stone[pos]=0;
      }
    }
  }
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF INT_MAX 
#define mod 1000000007
using namespace std;
int main(){
    ll wan;
    ll N,P,i=0;
    vector<int> people(100,0);
    while(1){
        cin >> N >> P;
        if(N==0&&P==0) return 0;
        wan = P;
        for(int i=0;i<N;i++) people[i]=0;
        i=0;
        while(1){
            if(wan>0){
                people[i]++;
                wan--;
                if(people[i]==P){
                    cout << i << endl;
                    break;
                }
            }else{
                wan = people[i];
                people[i] = 0;
            }
            i++;
            if(i==N) i=0;
        }
    } 
}