Problem:
Problem F: Ben Toh As usual, those who called wolves get together on 8 p.m. at the supermarket. The thing they want is only one, a box lunch that is labeled half price. Scrambling for a few discounted box lunch, they fiercely fight every day. And those who are blessed by hunger and appetite the best can acquire the box lunch, while others have to have cup ramen or something with tear in their eyes. A senior high school student, Sato, is one of wolves. A dormitry he lives doesn't serve a dinner, and his parents don't send so much money. Therefore he absolutely acquire the half-priced box lunch and save his money. Otherwise he have to give up comic books and video games, or begin part-time job. Since Sato is an excellent wolf, he can acquire the discounted box lunch in 100% probability on the first day. But on the next day, many other wolves cooperate to block him and the probability to get a box lunch will be 50%. Even though he can get, the probability to get will be 25% on the next day of the day. Likewise, if he gets a box lunch on a certain day, the probability to get on the next day will be half. Once he failed to get a box lunch, probability to get would be back to 100%. He continue to go to supermaket and try to get the discounted box lunch for n days. Please write a program to computes the expected value of the number of the discounted box lunches he can acquire. Input Input consists of several datasets. Input for a single dataset is given as a single integer n . Input terminates with a dataset where n = 0. Output For each dataset, write a line that contains an expected value. You may print any number of digits after the decimal point. Answers that have an error less than 1.0e-2 will be accepted. Constraints  1 ≤ n ≤ 100,000  Sample Input 1
2
3
0 Output for the Sample Input 1.00000000
1.50000000
2.12500000

Solution (Python):

if __name__ == "__main__":
    import sys
    dp = [0.0] * 100001
    dp[1] = 1.0
    for i in xrange(2, 100001):
        p = 1.0
        w = 0.5
        for j in xrange(1,i):
            if p < 1e-13:
                break
            dp[i] += (p*(1-w))*(j+dp[max(0,i-j-1)])
            p *= w
            w *= 0.5
        dp[i] += i*p
    while True:
        N = int(raw_input())
        if N == 0:
            sys.exit()
        print "%.8f" % dp[N]

Solution (Python):

if __name__ == "__main__":
    import sys
    dp = [0.0] * 100001
    dp[1] = 1.0
    for i in xrange(2, 100001):
        p = 1.0
        w = 0.5
        for j in xrange(1,i):
            if p < 1e-13:
                break
            dp[i] += (p*(1-w))*(j+dp[max(0,i-j-1)])
            p *= w
            w *= 0.5
        dp[i] += i*p
    while True:
        N = int(raw_input())
        if N == 0:
            sys.exit()
        print "%.8f" % dp[N]

Solution (Python):

if __name__ == "__main__":
    import sys
    dp = [0.0] * 100001
    dp[1] = 1.0
    for i in xrange(2, 100001):
        p = 1.0
        w = 0.5
        for j in xrange(1,i):
            if p < 1e-13:
                break
            dp[i] += (p*(1-w))*(j+dp[max(0,i-j-1)])
            p *= w
            w *= 0.5
        dp[i] += i*p
    while True:
        N = int(raw_input())
        if N == 0:
            sys.exit()
        print "%.8f" % dp[N]

Solution (Python):
dp=[.0]*100001
dp[1]=1.
for i in range(2,100001):
    a,b,j=.5,1,1
    while j<i and b>1e-15:
        dp[i]+=b*(1-a)*(j+dp[i-j-1])
        b*=a;a/=2;j+=1
    dp[i]+=i*b
while 1:
    n=int(input())
    if n==0:break
    print(dp[n])

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

P = [0.0 for i in range(110000)]
ans = [0.0 for i in range(110000)]
for i in range(100020):
    p = 1.0 - P[i]
    mul = 1.0
    for j in range(1, 11):
        P[i + j] += p * mul
        for k in range(j):
            mul /= 2
for i in range(1, 100020):
    ans[i] = ans[i - 1] + P[i]

while True:
    n = input()
    if n == 0:
        break
    print ans[n]