Turn Game
Time limit2sMemory limit512 MB
Given final scores x and y, decide whether some prefix of 1,2,3,... can be partitioned into two groups summing to x and y, and find the minimum count of terms in Yunho's group.
- Level
Medium7 of 10
- Topics
- Math, Greedy, Binary search, Number theory
- Solved
- No attempts yet
Problem
Yunho and Donghyuk are at an algorithm camp. When a problem refuses to come out, the two of them play a game instead.
The game is made of turns, and each turn is won by one of the two players. Turns are numbered starting from , and whoever wins turn earns points.
Given two integers and , write a program that decides whether the game can end with Yunho holding points and Donghyuk holding points. If it can, also find the smallest number of turns Yunho has to win.
Input
The first line contains two integers and . ()
Output
Print the smallest number of turns Yunho has to win. If no such game exists, print .
Hint
If Yunho scores and Donghyuk scores , the game runs for turns. Yunho winning turns while Donghyuk wins turns is one possible result. Yunho wins the fewest turns when he takes turns and and Donghyuk takes turns .