본문 바로가기

HackerRank/Algorithms

Save the Prisoner!

A jail has a number of prisoners and a number of treats to pass out to them. Their jailer decides the fairest way to divide the treats is to seat the prisoners around a circular table in sequentially numbered chairs. A chair number will be drawn from a hat. Beginning with the prisoner in that chair, one candy will be handed to each prisoner sequentially around the table until all have been distributed.

The jailer is playing a little joke, though. The last piece of candy looks like all the others, but it tastes awful. Determine the chair number occupied by the prisoner who will receive that candy.

For example, there are  prisoners and  pieces of candy. The prisoners arrange themselves in seats numbered  to . Let's suppose two is drawn from the hat. Prisoners receive candy at positions . The prisoner to be warned sits in chair number .

Input Format

The first line contains an integer, , denoting the number of test cases. 
The next  lines each contain  space-separated integers: 
: the number of prisoners 
: the number of sweets 
: the chair number to start passing out treats at

Constraints

Output Format

For each test case, print the chair number of the prisoner who receives the awful treat on a new line.

Sample Input 0

2
5 2 1
5 2 2

Sample Output 0

2
3

Explanation 0

In first query, there are  prisoners and  sweets. Distribution starts at seat number . Prisoners in seats numbered  and  get sweets. Warn prisoner 
In the second query, distribution starts at seat  so prisoners in seats  and  get sweets. Warn prisoner .




이 문제는 문제에서 간단한 규칙을 발견할 수 있느냐 없느냐의 문제이다.

그냥 막연하게 루프를 돌리면서 계산을 하는 방식으로(사탕의 갯수를 하나씩 줄이면서 죄수번호를 찾아가는) 하게 되면, 테스트 케이스를 통과하지 못한다.

큰 숫자의 테스트 케이스에서는 타임 아웃이 발생하게 된다. 그러니 규칙을 발견하고 간단한 수식으로 해결해야 한다.




'HackerRank > Algorithms' 카테고리의 다른 글

Sequence Equation  (0) 2018.07.17
Circular Array Rotation  (0) 2018.07.17
Viral Advertising  (0) 2018.07.16
Beautiful Days at the Movies  (0) 2018.07.16
Angry Professor  (0) 2018.07.16