Problem:


FizzBuzz

「Fizz Buzz」と言われる数字を使ったゲームがあります。このゲームは複数のプレイヤーで数字を1 から順にひとつずつ数え上げていくもので、各プレイヤーは直前のプレイヤーが発言した次の数字をひとつだけ発言します。その時、3 で割り切れる場合は 「Fizz」, 5 で割り切れる場合は 「Buzz」、両者で割り切れる場合は「FizzBuzz」と数の代わりに発言しなければなりません。例えば、最初の 16 までの発言は以下のようになります。

    1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, ・・・

  太郎君は友達と「Fizz Buzz」をして遊ぶことにしました。太郎君たちはルールを次のように決めました。 

  

  「間違えた人は脱落する。その次の人は間違えた数の次の数から始める。つまり、1, 2, 3 と発言した場合、3 で間違えたので次は 4 から始めることになる。」

  

このルールに従ってゲームを行うのですが、ゲームに慣れていないため、間違えたことに気付かないことがあり、公平な判断ができません。そこであなたは太郎君たちがこのゲームを楽しめるように、決められた発言回数が終わった時点で残っていた人を出力するプログラムを作成することにしました。

  プレイヤー数、ゲーム中に発言された回数、それぞれの発言を入力とし、入力が終わった時点で残っているプレイヤーの番号を小さい順に出力するプログラムを作成してください。ただし、プレイヤーには 1 から番号が割り振られており、発言順番も 1 番目のプレイヤーから順に行い、一通り発言が終わると、再度 1 番目のプレイヤーから発言することとします。順番の回ってきたプレイヤーが既に脱落している場合は、その次のプレイヤーが発言します。また、このプログラムは、プレイヤーが一人になった時点で、その後の発言を無視しなければなりません。

Input

複数のデータセットの並びが入力として与えられます。入力の終わりはゼロふたつの行で示されます。
各データセットは以下の形式で与えられます。

m n
s1
s2
:
sn

1 行目にプレイヤー数 m (2 ≤ m ≤ 1000) と発言回数 n (1 ≤ n ≤ 10000) が与えられます。

続く n 行に i 番目の発言 s1 が与えられます。 si は整数、Fizz、Buzz、または FizzBuzz を示す文字列（8文字以下）です。

n 行に i 番目の発言 s1 が与えられます。 si は数字、Fizz、Buzz、FizzBuzz、あるいは間違った発言を示すその他の文字列 (20 文字以下) です。

-->

データセットの数は 50 を超えません。

Output

入力データセットごとに、指定された発言回数まで入力されたときに残っているプレイヤーの番号を小さい順に出力します。

Sample Input

5 7
1
2
Fizz
4
Buzz
6
7
3 5
1
2
3
4
5
0 0

Output for the Sample Input

2 3 4 5
1



Sample input:
5 7
1
2
Fizz
4
Buzz
6
7
3 5
1
2
3
4
5
0 0


Sample output:
2 3 4 5
1

Submission status: Wrong Answer

Solution code:
while(True):
    m,n = map(int,input().split())
    if not m: break
    a = [True]*m
    j = -1
    for i in range(1,n+1):
        if not sum(a): input(); continue
        j = (j+1+((a+a)[j+1:]).index(True))%m
        s = input()
        if not i%15:
            if s == "FizzBuzz": continue
            else: a[j] = False; continue
        if not i%5:
            if s == "Buzz": continue
            else: a[j] = False; continue
        if not i%3:
            if s == "Fizz": continue
            else: a[j] = False; continue
        if s != str(i): print("aaa"); a[j] = False;continue
    print(" ".join(str(e+1) for e in range(m) if a[e]))

Submission status: Wrong Answer

Solution code:
while(True):
    m,n = map(int,input().split())
    if not m: break
    a = [True]*m
    j = -1
    for i in range(1,n+1):
        if not sum(a): input(); continue
        j = (j+1+((a+a)[j+1:]).index(True))%m
        s = input()
        if not i%15:
            if s == "FizzBuzz": continue
            else: a[j] = False; continue
        if not i%5:
            if s == "Buzz": continue
            else: a[j] = False; continue
        if not i%3:
            if s == "Fizz": continue
            else: a[j] = False; continue
        if s != str(i): a[j] = False;continue
    print(" ".join(str(e+1) for e in range(m) if a[e]))

