Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor. 

Let's assume that n people stand in the queue for the escalator. At each second one of the two following possibilities takes place: either the first person in the queue enters the escalator with probability p, or the first person in the queue doesn't move with probability (1 - p), paralyzed by his fear of escalators and making the whole queue wait behind him.

Formally speaking, the i-th person in the queue cannot enter the escalator until people with indices from 1 to i - 1 inclusive enter it. In one second only one person can enter the escalator. The escalator is infinite, so if a person enters it, he never leaves it, that is he will be standing on the escalator at any following second. Ilya needs to count the expected value of the number of people standing on the escalator after t seconds. 

Your task is to help him solve this complicated task.


-----Input-----

The first line of the input contains three numbers n, p, t (1 ≤ n, t ≤ 2000, 0 ≤ p ≤ 1). Numbers n and t are integers, number p is real, given with exactly two digits after the decimal point.


-----Output-----

Print a single real number — the expected number of people who will be standing on the escalator after t seconds. The absolute or relative error mustn't exceed 10^{ - 6}.


-----Examples-----
Input
1 0.50 1

Output
0.5

Input
1 0.50 4

Output
0.9375

Input
4 0.20 2

Output
0.4

Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Combinatorics

Mathematics

Probability
Skill types: Dynamic programming

Starter code:

Solutions:
(a, b, c) = map(float, input().split(' '))
a = int(a)
c = int(c)
dp = [[0] * (c + 1) for _ in range(c + 1)]
for i in range(c + 1):
	for j in range(c + 1):
		if i == 0:
			if j == 0:
				dp[i][j] = 1
			else:
				dp[i][j] = 0
		elif j > a:
			dp[i][j] = 0
		elif j == a:
			dp[i][j] = dp[i - 1][j - 1] * b + dp[i - 1][j]
		else:
			dp[i][j] = (1 - b) * dp[i - 1][j] + b * dp[i - 1][j - 1]
sumx = 0
for i in range(c + 1):
	sumx += i * dp[c][i]
print(sumx)


def find_expected_people(people_count, probability, seconds):
	current_row = [0.0] * (people_count + 1)
	current_row[0] = 1.0
	for sec in range(1, seconds + 1):
		next_row = [x * (1.0 - probability) for x in current_row]
		next_row[-1] = current_row[-1]
		for person in range(people_count):
			next_row[person + 1] += current_row[person] * probability
		current_row = next_row
	return sum((index * value for (index, value) in enumerate(current_row)))

def main():
	raw_input = input().split()
	people_count = int(raw_input[0])
	probability = float(raw_input[1])
	seconds = int(raw_input[2])
	print(find_expected_people(people_count, probability, seconds))
main()


(n, p, t) = list(map(float, input().split()))
(n, t) = (int(n + 0.5), int(t + 0.5))
if p < 0.001 or p > 0.999:
	print(n * p)
	return
from math import exp, lgamma, log
print('%.10f' % sum((exp(i * log(p) + (t - i) * log(1 - p) + lgamma(t + 1) - lgamma(i + 1) - lgamma(t - i + 1)) * min(i, n) for i in range(t + 1))))


(n, p, t) = input().split()
n = int(n)
p = float(p)
t = int(t)
n = min(n, t)
dp = [[0 for i in range(t + 1)] for j in range(n + 1)]
for i in range(1, n + 1):
	for j in range(1, t + 1):
		dp[i][j] = (dp[i - 1][j - 1] + 1) * p + dp[i][j - 1] * (1 - p)
print(dp[n][t])


(n, p, t) = map(float, input().split())
(n, t) = (int(n), int(t))
dp = [[0 for i in range(t + 1)] for j in range(t + 1)]
dp[0][0] = 1
for i in range(1, t + 1):
	for j in range(i + 1):
		if j >= 1:
			dp[i][j] = p * dp[i - 1][j - 1] + (1 - p) * dp[i - 1][j]
		else:
			dp[i][j] = (1 - p) * dp[i - 1][j]
s = 0
s1 = sum(dp[t])
s2 = 0
for j in range(t + 1):
	if j > n:
		s += n * (s1 - s2)
		break
	s += dp[t][j] * j
	s2 += dp[t][j]
