Counting Skyscrapers

A number of skyscrapers have been built in a line. The number of skyscrapers was chosen uniformly at random between 2 and 314! (314 factorial, a very large number). The height of each skyscraper was chosen randomly and independently, with height i having probability 2 - i for all positive integers i. The floors of a skyscraper with height i are numbered 0 through i - 1.

To speed up transit times, a number of zip lines were installed between skyscrapers. Specifically, there is a zip line connecting the i-th floor of one skyscraper with the i-th floor of another skyscraper if and only if there are no skyscrapers between them that have an i-th floor.

Alice and Bob decide to count the number of skyscrapers.

Alice is thorough, and wants to know exactly how many skyscrapers there are. She begins at the leftmost skyscraper, with a counter at 1. She then moves to the right, one skyscraper at a time, adding 1 to her counter each time she moves. She continues until she reaches the rightmost skyscraper.

Bob is impatient, and wants to finish as fast as possible. He begins at the leftmost skyscraper, with a counter at 1. He moves from building to building using zip lines. At each stage Bob uses the highest available zip line to the right, but ignores floors with a height greater than h due to fear of heights. When Bob uses a zip line, he travels too fast to count how many skyscrapers he passed. Instead, he just adds 2 i to his counter, where i is the number of the floor he’s currently on. He continues until he reaches the rightmost skyscraper.

Consider the following example. There are 6 buildings, with heights 1, 4, 3, 4, 1, 2 from left to right, and h = 2. Alice begins with her counter at 1 and then adds 1 five times for a result of 6. Bob begins with his counter at 1, then he adds 1, 4, 4, and 2, in order, for a result of 12. Note that Bob ignores the highest zip line because of his fear of heights ( h = 2).

Bob’s counter is at the top of the image, and Alice’s counter at the bottom. All zip lines are shown. Bob’s path is shown by the green dashed line and Alice’s by the pink dashed line. The floors of the skyscrapers are numbered, and the zip lines Bob uses are marked with the amount he adds to his counter.

When Alice and Bob reach the right-most skyscraper, they compare counters. You will be given either the value of Alice’s counter or the value of Bob’s counter, and must compute the expected value of the other’s counter.


The first line of input will be a name, either string “Alice” or “Bob”. The second line of input contains two integers n and h (2 ≤ n ≤ 30000, 0 ≤ h ≤ 30). If the name is “Alice”, then n represents the value of Alice’s counter when she reaches the rightmost skyscraper, otherwise n represents the value of Bob’s counter when he reaches the rightmost skyscraper; h represents the highest floor number Bob is willing to use.


Output a single real value giving the expected value of the Alice’s counter if you were given Bob’s counter, or Bob’s counter if you were given Alice’s counter.

You answer will be considered correct if its absolute or relative error doesn’t exceed 10 - 9.



3 1




2 30




2572 10




In the first example, Bob’s counter has a 62.5% chance of being 3, a 25% chance of being 4, and a 12.5% chance of being 5.


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <memory.h>

using namespace std;

double f[33333][33], s[33333][33];
double prob[33], sp[33];

int main() {
char foo[42];
scanf("%s", foo);
int n, h;
scanf("%d %d", &n, &h);
if (foo[0] == 'A') {
prob[0] = 0.5;
for (int j=1;j<=h;j++) prob[j] = prob[j-1] * 0.5;
    prob[h] *= 2;
    f[0][0] = 1.0;
    s[0][0] = 0.0;
    for (int i=0;i<n;i++)
      for (int j=0;j<=h;j++) {
        for (int q=0;q<=h;q++) {
          double ft = f[i][j] * prob[q];
          double st = s[i][j] * prob[q];
          if (ft < 1e-100) ft = 0.0;
          if (q < j) {
            f[i + 1][j] += ft;
            s[i + 1][j] += st;
          } else
          if (q == j) {
            f[i + 1][j] += ft;
            s[i + 1][j] += st + (1 << j) * ft;
          } else {
            f[i + 1][q] += ft;
            s[i + 1][q] += st + (1 << j) * ft;
    sp[h] = prob[h];
    for (int i=h-1;i>=0;i--) sp[i] = prob[i] + sp[i+1];
double ans = 0.0, tp = 0.0;
for (int i=1;i<=n;i++) {
      for (int q=0;q<h;q++) {
        for (int oq=0;oq<=h;oq++) {
          int j = oq;
          if (q+1 > j) j = q+1;
double p = f[n - i][oq] * f[i - 1][q] * sp[j];
if (p > 1e-50) {
double s1 = s[n - i][oq] / f[n - i][oq];
double s2 = s[i - 1][q] / f[i - 1][q];
ans += (s1 + s2 + (1 << q) + (1 << oq) - 1) * p;
            tp += p;
    double lastp = 1.0;
    for (int i=0;i<n;i++) {
      lastp *= prob[0];
      if (lastp < 1e-50) lastp = 0.0;
    ans += lastp * n;
    tp += lastp;
    printf("%.17lf\n", ans);
  } else {
    printf("%d\n", n);
  return 0;