Can floating-point underflow cause a WA here?

Revision en3, by Phantom_Dreams, 2025-10-06 02:02:57

I recently upsolved C. Gellyfish and Eternal Violet with this submission, which resulted in an AC verdict.

But I am concerned about this part of my code:

double ans = 0, last = 1;
for(int i = 1; i <= s; i++) last *= q;
for(int i = s; i <= r; i++) {
    ans += last * dp0[r-i][max(1,m-i+s)];
    last *= p * i / (i+1-s);
}
cout << ans << endl;

Basically, last is just a probability representing the current binomial distribution: $$$\binom{i-1}{s-1}\,p^{i-s}\,q^{s}$$$.

I initially computed the probability of last with $$$i=s$$$, which is just $$$\binom{s-1}{s-1}\,p^{0}\,q^{s}$$$, before starting the second loop and transitioning it to the next value of $$$i$$$ every cycle.

What I'm worried about here is that $$$q^s$$$ can be very small (effectively as small as $$$0.01^{4000}$$$ under the constraints of the problem).

This can be problematic since I'm working with double, which makes me question if there is a hack that can break my solution.

Assuming there is a hack that breaks my solution, how can I compute last to avoid running into problems with floating-point underflow?

Tags dp, math, probabilities, combinatorics, floating-point

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English Phantom_Dreams 2025-10-06 21:50:06 102 Tiny change: 't-1315332)' -> 't-1315332).'
en4 English Phantom_Dreams 2025-10-06 02:08:42 12 Tiny change: '$i$ every cycle.\n\nWhat ' -> '$i$ every iteration.\n\nWhat '
en3 English Phantom_Dreams 2025-10-06 02:02:57 0 (published)
en2 English Phantom_Dreams 2025-10-06 01:59:00 1004 Tiny change: 'asically, ~last~ is just a' -> 'asically, `last` is just a'
en1 English Phantom_Dreams 2025-10-06 01:16:08 260 Initial revision (saved to drafts)