print(s)


dp = [[0 for i in range(2005)] for i in range(2005)]
(n, p, t) = (0, 0, 0)
(n, p, t) = map(float, input().split())
(n, t) = (int(n), int(t))
dp[0][0] = 1
for i in range(1, t + 1):
	for j in range(n + 1):
		if j == n:
			dp[i][j] = dp[i - 1][j]
		else:
			dp[i][j] = dp[i - 1][j] * (1 - p)
		if j:
			dp[i][j] += dp[i - 1][j - 1] * p
ans = 0
for i in range(n + 1):
	ans += i * dp[t][i]
print(ans)


xs = input().split()
(n, p, t) = (int(xs[0]), float(xs[1]), int(xs[2]))
(cur, nxt) = ([0.0] * (n + 1), [0.0] * (n + 1))
cur[0] = 1.0
for i in range(t):
	nxt[n] = cur[n]
	for j in range(n):
		nxt[j] = cur[j] * (1.0 - p)
	for j in range(n):
		nxt[j + 1] += cur[j] * p
	(cur, nxt) = (nxt, cur)
print(sum((cur[i] * i for i in range(n + 1))))


import math

def round_up(n, decimals=0):
	multiplier = 10 ** decimals
	return math.ceil(n * multiplier) / multiplier
a = list(map(float, input().split()))
n = int(a[0])
t = int(a[2])
p = a[1]
dp = [[float(0) for k in range(n + 1)] for i in range(t + 1)]
dp[0][n] = 1
dp[1][n] = 1 - p
dp[1][n - 1] = p
for i in range(t + 1):
	dp[i][n] = (1 - p) ** i
for j in range(n)[::-1]:
	for i in range(1, t + 1):
		dp[i][j] = (1 - p) * dp[i - 1][j] + p * dp[i - 1][j + 1]
sum = float(0)
for j in range(n + 1):
	sum += dp[t][j] * j
print(round_up(n - sum, 6))


import math
MAX = 2001
fact = [math.log10(1)]
for zz in range(1, MAX + 1):
	fact.append(fact[-1] + math.log10(zz))

def nCr(n, r):
	num = fact[n]
	den = fact[r] + fact[n - r]
	return num - den
(n, p, t) = [float(x) for x in input().split()]
n = int(n)
t = int(t)
if p == 0:
	print(0)
elif p == 1:
	print(min(n, t))
else:
	ans = 0
	probs = [0 for _ in range(t + 1)]
	for x in range(t + 1):
		one = nCr(t, x)
		two = (t - x) * math.log10(1 - p)
		three = x * math.log10(p)
		probs[x] = 10 ** (one + two + three)
	for (x, p) in enumerate(probs):
		if x <= n:
			ans += x * p
		else:
			ans += n * p
	print(ans)


import sys
(n, p, t) = list(map(str, sys.stdin.readline().split()))
n = int(n)
p = float(p)
t = int(t)

def CC(nn, k):
	tmp = n
	t = max(nn - k, k)
	for i in range(1, min(nn - k, k) + 1):
		tmp = tmp * (t + i) * (1 - p) / i
	if k > nn - k:
		tmp = tmp * pow(1 - p, k + k - nn)
	return tmp

def C(n, k):
	tmp = 1
	if n - k > k:
		tmp = tmp * pow(1 - p, n - k - k)
	else:
		tmp = tmp * pow(p, k + k - n)
	t = max(n - k, k)
	for i in range(1, min(n - k, k) + 1):
		tmp = tmp * (t + i) * p * (1 - p) / i
	return tmp
if n >= t:
	print(t * p)
elif p != 1 and p != 0:
	a = 0
	b = 0
	for i in range(n):
		q = C(t, i)
		a = a + q * i
		b = b + q
	a = a + (1 - b) * n
	print(a)
	b = n
	for i in range(t - n):
		b = b + CC(i + 1, n + i)
	b = b * pow(p, n)
elif p == 1:
	print(n)
else:
	print(0)