Submission status: Wrong Answer

Solution code:
while(True):
    m,n = map(int,input().split())
    if not m: break
    a = list(range(1,m+1))
    b = [input() for _ in range(n)]
    t = [str(i+1) for i in range(n)]
    t[2::3]   = ["Fizz"]*len(t[2::3])
    t[4::5]   = ["Buzz"]*len(t[4::5])
    t[14::15] = ["FizzBuzz"]*len(t[14::15])
    i=0
    for j in range(n):
        if len(a) <2: break
        if b[j] != t[j]: del a[i]; i = i if i > len(a) else 0; continue
        i += 1
        i = i%len(a)
    print(" ".join(str(e) for e in a))

Submission status: Wrong Answer

Solution code:
def fizz_buzz():
    c = 1
    while True:
        res = ''
        if c % 3 == 0:
            res = res + 'Fizz'
        if c % 5 == 0:
            res = res + 'Buzz'
        if res == '':
            yield str(c)
        else:
            yield res
        c += 1

Submission status: Wrong Answer

Solution code:
def fizzbuzz(i):
    if i % 15 == 0:
        return 'FizzBuzz'
    elif i % 5 == 0:
        return 'Buzz'
    elif i % 3 == 0:
        return 'Fizz'
    else:
        return str(i)

import sys
f = sys.stdin

while True:
    m, n = map(int, f.readline().split())
    if m == n == 0:
        break
    member = set(range(1, m + 1))
            
    for i in range(n):
        si = f.readline().strip()
        if si != fizzbuzz(i + 1):
            member -= set([i % m + 1])
        
    print(*member)

Submission status: Wrong Answer

Solution code:
def fizzbuzz(i):
    if i % 15 == 0:
        return 'FizzBuzz'
    elif i % 5 == 0:
        return 'Buzz'
    elif i % 3 == 0:
        return 'Fizz'
    else:
        return str(i)

import sys
f = sys.stdin

while True:
    m, n = map(int, f.readline().split())
    if m == n == 0:
        break
    member = set(range(1, m + 1))
            
    for i in range(n):
        si = f.readline().strip()
        if si != fizzbuzz(i + 1):
            member -= set([i % m + 1])
            if len(member) == 1:
                break

        
    print(*member)

Submission status: Wrong Answer

Solution code:
def fizzbuzz(i):
    if i % 15 == 0:
        return 'FizzBuzz'
    elif i % 5 == 0:
        return 'Buzz'
    elif i % 3 == 0:
        return 'Fizz'
    else:
        return str(i)

import sys
f = sys.stdin

while True:
    m, n = map(int, f.readline().split())
    if m == n == 0:
        break
    member = set(range(1, m + 1))
    s = [f.readline().strip() for _ in range(n)]
    for i in range(n):
        if s[i] != fizzbuzz(i + 1):
            member -= set([i % m + 1])
            if len(member) == 1:
                break
        
    print(*sorted(list(member)))

Submission status: Wrong Answer

Solution code:
def fizzbuzz(i):
    if i % 15 == 0:
        return 'FizzBuzz'
    elif i % 5 == 0:
        return 'Buzz'
    elif i % 3 == 0:
        return 'Fizz'
    else:
        return str(i)

import sys
f = sys.stdin

while True:
    m, n = map(int, f.readline().split())
    if m == n == 0:
        break
    member = list(range(1, m + 1))
    s = [f.readline().strip() for _ in range(n)]
    offset = 0
    for i in range(n):
        print(s[i]==fizzbuzz(i+1),offset,m)
        if s[i] != fizzbuzz(i + 1):
            pos = (i - offset) % m
            member.remove(member[pos])
            offset = i - pos + 1
            m -= 1
            if len(member) == 1:
                break
        
    print(*member)

