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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)

int main(void){
  int n,p;
  while(cin>>n>>p){
    if(n==0) break;
    vector<int> a(n);
    int i=0;
    while(1){
      if(p){
        p--;
        a[i]++;
        if(p==0&&count(a.begin(),a.end(),0)==n-1){
          cout<<i<<endl;
          break;
        }
      }else{
        p = a[i];
        a[i] = 0;
      }
      i=(i+1)%n;
    }
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	int i,j=0,n,p,temp;
	int kouho[300];

		while(1){
			for(i=0;i<300;i++)
			kouho[i]=0;
			cin >> n >> p;
			temp =p;
			if(n==0) break;
			if(p!=0){
				for(i=0;;i++){
					if(i==n)	i=0;
					if(p!=0){
						kouho[i]+=1;
						p-=1;
					}
					else{
						if(kouho[i]==temp){
							cout << i;
							puts("");
							break;
						}else{
							//cout << kouho[i];
							p=kouho[i];
							kouho[i]=0;
						}
					}	//p=0
				}
			}
		}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
template <typename T> using V = vector<T>;

bool solve() {
    ll N, P;
    cin >> N >> P;
    if(!(N + P)) return false;

    V<ll> has(N);
    ll cup = P;

    while(true) {
        bool suc = false;
        for(ll i = 0; i < N; i++) {
            if(cup > 0) {
                cup--;
                has[i]++;
                if(has[i] == P) {
                    cout << i << endl;
                    suc = true;
                    break;
                }
            } else {
                cup += has[i];
                has[i] = 0;
            }
        }
        if(suc) break;
    }
    return true;
}

int main() {
    while(solve());
    return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;

int main()
{
  while(1) {
    int n, p;
    cin >> n >> p;
    if(n == 0) break;
    int num[n];
    FOR(i,0,n) num[i] = 0;
    int wan = p;
    FOR(i,0,1000006) {
      int now = i % n;
      if(wan > 0) {
        wan--;
        num[now]++;
        if(num[now] == p) {
          cout << now << endl;
          break;
        }
      } else {
        wan = num[now];
        num[now] = 0;
      }
    }
  }
  return 0;
}

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

int main(void){
  char step[1000010];

  while(1) {
    cin >> step;
    if(step[0] == '#') break;
    int counter = 0;
    int state;

    if(step[0] == '1' || step[0] == '4' || step[0] == '7') state = 6;
    else if(step[0] == '2' || step[0] == '8') state = 7;
    else state = 8;
    
    int i;
    
    for(i = 1; step[i] != '\0'; i++){

      if(state == 6){
        if(step[i] == '1' || step[i] == '4' || step[i] == '7'){
          continue;
        }else if(step[i] == '8' || step[i] == '2'){
          state = 3;
          continue;
        }else{
          state = 5;
          continue;
        }
      }

      if(state == 7){
        if(step[i] == '2' || step[i] == '8'){
          continue;
        }else if(step[i] == '1' || step[i] == '4' || step[i] == '7'){
          state = 0;
          continue;
        }else{
          state = 5;
          continue;
        }
      }

      if(state == 8){
        if(step[i] == '9' || step[i] == '6' || step[i] == '3'){
          continue;
        }else if(step[i] == '8' || step[i] == '2'){
          state = 2;
          continue;
        }else{
          state = 0;
          continue;
        }
      }
      

      if(state == 0){
        if(step[i] == '3' || step[i] == '6' || step[i] == '9'){
          state = 5;
          continue;
        }else if(step[i] == '8' || step[i] == '2'){
          state = 3;
          continue;
        }else{
          state = 1;
          continue;
        }
      }

      if(state == 1){
        if(step[i] == '4' || step[i] == '7'|| step[i] == '1'){
          state = 0;
          continue;
        }else if(step[i] == '8' || step[i] == '2'){
          counter++;
          state = 3;
          continue;
        }else{
          counter++;
          state = 5;
          continue;
        }
      }

      if(state == 2){
        if(step[i] == '3' || step[i] == '6' || step[i] == '9'){
          state = 5;
          continue;
        }else if(step[i] == '2' || step[i] == '8'){
          state = 3;
          continue;
        }else{
          counter++;
          state = 0;
          continue;
        }
      }

      if(state == 3){
        if(step[i] == '1' || step[i] == '4' || step[i] == '7'){
          state = 0;
          continue;
        }else if(step[i] == '2' || step[i] == '8'){
          state = 2;
          continue;
        }else{
          counter++;
          state = 5;
          continue;
        }
      }

      if(state == 4){
        if(step[i] == '3' || step[i] == '6' || step[i] == '9'){
          state = 5;
          continue;
        }else if(step[i] == '8' || step[i] == '2'){
          counter++;
          state = 2;
          continue;
        }else{
          counter++;
          state = 0;
          continue;
        }
      }

      if(state == 5){
        if(step[i] == '6' || step[i] == '3' || step[i] == '9'){
          state = 4;
          continue;
        }else if(step[i] == '8' || step[i] == '2'){
          state = 2;
          continue;
        }else{
          state = 0;
          continue;
        }
      }
    }

    cout << counter << endl;
  }
  return 0;
}

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

using namespace std;

bool check(vector<int> v){
	int sum = 0;
	for (int i = 0; i < v.size(); i++){
		if (v[i] != 0) sum++;
	}
	if (sum == 1){
		return true;
	}else{
		return false;
	}
}


int main(){
	while(1){
		int n; cin >> n;
		int p; cin >> p;
		if (n == 0 and p == 0) break;
		int wan = p;
		vector<int> player_have(n);
		int player = 0;
		int idx = 0;
		while(wan != 0 or !check(player_have) ){
			player = idx%n;
			//cout << "wan = " << wan <<  endl;
			if (wan > 0){
				player_have[player] += 1;
				wan -= 1;
			}else if (wan == 0){
				wan = player_have[player];
				player_have[player] = 0;
			}
			idx++;;
		}
		cout << player << endl;
		player_have.clear();
	
	}

return 0;
}