a = input().split()
n = int(a[0])
p = float(a[1])
t = int(a[2])
den = 100 ** t
p = round(p * 100 + 1e-09)
q = 100 - p
ncr = [1 for i in range(2001)]
for i in range(1, t + 1):
	ncr[i] = ncr[i - 1] * (t - i + 1) // i
ans = 0
for i in range(2001):
	ans += min(i, n) * ncr[i] * p ** i * q ** (t - i) if t >= i else 0
ans /= den
print(ans)


(n, p, t) = input().split()
(n, t) = map(int, (n, t))
p = float(p)
q = 1 - p
ans = 0
dp = [[0 for i in range(n + 3)] for j in range(t + 3)]
dp[0][0] = 1
for i in range(t):
	for j in range(n):
		dp[i + 1][j + 1] += dp[i][j] * p
		dp[i + 1][j] += dp[i][j] * q
	dp[i + 1][n] += dp[i][n]
for i in range(n + 1):
	ans += i * dp[t][i]
print(ans)


(n, p, t) = input().split()
n = int(n)
p = float(p)
t = int(t)
dp = [[0 for _ in range(t + 1)] for _ in range(n + 1)]
dp[0][0] = 1
for n_p in range(n + 1):
	for t_p in range(t):
		dp[n_p][t_p + 1] = dp[n_p][t_p] * (1 - p) + dp[n_p][t_p + 1]
		if n_p != n:
			dp[n_p + 1][t_p + 1] = dp[n_p][t_p] * p + dp[n_p + 1][t_p + 1]
		else:
			dp[n_p][t_p + 1] = dp[n_p][t_p] * p + dp[n_p][t_p + 1]
ans = 0
for n_p in range(n + 1):
	ans += dp[n_p][-1] * n_p
print(ans)


MAXN = 2005
dp = [[0 for col in range(MAXN)] for row in range(MAXN)]
(n, q, t) = input().split()
n = int(n)
t = int(t)
q = float(q)
if t <= n:
	print(t * q)
else:
	dp[0][0] = 1
	for i in range(t + 1):
		for j in range(min(t, n) + 1):
			if j == n:
				dp[i + 1][j] += dp[i][j]
			else:
				dp[i + 1][j] += (1 - q) * dp[i][j]
				dp[i + 1][j + 1] += q * dp[i][j]
	ans = 0
	for i in range(n + 1):
		ans += i * dp[t][i]
	print(ans)


(n, p, t) = map(float, input().split())
mx = int(max(n, t) + 1)
dp = [[0 for i in range(mx + 10)] for j in range(mx + 10)]
dp[0][0] = 1
for i in range(mx):
	for j in range(1, mx):
		if i - 1 >= 0:
			dp[i][j] += dp[i - 1][j - 1] * p
		if i >= int(n):
			dp[i][j] += dp[i][j - 1]
		else:
			dp[i][j] += dp[i][j - 1] * (1 - p)
oup = 0
prev = 0
for i in range(int(n) + 1):
	oup += i * dp[i][int(t)]
print(oup)


import os
import sys
from io import BytesIO, IOBase

def main():
	(n, p, t) = input().split()
	n = int(n)
	p = float(p)
	t = int(t)
	if p == float(0):
		print(0)
		exit()
	if p == float(1):
		print(min(n, t))
		exit()
	dp = [[0 for _ in range(n + 1)] for _ in range(t + 1)]
	dp[1][1] = p
	for r in range(2, t + 1):
		for c in range(1, n + 1):
			dp[r][c] = p * dp[r - 1][c - 1] + (1 - p) * dp[r - 1][c]
	ans = 0
	for item in dp:
		ans += sum(item)
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


(n, p, t) = input().split()
(n, p) = (int(n), float(p))
f = [p] * n + [0]
for i in range(int(t) - 1):
	f = [f[k] * (1 - p) + p * (1 + f[k - 1]) for k in range(n)] + [0]
print(f[-2])


(n, p, t) = map(float, input().split())
n = int(n)
t = int(t)
d = [0] * (n + 1)
d[0] = 1
for i in range(t):
	for j in range(n + 1)[::-1]:
		if j == 0:
			d[j] = d[j] * (1 - p)
		elif j == n:
			d[j] = d[j - 1] * p + d[j]
		else:
			d[j] = d[j] * (1 - p) + d[j - 1] * p
