eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Double Dealing

Double Dealing

Take a deck of \textbf{n} unique cards. Deal the entire deck out to \textbf{k} players in the usual way: the top card to player \textbf{1}, the next to player \textbf{2}, the \textbf{k}^th to player \textbf{k}, the \textbf{k+1}^st to player \textbf{1}, and so on. Then pick up the cards -- place player \textbf{1}′s cards on top, then player \textbf{2}, and so on, so that player \textbf{k}’s cards are on the bottom. Each player’s cards are in reverse order -- the last card that they were dealt is on the top, and the first on the bottom. How many times, including the first, must this process be repeated before the deck is back in its original order? \InputFile There will be multiple test cases in the input. Each case will consist of a single line with two integers, \textbf{n} and \textbf{k} (\textbf{1} ≤ \textbf{n} ≤ \textbf{800}, \textbf{1} ≤ \textbf{k} ≤ \textbf{800}). The input will end with a line with two \textbf{0}s. \OutputFile For each test case in the input, print a single integer, indicating the number of deals required to return the deck to its original order. Output each integer on its own line, with no extra spaces, and no blank lines between answers. All possible inputs yield answers which will fit in a signed \textbf{64}-bit integer.
Ліміт часу 20 секунд
Ліміт використання пам'яті 32 MiB
Вхідні дані #1
1 3
10 3
52 4
0 0
Вихідні дані #1
1
4
13
Джерело The University of Chicago Invitational Programming Contest 2012 15 April 2012