TopcoderArchive
Archive
Algorithm problem

ShootingGame

Problem statement, definition, constraints, and public examples.

Problem Statement

Alice and Bob are playing a shooting game.

The players take alternating turns, Alice goes first. In each turn the active player gets to take one shot at a target. Whoever hits the target first, wins the game.

In any single shot Alice hits the target with probability pAlice, while Bob hits the target with probability pBob.

You are given an int p such that pAlice = p / 10^6. Compute and return the value of pBob for which both players have an equal chance of winning the game. If this is impossible, return -1 instead.

Definition

Class:
ShootingGame
Method:
findProbability
Parameters:
int
Returns:
double
Method signature:
double findProbability(int p)
(be sure your method is public)

Notes

  • Your answer will be accepted if the absolute error between your answer and the correct answer is less than 10-6.

Constraints

  • p will be between 1 and 10^6, inclusive.

Examples

  1. 999997
    Returns: -1.0
    Alice almost certainly wins the game already with her first shot, regardless of how good Bob happens to be.
  2. 400000
    Returns: 0.6666666666666667
    Each of Alice's shots has a 40% probability to hit the target. In order to have the same chance to win as Alice, Bob must be a much better marksman than she is.
Back to all problems