TopcoderArchiveVisit Topcoder
TCO8 Apr 2015
2015 TCO 2B

Bitwisdom

Problem statement, definition, constraints, and public examples.

Problem Statement

In this problem we are dealing with a string of N bits, numbered from 0 to N-1.
Julia likes the string that consists of N zeros.
If you give her any N-bit string, she will convert it into a string of N zeros using the smallest possible number of actions.
An action consists of selecting an integer k (1 <= k <= N) and flipping either the first k bits or the last k bits of the string.
Formally, Julia can flip either all bits with number i such that i < k or all bits with number i such that i >= N - k.

You are going to generate a random string of N bits.
You are given a int[] p with N elements.
For each i, bit number i has a p[i] percent chance of being a 1 and a (100 - p[i]) percent chance of being a 0.
The values of the bits are chosen independently from each other.

After you generate the string, you are going to give it to Julia.
Please find and return the expected number of actions Julia will take.

Definition

Class:
Bitwisdom
Method:
expectedActions
Parameters:
int[]
Returns:
double
Method signature:
double expectedActions(int[] p)
(be sure your method is public)

Notes

  • When a bit is flipped, its value changes. If the bit is originally 1, it will become 0. If it is originally 0, it will become 1.
  • Your answer must have an absoute or relative error not exceeding 10^(-9).

Constraints

  • N will be between 1 and 300, inclusive.
  • p will contain exactly N elements.
  • Each element of p will be between 0 and 100, inclusive.

Examples

  1. {100, 100, 100}
    Returns: 1.0
    All of the bits are guaranteed to be 1, and Julia can set them all to 0 in a single action.
  2. {50, 50}
    Returns: 0.75
    There is a 25% chance that all of the bits are 0, in which case Julia will do nothing. All other configurations allow Julia to clear the field in 1 action, so the answer is 0.75 * 1 = 0.75.
  3. {0, 40, 50, 60}
    Returns: 1.4
  4. {37, 63, 23, 94, 12}
    Returns: 2.6820475464
Back to all problems