A game sells tickets at a price and pays prizes with given chances. Decide whether it is fair and what to expect over many tickets.
What to build
A program that judges a game by its expected value.
Requirements
- Print
expected payout X,expected gain per ticket Yandexpected gain over T tickets Z, all to 2 decimal places. - Then print
verdict fairwhen the expected gain is 0 (within 0.000000001),verdict favourablewhen it is positive andverdict unfavourablewhen it is negative.
Input
Line 1: the ticket price and the number of tickets T. Line 2: n. Then n lines, each a payout and its probability.
Output
Four lines.
Constraints
1 ≤ n ≤ 20; the probabilities add up to 1; 1 ≤ T ≤ 100000.
Example
Input:
2 100
2
10 0.1
0 0.9
Output:
expected payout 1.00
expected gain per ticket -1.00
expected gain over 100 tickets -100.00
verdict unfavourable
Why it works
The expected gain of one ticket is its expected payout minus its price, and by linearity T tickets are expected to gain T times as much.
Examples
Example 1 · Input
2 100 2 10 0.1 0 0.9
Expected output
expected payout 1.00 expected gain per ticket -1.00 expected gain over 100 tickets -100.00 verdict unfavourable
Example 2 · Input
1 50 2 2 0.5 0 0.5
Expected output
expected payout 1.00 expected gain per ticket 0.00 expected gain over 50 tickets 0.00 verdict fair
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The expected gain per ticket is the expected payout minus the price.
Hint 2
Compare the gain with 0, allowing a tiny rounding difference.
Solution
Show one correct solution (try it yourself first)
price, t = input().split()
price, t = float(price), int(t)
n = int(input())
payout = 0.0
for _ in range(n):
value, p = map(float, input().split())
payout += value * p
gain = payout - price
print(f"expected payout {payout:.2f}")
print(f"expected gain per ticket {gain:.2f}")
print(f"expected gain over {t} tickets {gain * t:.2f}")
if abs(gain) <= 1e-9:
print("verdict fair")
elif gain > 0:
print("verdict favourable")
else:
print("verdict unfavourable")