ans = 0
for i in range(n + 1):
	ans += i * d[i]
print(ans)


inp = input().split()
(n, p, t) = (int(inp[0]), float(inp[1]), int(inp[2]))
dp = [[0] * (n + 2) for i in range(t + 2)]
dp[0][0] = 1.0
for i in range(t + 1):
	for j in range(n + 1):
		if j == n:
			dp[i + 1][j] += dp[i][j]
		else:
			dp[i + 1][j + 1] += dp[i][j] * p
			dp[i + 1][j] += dp[i][j] * (1 - p)
print(sum((dp[t][i] * i for i in range(n + 1))))


def main():
	a = input().split()
	n = int(a[0])
	p = float(a[1])
	t = int(a[2])
	z = [0.0] * (t + 2)
	for i in range(t + 2):
		z[i] = [0] * (n + 2)
	l = 0.0
	z[0][0] = 1.0
	for i in range(t + 1):
		for j in range(n + 1):
			if j == n:
				z[i + 1][j] += z[i][j]
			else:
				z[i + 1][j + 1] += z[i][j] * p
				z[i + 1][j] += z[i][j] * (1 - p)
	for j in range(n + 1):
		l += z[t][j] * j
	print(l)
main()


def main():
	tmp = input().split()
	(n, p, t) = (int(tmp[0]), float(tmp[1]), int(tmp[2]))
	q = 1.0 - p
	res = [0.0] * (n + 1)
	res[0] = 1.0
	for _ in range(t):
		tmp = [x * q for x in res]
		tmp[-1] = res[-1]
		for i in range(n):
			tmp[i + 1] += res[i] * p
		res = tmp
	print(sum((x * i for (i, x) in enumerate(res))))
main()


(n, p, t) = list(map(float, input().split()))
(n, t) = (int(n), int(t))
dp = [[0] * (n + 1) for __ in range(t)]
dp[0][0] = 1 - p
dp[0][1] = p
for i in range(1, t):
	dp[i][0] = dp[i - 1][0] * (1 - p)
	for j in range(1, n):
		dp[i][j] += dp[i - 1][j] * (1 - p)
		dp[i][j] += dp[i - 1][j - 1] * p
	dp[i][n] = dp[i - 1][n] + dp[i - 1][n - 1] * p
ans = 0
for i in range(n + 1):
	ans += i * dp[t - 1][i]
print(ans)


s = list(map(str, input().split()))
n = int(s[0])
p = float(s[1])
t = int(s[2])
combi = [[0 for _ in range(t + 1)] for _ in range(t + 1)]
combi[1][0] = 1 * p
combi[1][1] = 1 * (1 - p)
for i in range(2, t + 1):
	combi[i][0] = combi[i - 1][0] * p
	for j in range(1, i + 1):
		combi[i][j] = combi[i - 1][j - 1] * (1 - p) + combi[i - 1][j] * p
e = 0
for i in range(t + 1):
	v = n if t - i > n else t - i
	e += combi[t][i] * v
print(e)


inputs = [x for x in input().split()]
(n, p, t) = (int(inputs[0]), float(inputs[1]), int(inputs[2]))
saved = [[0 for _ in range(n)] for _ in range(t)]
saved[0][0] = p
for i in range(1, t):
	saved[i][0] = saved[i - 1][0] + (1 - saved[i - 1][0]) * p
for i in range(1, t):
	for j in range(1, n):
		saved[i][j] = saved[i - 1][j - 1] * p + saved[i - 1][j] * (1 - p)
print(sum(saved[-1]))


__author__ = 'PrimuS'
import decimal
decimal.getcontext().prec = 1000
inp = input().split()
n = int(inp[0])
p = decimal.Decimal(inp[1])
t = int(inp[2])
facs = {}
ps = {}
p1s = {}
facs[0] = decimal.Decimal(1)
ps[0] = decimal.Decimal(1)
p1s[0] = decimal.Decimal(1)
for i in range(1, max(n, t) + 1):
	facs[i] = facs[i - 1] * decimal.Decimal(i)
	ps[i] = ps[i - 1] * p
	p1s[i] = p1s[i - 1] * (1 - p)