Submission status: Wrong Answer

Solution code:
while(1):
    [m,n]=[int(x) for x in raw_input().split()]
    if m==0:
        break
    else:
        truenum=0
        player=[x for x in range(1,m+1)]
        for i in range(n):
            shout=raw_input()
            pnow=player.pop(0)
            truenum=truenum+1
            if truenum%15==0:
                if shout=="FizzBuzz":
                    player.append(pnow)
            elif truenum%5==0:
                if shout=="Buzz":
                    player.append(pnow)
            elif truenum%3==0:
                if shout=="Fizz":
                    player.append(pnow)
            else:
                if shout==str(truenum):
                    player.append(pnow)
        print " ".join([str(x) for x in sorted(player)])

Submission status: Wrong Answer

Solution code:
while True:
    M, N = map(int, raw_input().split())
    if M == N == 0: break
    m = [i for i in range(1, M+1)]
    index = 0
    num = 1
    for i in range(N):
        line = raw_input()
        if num % 15 == 0:
            if line != 'FizzBuzz':
                del m[index]
        elif num % 3 == 0:
            if line != 'Fizz':
                del m[index]
        elif num % 5 == 0:
            if line != 'Buzz':
                del m[index]
        else:
            if not line.isdigit():
                del m[index]
            else:
                if int(line) != num:
                    del m[index]
        if index+1 >= len(m):
            index = 0
        else:
            index += 1
        num += 1
    else:
        print ' '.join(map(str, m))

Submission status: Wrong Answer

Solution code:
while True:
    M, N = map(int, raw_input().split())
    if M == N == 0: break
    m = [i for i in range(1, M+1)]
    index = 0
    num = 1
    for i in range(N):
        if len(m) != 1:
            line = raw_input()
            if num % 15 == 0:
                if line != 'FizzBuzz':
                    del m[index]
            elif num % 3 == 0:
                if line != 'Fizz':
                    del m[index]
            elif num % 5 == 0:
                if line != 'Buzz':
                    del m[index]
            else:
                if not line.isdigit():
                    del m[index]
                else:
                    if int(line) != num:
                        del m[index]
            if index+1 >= len(m):
                index = 0
            else:
                index += 1
            num += 1
        else:
            raw_input()
    else:
        print ' '.join(map(str, m))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0221

"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline


def fb_gen(count=1):
    while True:
        if count % 15 == 0:
            ans = 'FizzBuzz'
        elif count % 5 == 0:
            ans = 'Buzz'
        elif count % 3 == 0:
            ans = 'Fizz'
        else:
            ans = str(count)
        yield ans
        count += 1


