eolymp
bolt
Try our new interface for solving problems

Casino

Michael Corleone decided to open a new casino in Las-Vegas, Nevada. His personal favorite game is a special version of the roulette game, there five players use the roulette wheel. There are N slots on the table, each slot has the card with a number from \textbf{1} to \textbf{N}. All cards have different numbers. Initially the cards are ordered, that is, the \textbf{1}^st card is in the 1st slot, the \textbf{2}^nd card is in the \textbf{2}^nd slot and etc. In other words, initially every card is in the right position - that is, the number of the card and the number of the slot match. The game consists of \textbf{M} moves made by players. Let's call players \textbf{A}, \textbf{B}, \textbf{C}, \textbf{D} and \textbf{E}. Here is how they play: \begin{itemize} \item Player \textbf{A} makes the first move. He takes the last card out of the last \textbf{N}^\{-th\} slot. After that he moves all the cards in slots \textbf{\[1 ... (N-1)\]} one slot right. That is, the \textbf{1}^st card moves to the \textbf{2}^nd slot, the \textbf{2}^nd card moves to the \textbf{3}^rd slot and etc. After that the \textbf{1}^st slot becomes empty. Player \textbf{A} puts the last card in the \textbf{1}^st slot. \item Player \textbf{B} makes the second move. He takes the last card from the last \textbf{N}^\{-th\} slot. After that he moves all the cards in slots \textbf{\[(N--N/2) ... (N-1)\]} one slot right. After that he puts the card that he has on his hands in empty slot. In other words, he does the same thing as the first player, but for the range \textbf{\[(N-N/2) ... N\]}. \item Player \textbf{C} makes the third move. He takes the card from the slot with number \textbf{N/4}. After that he moves all the cards in slots \textbf{\[1 ... (N/4-1)\]} one slot right. After that he puts the card that he has on his hands in empty slot. \item Played \textbf{D} makes the fourth move. He takes the card from the last \textbf{N}^\{-th\} slot. After that he moves all the cards in slots \textbf{\[(N--N/6) ... (N-1)\]} one slot right. After that he puts the card that he has on his hands in empty slot. In other words, he does the same thing as the first player, but for the range \textbf{\[(N--N/6) ... N)\]}. \item Player \textbf{E} makes the fifth move. He takes the card from the slot with number \textbf{N/8}. After that he moves all the cards in slots \textbf{\[1 ... (N/8-1)\]} one slot right. After that he puts the card that he has on his hands in empty slot. \end{itemize} After that the game repeats, until \textbf{M} moves are made. That is, if there are \textbf{7} moves, then the sequence of player moves is \textbf{ABCDEAB}, that is players \textbf{A} and \textbf{B} make two moves, but other players make just one move. Now, you have decided to make a bet on the card positions after \textbf{M} moves. According to casino rules, if you make a bet in the beginning of the game on card positions after \textbf{M} moves, you will get \textbf{L} dollars for that, where \textbf{L} is the number of cards in the right position after \textbf{M} moves. For example, suppose there are \textbf{20} slots on the table and you made a bet on three moves. Players \textbf{A}, \textbf{B} and \textbf{C} make a move each. After that suppose only three cards are in the right position - \textbf{3}, \textbf{10} and \textbf{17}. That means you will get \textbf{3 }dollars from the casino, if you make such a bet. You would like to know in advance how much you can win. \InputFile The input consists of two numbers \textbf{N} and \textbf{M} (\textbf{40} ≤ \textbf{N} ≤ \textbf{126}, \textbf{1} ≤ \textbf{M} < \textbf{10^9}) separated by spaces. \OutputFile The output should contain a single number \textbf{L} - the number of dollars the casino will pay you after \textbf{M} moves.
Zaman məhdudiyyəti 2 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
48 1
Çıxış verilənləri #1
0
Müəllif Шевченко Дмитрий
Mənbə Osipovsky Cup - 2013