Problem:


Problem A: And Then There Was One

Let’s play a stone removing game.

Initially, n stones are arranged on a circle and numbered 1, ... , n clockwise (Figure 1). You are also given two numbers k and m. From this state, remove stones one by one following the rules explained below, until only one remains. In step 1, remove stone m. In step 2, locate the k-th next stone clockwise from m and remove it. In subsequent steps, start from the slot of the stone removed in the last step, make k hops clockwise on the remaining stones and remove the one you reach. In other words, skip (k - 1) remaining stones clockwise and remove the next one. Repeat this until only one stone is left and answer its number.

For example, the answer for the case n = 8, k = 5, m = 3 is 1, as shown in Figure 1.

Figure 1: An example game

Initial state: Eight stones are arranged on a circle.

Step 1: Stone 3 is removed since m = 3.

Step 2: You start from the slot that was occupied by stone 3. You skip four stones 4, 5, 6 and 7 (since k = 5), and remove the next one, which is 8.

Step 3: You skip stones 1, 2, 4 and 5, and thus remove 6. Note that you only count stones that are still on the circle and ignore those already removed. Stone 3 is ignored in this case.

Steps 4-7: You continue until only one stone is left. Notice that in later steps when only a few stones remain, the same stone may be skipped multiple times. For example, stones 1 and 4 are skipped twice in step 7.

Final State: Finally, only one stone, 1, is on the circle. This is the final state, so the answer is 1.

Input

The input consists of multiple datasets each of which is formatted as follows.

n k m

The last dataset is followed by a line containing three zeros. Numbers in a line are separated by a single space. A dataset satisfies the following conditions.

                         2 ≤ n ≤ 10000,    1 ≤ k ≤ 10000,    1 ≤ m ≤ n

The number of datasets is less than 100.

Output

For each dataset, output a line containing the stone number left in the final state. No extra characters such as spaces should appear in the output.

Sample Input

8 5 3
100 9999 98
10000 10000 10000
0 0 0

Output for the Sample Input

1
93
2019



Sample input:
8 5 3
100 9999 98
10000 10000 10000
0 0 0


Sample output:
1
93
2019

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

import sys
import math
import itertools as it
from collections import deque

sys.setrecursionlimit(10000000)

while True:
    n, k, m = map(int, raw_input().split())
    if n == 0:
        break
    
    pos = 0
    for num in range(1, n):
        pos += k - 1
        pos %= num
        pos += 1
    print (m - (n - pos)) % n

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

import sys
import math
import itertools as it
from collections import deque

sys.setrecursionlimit(10000000)

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

    pos = 0
    for num in range(1, n):
        pos += k
        pos %= num
    ans = (m + pos) % n + 1

Submission status: Accepted

Solution code:
#!/usr/bin/env python

import sys
import math
import itertools as it
from collections import deque

sys.setrecursionlimit(10000000)

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

    pos = 0
    for num in range(1, n):
        pos += k - 1
        pos %= num
        pos += 1
    ans = (m - (n - pos)) % n
    
    if ans == 0:
        print n
    else:
        print ans

Submission status: Accepted

Solution code:
while 1:
    n,k,m=map(int,input().split())
    if not n and not k and not m:break
    l=list(range(1,n+1))
    p=l.index(m)
    while len(l)>1:
        l.pop(p)
        p=(p+k-1)%len(l)
    print(l[0])

Submission status: Accepted

Solution code:
while True:
	n,k,m=[int(e) for e in raw_input().split()]
	if n==0:break
	r=0
	for i in range(1,n):r=(r+k)%i
	print((r+m)%n+1)

Submission status: Accepted

Solution code:
import sys
if sys.version_info[0]>=3: raw_input=input
while True:
	n,k,m=[int(e) for e in raw_input().split()]
	if n==0:break
	r=0
	for i in range(1,n):r=(r+k)%i
	print((r+m)%n+1)

Submission status: Accepted

Solution code:
while 1:	
	n,k,m = map(int,raw_input().split())
	if (n, k, m) == (0,0,0):
		break
	stone = range(n+1)
	here = m
	stone.pop(here)
	rest = n - 1
	while rest >= 2:
		if here - 1 + k <= rest:
			tmp = here - 1 + k
		else:
			rem = (k - (rest-here+1)) % rest
			tmp = rest if rem == 0 else rem
		stone.pop(tmp)
		rest -= 1
		here = 1 if tmp > rest else tmp
	print stone[1]

Submission status: Accepted

Solution code:
while 1:
    n,k,m = map(int,raw_input().split())
    if n == 0: break
    stone = range(1,n+1)
    p = m-1
    while len(stone) > 1:
        stone.pop(p)
        p = (p+k-1)%len(stone)
    print stone[0]

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

while True:
    n,k,m = map(int,input().split())
    if (n,k,m) == (0,0,0):
        break
    stones = [i + 1 for i in range(n)] 
    pointer = m - 1
    stones_length = len(stones)
    while stones_length > 1:
        stones.pop(pointer)
        stones_length -= 1
        pointer = (pointer - 1 + k) % stones_length
    print(stones[0])

