Baskin-Robbins 31
InterviewTime limit2sMemory limit256 MB
Given n games with parameters j and m, compute how many turns each lasts and report the earliest game with the minimum turn count.
- Level
Easy3 of 10
- Topics
- Math, Implementation, Simulation, Brute force
- Solved
- No attempts yet
Problem
On the first night of a retreat, Yujin and Gyuyong play the Baskin-Robbins 31 game, and the loser has to drink soy sauce. The rules are as follows.
- Yujin and Gyuyong sit side by side in a row. Yujin sits at the far left.
- The game starts with Yujin and proceeds to the right, so the two players take turns alternately.
- On your turn, you say between 1 and consecutive natural numbers from 1 to , continuing from the last number said. You must say at least one number.
- The player who says loses.
They played several times, but Yujin kept drinking the soy sauce. Annoyed, Yujin searched the internet and found a strategy that always wins. The strategy is:
- Divide the total count by and take the remainder .
- The number is the first winning number.
- Adding to it again and again gives all the winning numbers.
- When the game starts, say numbers up to the first winning number.
- After that, no matter how many numbers the opponent says, say numbers up to the next winning number on each of your turns, and you win.
For example, if and , then , so and the winning numbers are .
Define the length of a game as the number of turns it takes until Yujin wins by following this strategy. The count includes the turns of both players, including Gyuyong's final turn in which he says . In the example above, Yujin has 8 turns and Gyuyong has 8 turns, so the length is 16.
Given games, find the number of the shortest game and its length.
Input
The first line contains the number of games ().
Each of the next lines describes one game. It contains , the total count and the number whose speaker loses, and , the maximum count of natural numbers that can be said in one turn, separated by a space (, ).
It is guaranteed that and that is not a multiple of . In other words, Yujin can always win.
Output
Print the number of the shortest game and its length, separated by a space. Games are numbered from 1 in input order. If two or more games share the shortest length, print the number of the one that appears first in the input, together with its length.