There are N children standing in a circle, numbered from 1 to N in clockwise order. A teacher distributes T toys, starting from the child at position D and giving one toy to each consecutive child in clockwise order (wrapping back to child 1 after child N).
Return the position (1-indexed) of the last child to receive a toy.
Function Signature
def last_child(n: int, t: int, d: int) -> int:
Rules
-
The parameters
n
,
t
and
d
are
N
,
T
and
D
from the statement.
-
Child
d
receives the first toy. A child can receive more than one toy when
t > n
.
-
Return a position between
1
and
n
inclusive.
Constraints
-
1 <= n <= 10^9
-
1 <= t <= 10^9
-
1 <= d <= n
Examples
Input: n = 5, t = 2, d = 1
Output: 2
Toy 1 goes to child 1 and toy 2 goes to child 2, so the last child is 2.
Input: n = 5, t = 7, d = 3
Output: 4
Starting at child 3, the toys go to children 3, 4, 5, 1, 2, 3, 4. The seventh and last toy goes to child 4.
Input: n = 4, t = 6, d = 3
Output: 4
The toys go to children 3, 4, 1, 2, 3, 4, so the last toy lands on child 4, the highest position in the circle.