Submission status: Accepted

Solution code:
#! /usr/bin/python
# -*- coding: utf-8 -*-

(n, k, m) = map(int, raw_input().split())
while n != 0:
    clk = [i for i in range(1, n+1)]
    p = m-1
    clk.pop(m-1)
    num = n-1
    for i in range(n-2):
        p = (p-1+k)%num
        clk.pop(p)
        num -= 1
    print clk[0]

    (n, k, m) = map(int, raw_input().split())

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-/
import sys
def main():
    for line in sys.stdin:
        n, k, m = map(int, line.split()) # ??\???
        if not(n or k or m): # 0 0 0??§??????
            break
        stone = list(range(1, n + 1)) # 1 ~ n????????????????????§??¨???
        m -= 1 # ????????????????????¨????????????????????¬?????????
        while len(stone) > 1: # ??????1?????????????????§
            stone.pop(m) # ?????????????????????m???????´??????????
            m = (m + k - 1) % len(stone) # ?¬??????????????????????????±????
        print(stone[0]) # ?????£??????????????????

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
while True:
    N,K,M = map(int,input().split())
    if N == 0: break
    li = list(range(1,N+1))
    i = M-1
    while len(li) > 1:
        li.pop(i)
        i = (i+K-1) % len(li)
    print(li[0])

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        n,k,m = LI()
        if n == 0:
            break
        a = list(range(1,n+1))
        t = m - 1
        for i in range(n-1):
            a = a[:t] + a[t+1:]
            t = (t - 1 + k) % len(a)
        rr.append(a[0])

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
import sys

while True:
    n, k, m = map(int, sys.stdin.readline().split())
    
    if n == 0:
        break
    
    stone = range(1, n + 1)
    stone.pop(m - 1)
    cur = m - 2
    for i in xrange(n - 2):
        cur = (cur + k) % len(stone)
        stone.pop(cur)
        cur -= 1
    print stone[0]

Submission status: Accepted

Solution code:
while True:
	n, k, m = map(int, raw_input().split())
	if n == k == m == 0: break
	stones = range(1, n + 1)
	m -= 1
	k -= 1
	while len(stones) != 1:
		stones.pop(m)
		m = (m + k) % len(stones)
	print stones[0]

Submission status: Accepted

Solution code:
while True:
    n,k,m=map(int,raw_input().split())
    if n==k==m==0:break
    L=range(1,n+1)
    del	L[m-1]
    i=(m-2+k)%len(L)
    while len(L)!=1:
	del L[i]
        i=(i+k-1)%len(L)
    print L[0]

Submission status: Accepted

Solution code:
while True :
    n, k, m = map(int, input().split())
    if n == 0 and k == 0 and m == 0 :
        break
    
    stone = []
    for i in range(n) :
        stone.append(i+1)
        
    i = m-1
    while True :
        del stone[i]
        if len(stone) == 1 :
            break
        i += k-1
        if i >= len(stone) :
            i = i % len(stone)
    print(stone[0])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Problems 1275
2007年 アジア地区予選 東京大会 問題A And Then There Was One
"""

while True:
    n, k, m = map(int, input().split())
    if n == 0 and k == 0 and m == 0:
        break
    list = [int(i) for i in range(1,n+1)]
    m -= 1
        
    while True:
        if 1 < n:
            list.pop(m)
            n -= 1
            m = (m+k-1) % n
        else:
            print(list[0])
            break

Submission status: Accepted

Solution code:
"""
Problem A: 次期町長
https://onlinejudge.u-aizu.ac.jp/problems/1159
2≤ N ≤10000、1≤ K ≤10000、1≤ M ≤ Nß
"""
def solve(n, k, m):

    table = [i+1 for i in range(n)]
    taget = m-1
    while(len(table)>1):

        table.pop(taget)
        taget -= 1
        taget = (taget+k)%len(table)

    return table[0]

if __name__ == '__main__':
    ans = []
    while(True):
        n, k, m = map(int, input().split())
        if n == 0 and k == 0 and m == 0:
            break
        ans.append(solve(n, k, m))
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
ans = []
while True:
    n, k, m = map(int, input().split())
    if n == k == m == 0:
        break
    stones = [i + 1 for i in range(n)]
    m -= 1
    while n > 1:
        stones.pop(m)
        n -= 1
        m = ((m + k) - 1) % n
    ans.append(stones[0])

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
ans = []
while True:
    N, K, M = map(int, input().split())
    if not N and not K and not M:
        break
    
    remind = [i for i in range(N)]
    
    now = M - 1
    while len(remind) > 1:
        remind.pop(now)
        now = (now + K - 1) % len(remind)
    
    ans.append(remind[0])
                     
[print(i + 1) for i in 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:

    N,K,M = map(int,input().split())
    if N == 0 and K == 0 and M == 0:
        break

    table = [i for i in range(1,N+1)]

    loc = M-1

    while len(table) > 1:
        table.pop(loc)
        loc = (loc+K-1)%(len(table))

    print("%d"%table[0])