def main(args):
    while True:
        m, n = map(int, input().split())
        if m == 0 and n == 0:
            break
        players = deque(range(1, m+1))
        fb = fb_gen()
        for _ in range(n):
            p = input().strip()
            # print('player {}: {}'.format(players[0], p))
            if p != fb.__next__():
                # drop = players.popleft()
                pass
                # print('player {} dropped'.format(drop))
                if len(players) == 1:
                    break
            else:
                players.rotate(-1)

        result = list(players)
        result.sort()
        print(*result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
def fizzbuzz(n):
	if n%15==0: return "FizzBuzz"
	if n%5==0: return "Buzz"
	if n%3==0: return "Fizz"
	return str(n)

while 1:
	m,n=map(int,raw_input().split())
	if m==0:break
	player=[i+1 for i in range(m)]
	for i in range(n):
		if fizzbuzz(i+1)!=raw_input():
			player.pop((i-(m-len(player)))%len(player))
	print " ".join(map(str,player))

Submission status: Wrong Answer

Solution code:
def fizzbuzz(n):
	if n%15==0: return "FizzBuzz"
	if n%5==0: return "Buzz"
	if n%3==0: return "Fizz"
	return str(n)

while 1:
	try:
		m,n=map(int,raw_input().split())
		if m==0:break
		player=[i+1 for i in range(m)]
		for i in range(n):
			if fizzbuzz(i+1)!=raw_input():
				player.pop((i-(m-len(player)))%len(player))
				if len(player)==1:
					break
		print " ".join(map(str,player))
	except:
		pass

Submission status: Time Limit Exceeded

Solution code:
while True:
    m,n=map(int,raw_input().split())
    if m==n==0:break
    L=range(1,m+1)
    j=0
    for i in range(1,n+1):
        c=raw_input()
        if i%15==0:
            if c!="FizzBuzz":
                L[j]=0
        elif i%5==0:
            if c!="Buzz":
		L[j]=0
	elif i%3==0:
            if c!="Fizz":
		L[j]=0
        elif c<>str(i):
            L[j]=0
        j+=1
        while L[j%m]==0:
            j=(j+1)%m
        j%=m
    print " ".join([str(i) for i in L if i!=0])

Submission status: Time Limit Exceeded

Solution code:
while True:
    m,n=map(int,raw_input().split())
    if m==n==0:break
    L=range(1,m+1)
    j=0
    for i in range(1,n+1):
        c=raw_input()
	if len(L)==1:break
        if i%15==0:
            if c!="FizzBuzz":
                L[j]=0
        elif i%5==0:
            if c!="Buzz":
		L[j]=0
	elif i%3==0:
            if c!="Fizz":
                L[j]=0
        elif c<>str(i):
            L[j]=0
        j+=1
        while L[j%m]==0:
            j=(j+1)%m
        j%=m
    print " ".join([str(i) for i in L if i!=0])

Submission status: Time Limit Exceeded

Solution code:
while 1:
        n = map(int,raw_input().split())
        if n[0] == n[1] == 0:
                break
        m, o = [1 for x in range(n[0])], 0

        for x in range(1,n[1]+1):
                a = raw_input()
                print o
                if sum(m) == 1:
                        pass
                elif x % 15 == 0:
                        if a != "FizzBuzz":
                                m[o] = 0
                elif x % 3 == 0:
                        if a != "Fizz":
                                m[o] = 0
                elif x % 5 == 0:
                        if a != "Buzz":
                                m[o] = 0
                else:
                        if a != str(x):
                                m[o] = 0
                while 1:
                        o += 1
                        if o == len(m):
                                o = -1
                        elif m[o] == 1:
                                break

        for x in range(n[0]):
                if m[x] == 1:
                        print x+1,
        print ""

Submission status: Accepted

Solution code:
while(True):
    m,n = map(int,input().split())
    if not m: break
    a = [True]*m
    j = -1
    for i in range(1,n+1):
        if sum(a) < 2: input(); continue
        j = (j+1+((a+a)[j+1:]).index(True))%m
        s = input()
        if not i%15:
            if s == "FizzBuzz": continue
            else: a[j] = False; continue
        if not i%5:
            if s == "Buzz": continue
            else: a[j] = False; continue
        if not i%3:
            if s == "Fizz": continue
            else: a[j] = False; continue
        if s != str(i): a[j] = False;continue
    print(" ".join(str(e+1) for e in range(m) if a[e]))

Submission status: Accepted

Solution code:
while(True):
    m,n = map(int,input().split())
    if not m: break
    a = list(range(1,m+1))
    b = [input() for _ in range(n)]
    t = [str(i+1) for i in range(n)]
    t[2::3]   = ["Fizz"]*len(t[2::3])
    t[4::5]   = ["Buzz"]*len(t[4::5])
    t[14::15] = ["FizzBuzz"]*len(t[14::15])
    i=0
    for j in range(n):
        if len(a) <2: break
        if b[j] != t[j]: del a[i]; i = i%len(a); continue
        i += 1
        i = i%len(a)
    print(" ".join(str(e) for e in a))

Submission status: Accepted

Solution code:
while(True):
    m,n = map(int,input().split())
    if not m: break
    a = list(range(1,m+1))
    b = [input() for _ in range(n)]
    t = [str(i+1) for i in range(n)]
    t[2::3]   = ["Fizz"]*len(t[2::3])
    t[4::5]   = ["Buzz"]*len(t[4::5])
    t[14::15] = ["FizzBuzz"]*len(t[14::15])
    i=0
    for j in range(n):
        if len(a) <2: break
        if b[j] != t[j]: del a[i]; i = 0 if i >= len(a) else i; continue
        i += 1
        i = i%len(a)
    print(" ".join(str(e) for e in a))

Submission status: Accepted

Solution code:
def iter( begin, end ):
    x = begin
    while x < end:
        yield x
        x += 1
    raise StopIteration

def valid( val , txt ):
    if val%15 == 0: return txt == 'FizzBuzz'
    if val%3 == 0: return txt == 'Fizz'
    if val%5 == 0: return txt == 'Buzz'
    return str(val) == txt
    
while True:
    m, n = map(int, raw_input().split())
    if m+n == 0: break
    player = [ x for x in iter(1, m+1) ]
    ind = 0
    for a in iter(1, n+1):
        txt = raw_input()
        if len(player) == 1: continue
        if valid( a, txt ):
            ind = (ind+1)%len(player)
        else:
            player.pop(ind)
            ind = ind%len(player)
    for p in player[:-1]:
        print p,
    print player[-1]

Submission status: Accepted

Solution code:
def fizz_buzz():
    c = 1
    while True:
        res = ''
        if c % 3 == 0:
            res = res + 'Fizz'
        if c % 5 == 0:
            res = res + 'Buzz'
        if res == '':
            yield str(c)
        else:
            yield res
        c += 1

while True:
    m,n = map(int,input().split())
    if m == 0: break

    player = list(range(m))

    p = 0
    fb = fizz_buzz()
    for i in range(n):
        inp = input()
        if len(player) > 1:
            if  inp != next(fb):
                del player[p] 
                p = p % len(player)
            else:
                p = (p+1) % len(player)

    result = str(player[0]+1)
    if len(player) > 1:
        for pi in player[1:]:
            result += ' ' + str(pi+1)

    print(result)

Submission status: Accepted

Solution code:
def fizzbuzz(i):
    if i % 15 == 0:
        return 'FizzBuzz'
    elif i % 5 == 0:
        return 'Buzz'
    elif i % 3 == 0:
        return 'Fizz'
    else:
        return str(i)

import sys
f = sys.stdin


while True:
    m, n = map(int, f.readline().split())
    if m == n == 0:
        break
    member = list(range(1, m + 1))
    s = [f.readline().strip() for _ in range(n)]

    pos = 0
    for i in range(n):
        if s[i] != fizzbuzz(i + 1):
            del member[pos]
            m = len(member)
            if m == 1:
                break
        else:
            pos += 1
        pos %= m
        
    print(*member)

Submission status: Accepted

Solution code:
while(1):
    [m,n]=[int(x) for x in raw_input().split()]
    if m==0:
        break
    else:
        truenum=0
        player=[x for x in range(1,m+1)]
        for i in range(n):
            shout=raw_input()
            if len(player)>1:
                pnow=player.pop(0)
                truenum=truenum+1
                if truenum%15==0:
                    if shout=="FizzBuzz":
                        player.append(pnow)
                elif truenum%5==0:
                    if shout=="Buzz":
                        player.append(pnow)
                elif truenum%3==0:
                    if shout=="Fizz":
                        player.append(pnow)
                else:
                    if shout==str(truenum):
                        player.append(pnow)
        print " ".join([str(x) for x in sorted(player)])

Submission status: Accepted

Solution code:
while True:
    M, N = map(int, raw_input().split())
    if M == N == 0: break
    m = [i for i in range(1, M+1)]
    index = 0
    num = 1
    for i in range(N):
        if len(m) != 1:
            line = raw_input()
            flag = False
            if num % 15 == 0:
                if line != 'FizzBuzz':
                    del m[index]
                    flag = True
            elif num % 3 == 0:
                if line != 'Fizz':
                    del m[index]
                    flag = True
            elif num % 5 == 0:
                if line != 'Buzz':
                    del m[index]
                    flag = True
            else:
                if not line.isdigit():
                    del m[index]
                    flag = True
                else:
                    if int(line) != num:
                        del m[index]
                        flag = True
            if not flag:
                index += 1
            if index+1 > len(m):
                index = 0
            num += 1
        else:
            raw_input()
    else:
        print ' '.join(map(str, m))

Submission status: Accepted

Solution code:
while 1:
    m,n=map(int,input().split())
    if m==0:break
    a,i,c=list(range(1,m+1)),0,0
    while i<n:
        i+=1
        b,f=input(),0
        if m<2:continue
        if i%15==0:
            if b!='FizzBuzz':
                del a[c]
                f=1
        elif i%5==0:
            if b!='Buzz':
                del a[c]
                f=1
        elif i%3==0:
            if b!='Fizz':
                del a[c]
                f=1
        elif b!=str(i):
            del a[c]
            f=1
        if f:m-=1
        else:c+=1
        c%=m
    print(*a)

Submission status: Accepted

Solution code:
hoge = ["FizzBuzz" if i % 15 == 0 else("Fizz" if i % 3 == 0 else("Buzz" if i % 5 == 0 else str(i))) for i in range(10001)]

while True:
    m, n = map(int, input().split())
    if m == 0:
        break
    man = list(range(1, m+1))
    game = [input() for _ in range(n)]
    idx = 0
    for i in range(n):
        if game[i] != hoge[i+1]:
            del man[idx]
            if len(man) == 1:
                break
        else:
            idx += 1
        idx %= len(man)
    print(*man)

Submission status: Accepted

Solution code:
# Aizu Problem 0221: Fizz Buzz

import sys, math, os, struct

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def valid(k, a):
    if k % 15 == 0: 
        return a == "FizzBuzz"
    elif k % 3 == 0:
        return a == "Fizz"
    elif k % 5 == 0:
        return a == "Buzz"
    else:
        return a == str(k)

    
def fizz_buzz(m, n, A):
    players = list(range(1, m + 1))
    p = 0
    k = 0
    while len(A) > 0:
        k += 1
        a = A.pop(0)
        if valid(k, a):
            p = (p + 1) % m
        else:
            if len(players) == 0:
                print()
                return
            players.pop(p)
            if len(players) == 1:
                break
            m -= 1
            p %= m
    print(' '.join([str(p) for p in players]))
    
    
while True:
    m, n = [int(_) for _ in input().split()]
    if m == 0:
        break
    A = [input().strip() for _ in range(n)]
    fizz_buzz(m, n, A)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0221

"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline


def fb_gen(count=1):
    while True:
        if count % 15 == 0:
            ans = 'FizzBuzz'
        elif count % 5 == 0:
            ans = 'Buzz'
        elif count % 3 == 0:
            ans = 'Fizz'
        else:
            ans = str(count)
        yield ans
        count += 1


def main(args):
    while True:
        m, n = map(int, input().split())
        if m == 0 and n == 0:
            break
        players = deque(range(1, m+1))
        fb = fb_gen()
        for _ in range(n):
            p = input().strip()
            if p != fb.__next__():
                if len(players) > 1:
                    players.popleft()
            else:
                players.rotate(-1)

        result = list(players)
        result.sort()
        print(*result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0221

"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline


def fb_gen(count=1):
    while True:
        if count % 15 == 0:
            ans = 'FizzBuzz'
        elif count % 5 == 0:
            ans = 'Buzz'
        elif count % 3 == 0:
            ans = 'Fizz'
        else:
            ans = str(count)
        yield ans
        count += 1


def main(args):
    while True:
        m, n = map(int, input().split())
        if m == 0 and n == 0:
            break
        players = deque(range(1, m+1))
        fb = fb_gen()
        ok_count = 0
        for _ in range(n):
            p = input().strip()
            if p != fb.__next__():
                if len(players) > 1:
                    players.rotate(ok_count)
                    players.popleft()
                    ok_count = 0
            else:
                ok_count -= 1

        result = list(players)
        result.sort()
        print(*result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
while True:
  m, n = map(int, input().split())
  if m == 0:
    break

  plst = [i for i in range(1, m + 1)]
  length = m
  ind = 0
  for i in range(1, n + 1):
    s = input()
    if length == 1:
      continue
    if i % 15 == 0:
      if s != "FizzBuzz":
        plst.pop(ind)
        length -= 1
        ind %= length
      else:
        ind += 1
        ind %= length
    elif i % 5 == 0:
      if s != "Buzz":
        plst.pop(ind)
        length -= 1
        ind %= length
      else:
        ind += 1
        ind %= length
    elif i % 3 == 0:
      if s != "Fizz":
        plst.pop(ind)
        length -= 1
        ind %= length
      else:
        ind += 1
        ind %= length
    else:
      if s != str(i):
        plst.pop(ind)
        length -= 1
        ind %= length
      else:
        ind += 1
        ind %= length
  print(*plst)

Submission status: Accepted

Solution code:
def Judge(i):
    if i%15==0: return "FizzBuzz"
    if i%5==0:  return "Buzz"
    if i%3==0:  return "Fizz"
    return str(i)

while True:
    m,n=map(int,raw_input().split())
    if m==n==0:break
    L=range(1,m+1)
    j=0
    for i in range(1,n+1):
        c=raw_input()
	if len(L)>1:
            if Judge(i)!=c:
                del L[j]
                j%=len(L)
            else:
                j=(j+1)%len(L)
    print " ".join(map(str,L))

Submission status: Accepted

Solution code:
while 1:
        n = map(int,raw_input().split())
        if n[0] == n[1] == 0:
                break
        m, o = [1 for x in range(n[0])], 0

        for x in range(1,n[1]+1):
                a = raw_input()
                if sum(m) == 1:
                        pass
                elif x % 15 == 0:
                        if a != "FizzBuzz":
                                m[o] = 0
                elif x % 3 == 0:
                        if a != "Fizz":
                                m[o] = 0
                elif x % 5 == 0:
                        if a != "Buzz":
                                m[o] = 0
                else:
                        if a != str(x):
                                m[o] = 0
                while 1:
                        o += 1
                        if sum(m) == 1:
                                break
                        elif o == len(m):
                                o = -1
                        elif m[o] == 1:
                                break

        OutPut = ""
        for x in range(n[0]):
                if m[x] == 1:
                        OutPut += str(x+1) + " "
        print OutPut[:-1]

Submission status: Accepted

Solution code:
def fizzbuzz(n):
	if n%15==0: return "FizzBuzz"
	if n%5==0: return "Buzz"
	if n%3==0: return "Fizz"
	return str(n)

while 1:
	try:
		m,n=map(int,raw_input().split())
		if m==0:break
		player=range(1,m+1)
		p=0
		for i in range(n):
			if len(player)==1:
				break
			if fizzbuzz(i+1)!=raw_input():
				del player[p]
				p%=len(player)
			else:
				p=(p+1)%len(player)
		print " ".join(map(str,player))
	except:
		pass

Submission status: Accepted

Solution code:
def fizzbuzz(n):
	if n%15==0: return "FizzBuzz"
	if n%5==0: return "Buzz"
	if n%3==0: return "Fizz"
	return str(n)
while 1:
	try:
		m,n=map(int,raw_input().split())
		if m==0:break
		player=range(1,m+1)
		p=0
		for i in range(n):
			if fizzbuzz(i+1)!=raw_input():
				del player[p]
				if len(player)==1:
					break
				p%=len(player)
			else:
				p=(p+1)%len(player)
		print " ".join(map(str,player))
	except:
		pass

Submission status: Accepted

Solution code:
def fizzbuzz(n):
	if n%15==0: return "FizzBuzz"
	if n%3==0: return "Fizz"
	if n%5==0: return "Buzz"
	return str(n)
while 1:
	try:
		m,n=map(int,raw_input().split())
		if m==0:break
		player=range(1,m+1)
		p=0
		for i in range(n):
			if fizzbuzz(i+1)!=raw_input():
				del player[p]
				m=len(player)
				if m==1:
					break
				p%=m
			else:
				p=(p+1)%m
		print " ".join(map(str,player))
	except:
		pass

Submission status: Accepted

Solution code:
def FB(n):
    if n%3==0 and n%5==0: return 'FizzBuzz'
    if n%3==0: return 'Fizz'
    if n%5==0: return 'Buzz'
    return str(n)

while True:
    M,N=map(int,input().split())
    if M==0 and N==0: break
    TF=[1]*M
    S=[input() for _ in range(N)]
    k=0
    for i,s in enumerate(S):
        if sum(TF)==1: break
        while True:
            if not TF[k]:
                k=(k+1)%M
            else:
                if FB(i+1)==s:
                    k=(k+1)%M
                    break
                else:
                    TF[k]=0
                    k=(k+1)%M
                    break
    ans=[]
    for j,p in enumerate(TF):
        if p:
            ans.append(str(j+1))
    print(' '.join(ans))

Submission status: Accepted

Solution code:
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
import copy
from test.support import _MemoryWatchdog

BIG_NUM = 2000000000
HUGE_NUM = 99999999999999999
MOD = 1000000007
EPS = 0.000000001
sys.setrecursionlimit(100000)


while True:

    num_player,num_word = map(int,input().split())
    if num_player == 0 and num_word == 0:
        break

    Q = deque()
    table = [False]*(num_player+1)

    for i in range(1,num_player+1):
        Q.append(i)

    for i in range(1,num_word+1):

        tmp_str = input()
        if len(Q) == 1:
            continue

        player = Q.popleft()

        if i%15 == 0:
            if tmp_str == "FizzBuzz":
                Q.append(player)
        elif i%3 == 0:
            if tmp_str == "Fizz":
                Q.append(player)
        elif i%5 == 0:
            if tmp_str == "Buzz":
                Q.append(player)
        else:
            if tmp_str == "FizzBuzz" or tmp_str == "Fizz" or tmp_str == "Buzz":
                pass
            else:
                if int(tmp_str) == i:
                    Q.append(player)

    while len(Q) > 0:
        table[Q.popleft()] = True

    First = True
    for i in range(1,num_player+1):
        if table[i] == True:
            if First == True:
                print("%d"%(i),end="")
                First = False
            else:
                print(" %d"%(i),end="")
    print()

Submission status: Accepted

Solution code:
def solve():
    from itertools import cycle
    from sys import stdin
    f_i = stdin
    m_i = map(lambda x: x.rstrip(), f_i)
    
    while True:
        m, n = map(int, next(m_i).split())
        if m == 0:
            break
        
        player = list(range(1, m + 1))
        ans = ('FizzBuzz' if i % 15 == 0 else 'Fizz' if i % 3 == 0 else
               'Buzz' if i % 5 == 0 else str(i) for i in range(1, n + 1))
        
        while len(player) > 1:
            for p, a in zip(cycle(player), ans):
                n -= 1
                if next(m_i) != a:
                    break
            else:
                break
            idx = player.index(p)
            player = player[idx+1:] + player[:idx]
        while n:
            next(m_i)
            n -= 1
        player.sort()
        print(*player)

solve()

Submission status: Accepted

Solution code:
it = iter(open(0).read().split())
def reads(v):
    r = []
    for i in range(v):
        r.append(next(it).strip())
    return r
def read():
    return next(it).strip()


while 1:
    M, N = map(int, reads(2))
    if M == N == 0:
        break
    S = [1]*M; rest = M
    k = 0
    for i in range(1, N+1):
        s = read()
        if rest == 1:
            continue
        ok = 1
        if i % 15 == 0:
            if s != "FizzBuzz":
                ok = 0
        elif i % 3 == 0:
            if s != "Fizz":
                ok = 0
        elif i % 5 == 0:
            if s != "Buzz":
                ok = 0
        else:
            if s != str(i):
                ok = 0
        if not ok:
            S[k] = 0
            rest -= 1
        k = (k + 1) % M
        while not S[k]:
            k = (k + 1) % M
    print(*[i+1 for i in range(M) if S[i]])