Ride-hailing app~40 min2s · 256 MBtarget O(goal + log MOD) time, O(goal) spacenumber-theory · dp
You work on a ride-hailing app. A driver wants to earn at least goal units today. Ride requests arrive one at a time. With a chance of cancel_percent out of 100, the rider cancels and the driver earns nothing from that request.
Otherwise the ride is paid, and the fare is a whole number from 1 to max_fare, each equally likely. Your team lead asks: "How many requests should a driver expect to receive before reaching the goal?" The answer is a fraction, so we ask for it modulo a prime.
Write expected_trips(goal, max_fare, cancel_percent).
cancel_percent / 100. Otherwise it pays a fare chosen uniformly from 1 to max_fare.goal or more.E be the expected number of requests, counting cancelled ones. It is a fraction a / b in lowest terms.a × b^(-1) modulo 998,244,353, where b^(-1) is the modular inverse of b. The answer is a whole number from 0 to 998,244,352.Input: goal = 3, max_fare = 2, cancel_percent = 50
Output: 499122181The expected number of requests is 9/2. Its value modulo 998,244,353 is 499,122,181, because 2 × 499,122,181 = 9 + 998,244,353.
| Earnings so far | Expected requests still to come |
|---|---|
| 3 or more | 0 |
| 2 | 2 |
| 1 | 3 |
| 0 | 9/2 |
Input: goal = 1, max_fare = 1, cancel_percent = 0
Output: 1The first request is paid and reaches the goal, so exactly one request is expected.
Input: goal = 1, max_fare = 5, cancel_percent = 50
Output: 2Any paid fare reaches the goal, and half of the requests are paid, so two requests are expected.
1 ≤ goal ≤ 100,0001 ≤ max_fare ≤ 100,0000 ≤ cancel_percent ≤ 99Averaging over up to 100,000 fares for each of 100,000 earnings values would take 10 billion steps. But the sum of the next max_fare values moves by one value each time the earnings change by one. A running sum makes each earnings value cost a constant number of steps.