Given A players in a circle and a chant pattern by rounds, find who makes the T-th call of a chosen word (ppeon or degi), counting only that word.
Easy3MathImplementationSimulationBinary searchInterviewNo attempts yetTime limit1sMemory limit128 MBIlgu, a freshman who just entered the software department at Chung-Ang University, learned several drinking games at the freshman orientation. The one he liked most is Beondegi.
The rules are these. The first round chant is 'ppeon, degi, ppeon, degi, ppeon, ppeon, degi, degi'. The second round chant is 'ppeon, degi, ppeon, degi, ppeon, ppeon, ppeon, degi, degi, degi'. In general, round n starts with 'ppeon, degi, ppeon, degi', then says 'ppeon' n+1 times, then says 'degi' n+1 times. Every single call passes the turn to the next person. One round runs straight into the next one, and the game keeps going after the circle comes back around to Ilgu.
Ilgu, his classmates and the seniors, A people in total, sit in a circle as shown below.

Ilgu is person 0 and the game runs counterclockwise. Counting from the start of the first round, find the number of the person who makes the Tth call of a given word. A freshman is carried out the moment the count reaches 10000, so T is a natural number no larger than 10000.
The first line has the number of players A. A is a natural number no larger than 2,000.
The second line has the ordinal T you are asked about. T is a natural number no larger than 10000.
The third line has the word you are asked about: 0 for 'ppeon', 1 for 'degi'.
Print the number of the person who makes that call, as an integer, on the first line.
The Tth call counts only the word given on the third line. The two words are never counted together.
Ilgu is person 0, and the numbering continues counterclockwise as person 1, person 2, and so on.