-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveThePrisoner.py
41 lines (30 loc) · 895 Bytes
/
saveThePrisoner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/python3
import math
import os
import random
import re
import sys
"""
In this problem, we need to determine the ID of last prisoner which can be done by moving M-1 steps further from S.
ID of last prisoner= (M-1+S)
Since we are moving in a circle so we need to take mod of this summation with N.
Because the ID starts from 1, so the ID of last prisoner= (M – 1 + S – 1) % N + 1)
"""
# Complete the saveThePrisoner function below.
def saveThePrisoner(n, m, s):
value = (n+m+s-1)%n
if value == 0:
return n
else:
return value
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input())
for t_itr in range(t):
nms = input().split()
n = int(nms[0])
m = int(nms[1])
s = int(nms[2])
result = saveThePrisoner(n, m, s)
fptr.write(str(result) + '\n')
fptr.close()