def c(n, k):
	return facs[n] / facs[n - k] / facs[k]
res = decimal.Decimal(0)
up = min(n, t)
if n < t:
	up = n - 1
for i in range(1, up + 1):
	cur = decimal.Decimal(i)
	cur *= c(t, i)
	cur *= ps[i]
	cur *= p1s[t - i]
	res += cur
if n < t:
	cur2 = t
	while cur2 >= n:
		cur = decimal.Decimal(n)
		cur *= c(cur2 - 1, n - 1)
		cur *= ps[n]
		cur *= p1s[cur2 - n]
		res += cur
		cur2 -= 1
print(res)


(n, p, t) = input().split()
n = int(n)
t = int(t)
p = float(p)
ans = 0.0
s = t
if abs(1.0 - p) > 1e-06:
	a1 = 1.0
	for i in range(1, t + 1):
		a1 = a1 * (t - i + 1) / i if i <= t else 0
		a1 = a1 * p
		while a1 > 100000.0 and s > i:
			a1 *= 1 - p
			s -= 1
		while s < i:
			a1 /= 1 - p
			s += 1
		a2 = (1 - p) ** (s - i)
		ans += a1 * a2 * min(i, n)
else:
	ans = min(n, t)
print(ans)


(n, p, t) = [float(x) for x in input().split()]
n = int(n)
t = int(t)
probab = [[0 for _ in range(t + 1)] for __ in range(t + 1)]
probab[0][1] = 1 - p
probab[1][1] = p
for tt in range(2, t + 1):
	for xx in range(t + 1):
		if xx == 0:
			probab[xx][tt] = (1 - p) * probab[xx][tt - 1]
		else:
			probab[xx][tt] = p * probab[xx - 1][tt - 1] + (1 - p) * probab[xx][tt - 1]
ans = 0
for x in range(t + 1):
	ans += min(x, n) * probab[x][t]
print(ans)


def solve():
	a = list(input().split())
	n = eval(a[0])
	p = eval(a[1])
	t = eval(a[2])
	dp = []
	for i in range(0, t + 1):
		dp1 = []
		for j in range(0, n + 1):
			dp1.append(0)
		dp.append(dp1)
	dp[0][0] = 1
	for i in range(1, t + 1):
		for j in range(0, n + 1):
			if j == n:
				dp[i][j] += dp[i - 1][j]
			else:
				dp[i][j] += dp[i - 1][j] * (1 - p)
				dp[i][j + 1] += dp[i - 1][j] * p
	ans = 0
	for i in range(0, n + 1):
		ans += i * dp[t][i]
	print(ans)
for _ in range(1):
	solve()


import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

def solve(N, P, T):
	dp = [[0 for _ in range(N + 1)] for _ in range(T + 1)]
	p = 1
	for t in range(T + 1):
		dp[t][0] = p
		p *= 1 - P
	for t in range(1, T + 1):
		for i in range(1, min(N, t) + 1):
			dp[t][i] += dp[t - 1][i - 1] * P + dp[t - 1][i] * (1 - P if i < N else 1)
	return sum([i * dp[T][i] for i in range(N + 1)])
line = [x for x in input().split()]
(N, P, T) = (int(line[0]), float(line[1]), int(line[2]))
print(solve(N, P, T))


import sys
try:
	while True:
		dp = [[0 for j in range(2001)] for i in range(2001)] * 2001
		(n, p, t) = map(float, input().split(' '))
		n = int(n)
		t = int(t)
		dp[0][0] = 1
		for i in range(1, t + 1, 1):
			for j in range(0, n + 1, 1):
				if j == n:
					dp[i][j] = dp[i - 1][j]
				else:
					dp[i][j] = dp[i - 1][j] * (1 - p)
				if j > 0:
					dp[i][j] += dp[i - 1][j - 1] * p
		ans = 0
		for i in range(n + 1):
			ans += i * dp[t][i]
		print(ans)
except EOFError:
	pass