Connection Status:
Competition Arena > AliceGame
SRM 639 · 2014-08-25 · by dreamoon · Simple Math
Class Name: AliceGame
Return Type: long
Method Name: findMinimumValue
Arg Types: (long long, long long)
Problem Statement

Problem Statement

Alice and Kirito just played a game. The game consisted of a finite (possibly empty) sequence of turns. You do not know the exact number of turns. The turns were numbered starting from 1. In each turn, exactly one of our two players won. The winner of turn i scored 2*i-1 points.

You are given two longs x and y. Find out whether it is possible that at the end of the game Alice had exactly x points and Kirito had exactly y points. If it is possible, return the smallest number of turns Alice could have won. If the given final result is not possible, return -1 instead.

Constraints

  • x and y are between 0 and 1,000,000,000,000, inclusive.
Examples
0)
8
17
Returns: 2

This final result is possible. Alice must have won at least two turns. One possibility is that Alice won turns 2 and 3 (for 3+5 = 8 points) and Kirito won turns 1, 4, and 5 (for 1+7+9 = 17 points).

1)
17
8
Returns: 3
2)
0
0
Returns: 0
3)
9
9
Returns: -1
4)
500000
500000
Returns: 294

Submissions are judged against all 89 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class AliceGame with a public method long long findMinimumValue(long long x, long long y) · 89 test cases · 2 s / 256 MB per case

Submitting as anonymous