CF BUDDY
← Problems·

1866L · Lihmuf Balling

2400 · binary search, brute force, math

Problem: After working at a factory (Lihmuf Sorting), Lihmuf wants to play a game with his good friend, Pak Chanek. There are NN boxes numbered from 11 to NN. The ii-th box contains ii balls. Pak Chanek and Lihmuf will play a game using those boxes.

There will be NN turns numbered from 11 to NN. On each turn, Pak Chanek chooses one box and takes all balls from that box, then Lihmuf does the same thing for one box he chooses (it is possible that the two chosen boxes are the same).

Given an integer MM. Before the game begins, Lihmuf must choose an integer KK satisfying 1KM1 \leq K \leq M. It is known that on the jj-th turn, Pak Chanek will choose the jj-th box, while Lihmuf will choose the yy-th box, with y=((j×K1)modN)+1y=((j \times K - 1) \bmod N) + 1. Help Lihmuf choose the correct value of KK so that he will get as many balls as possible! If there are more than one possible values of KK that result in the maximum total balls, find the minimum such value of KK.

Keep in mind that each box can be chosen more than once, but after a box is chosen for the first time, the box becomes empty.

Input Format: The only line contains two integers NN and MM (1N1091 \leq N \leq 10^9; 1M20001 \leq M \leq 2000) — the number of boxes and the maximum limit for the value of KK.

Output Format: An integer representing the value of KK that will make Lihmuf get as many balls as possible. If there are more than one possible value of KK that result in the maximum total balls, find the minimum such value of KK.

Note: In the first example, if Lihmuf chooses K=1K=1, the game will go as follows:

  1. Pak Chanek chooses the 11-st box and gets 11 ball, then Lihmuf chooses the 11-st box and gets 00 balls.
  2. Pak Chanek chooses the 22-nd box and gets 22 balls, then Lihmuf chooses the 22-nd box and gets 00 balls.
  3. Pak Chanek chooses the 33-rd box and gets 33 balls, then Lihmuf chooses the 33-rd box and gets 00 balls.

In total, Lihmuf gets 0+0+0=00+0+0=0 balls.

The maximum total balls that can be earned by Lihmuf is 00 because he can only choose K=1K=1. Therefore, K=1K=1 is the minimum possible value of KK that results in the maximum total balls.

In the second example, if Lihmuf chooses K=3K=3, the game will go as follows:

  1. Pak Chanek chooses the 11-st box and gets 11 ball, then Lihmuf chooses the 33-rd box and gets 33 balls.
  2. Pak Chanek chooses the 22-nd box and gets 22 balls, then Lihmuf chooses the 11-st box and gets 00 balls.
  3. Pak Chanek chooses the 33-rd box and gets 00 balls, then Lihmuf chooses the 44-th box and gets 44 balls.
  4. Pak Chanek chooses the 44-th box and gets 00 balls, then Lihmuf chooses the 22-nd box and gets 00 balls.
  5. Pak Chanek chooses the 55-th box and gets 55 balls, then Lihmuf chooses the 55-th box and gets 00 balls.

In total, Lihmuf gets 3+0+4+0+0=73+0+4+0+0=7 balls.

It can be obtained that 77 is the maximum total balls that can be earned by Lihmuf. The possible values of KK that result in 77 total balls are 33 and 44. Therefore, K=3K=3 is the minimum possible value of KK that results in the maximum total balls.

Sample Cases

Case 1

Input

3 1

Output

1

Case 2

Input

5 4

Output

3

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback