We are given: Utkarsh starts at X=0, wants to reach exactly X=N. He can jump 2 steps with probability p = P/100, and jump 3 steps with probability q = 1 - p = 1 - P/100. Note: p and q are probabilities per jump.
We need to compute the probability that after some number of jumps (zero? Starting at 0, to reach N exactly) we land exactly at N. The process: at each step, from current position S, you jump to S+2 with prob p, or S+3 with prob q. We need probability that there exists some sequence of jumps that sums to exactly N. But we are not considering the number of jumps? Actually, it's a stochastic process: He keeps jumping until his position is >= N? The problem statement: "Utkarsh initially stood at X = 0. The teacher asked him to run to X = N. But, to make the process quick, Utkarsh decided that he will make jumps of 2 or 3 steps only, i.e., from X = S he can jump to X = S+2 or X = S+3. Utkarsh decided that he will jump 2 steps with probability P/100 and jump 3 steps with probability 1-P/100. You need to find the probability that he will reach exactly on X = N."
This description could be interpreted as: He continues jumping until he reaches or exceeds N? But then we want exactly at N. Alternatively, it might be that he will keep jumping until he reaches N exactly; if he overshoots, that's a failure? Usually in such problems, we consider the process stops when he reaches exactly N; if he overshoots, he fails (probability 0). But careful: "reach exactly on X = N." So we want probability that he lands exactly at N at some point in the process.
Important: The process is like a random walk on integers with steps 2 or 3, starting at 0. He continues making jumps indefinitely; we need the probability that he ever hits exactly N. Since steps are positive, it's a terminating process: either he hits N exactly (success) or overshoots past N (failure). However, note that he could also land exactly on N after some jumps. But there is no upper bound on number of jumps; but since steps are at least 2, number of jumps is bounded by ceil(N/2) if he never overshoots? Actually, overshoot is possible: he can overshoot N, but then he stops? Usually, if overshoot, he cannot go back; he fails. So the process is: Keep jumping until either you reach exactly N (success) or you exceed N (failure). Then probability of success is what we need. That's a typical hitting probability problem. But the sample: N=5, P=20. p=0.2, q=0.8. There are exactly two sequences of jumps that sum to 5: 2+3 and 3+2. If you overshoot, like 2+2=4 then next jump either 2 ->6 (overshoot) or 3->7 (overshoot). Or 3+3=6 overshoot. So indeed, the probability of exactly hitting 5 is the sum over all sequences of jumps that sum exactly to N of product of probabilities. This is just the probability that a sum of independent steps (each step 2 or 3 with probabilities p,q) equals N. However, note that if we consider infinite process, sequences that overshoot but later maybe come back? No, steps are only positive, so cannot come back. So the event "sum equals N" occurs exactly for finite sequences whose total sum is N. But also there is possibility of hitting N exactly after some jumps, but then if he continues jumping? Usually the process stops at success? The problem likely expects that we consider the first time reaching N or exceeding N; but if he reaches N exactly, he stops; so we only care about first time. But the sum of probabilities over all sequences that end exactly at N, where the process stops at that point, is exactly the probability of ever hitting N exactly. Since overshoots are terminal failures. So it's simply the probability that there exists a sequence of steps that sum exactly to N, and that the random process follows that sequence exactly, and does not overshoot earlier? Actually, careful: In a process where we keep jumping regardless, the probability of ever landing exactly on N is the sum over all finite sequences that sum to N, of the product of probabilities for each step, multiplied by the probability that the process does not overshoot before the end. But overshoot before end means if a prefix sum already exceeds N, then the process would have terminated? But if we consider infinite process, landing on N exactly could happen after overshooting? No, because once you overshoot, you're already > N, and further jumps only increase, so you never come back to N. So if you overshoot at any point, you will never hit N later. So the event of hitting N exactly is equivalent to: there exists a sequence of steps (a1, a2, ..., ak) such that sum_{i=1}^k a_i = N, and for all prefixes j<k, sum_{i=1}^j a_i < N. That's exactly the condition for the process to hit N exactly without having overshot earlier. But any sequence with total sum N automatically has all partial sums < N except possibly if some partial sum equals N before end? If it equals N at an earlier prefix, then that's a success already; but then the sequence continues? In the process, if you hit N earlier, you would stop. So the sequences we consider should be those where the first time the sum equals N is at the last step. But if there is a prefix that equals N, then the process would have stopped there, and the subsequent steps would not occur. So the probability of hitting N is the sum over all sequences where the partial sums are < N until the last step where sum equals N. However, note that there might be sequences that sum to N that have a prefix sum = N (i.e., the last step is zero? But steps are 2 or 3, so partial sums are increasing; they can equal N only at the end; because if a prefix sum equals N, that's the end. So any sequence that sums to N will have all partial sums < N until the final step because each step is at least 2, so if the sum at step k-1 was less than N, adding at least 2 will get to at most N only if step k exactly fills the remainder. But it's possible that some earlier prefix also equals N? That would require that the sum of first m steps equals N, but then the total sum would be > N if there are more steps. So if we consider sequences that sum to N, they cannot have a prefix that equals N and then additional steps, because then total sum > N. So the condition is automatically satisfied. Thus the probability is simply the sum over all finite sequences of 2s and 3s whose sum is exactly N, of the product of probabilities (p for 2, q for 3). So we need to compute:
Probability = sum_{k, composition of N using 2 and 3} p^{count2} q^{count3}.
That's equivalent to: if N can be expressed as 2a + 3b for nonnegative integers a,b, then each such representation corresponds to a sequence of a twos and b threes, and the number of sequences is the number of permutations (a+b choose a). But careful: The probability for a given order is p^a q^b. So the total probability for that representation (a,b) is (number of orders) * p^a q^b = C(a+b, a) p^a q^b.
However, we must sum over all pairs (a,b) such that 2a+3b = N. But wait, there may be multiple representations? For a given N, there may be multiple (a,b) solutions in nonnegative integers to 2a+3b = N. For example N=5: solutions: a=1,b=1 gives 2+3=5; a=?? a=4? 24=8 >5; b only integer: b=0 => a=2.5 not integer; b=1 => a=1; b=2 => a=-0.5; so only (1,1). And there are 2 orders. So total probability 2 * p q = 20.2*0.8=0.32.
Thus the problem reduces to computing:
P_N = sum over all nonnegative integer pairs (a,b) with 2a+3b=N of C(a+b, a) p^a q^b, where p = P/100, q = 1-p.
N up to 1e6, so we need efficient computation.
We need to compute this sum with high precision (6 decimal digits). Since p and q are probabilities, and we will compute double-precision floating point, it should be fine. But careful: p and q are rational numbers with denominator 100, but we can treat as double.
But we must consider also the possibility that N is not reachable at all, i.e., there is no (a,b) such that 2a+3b=N. For some N, like N=1, there is no solution; then probability = 0. Indeed, because you cannot reach 1 with steps of 2 and 3. So answer 0.000000.
Thus we need to sum over all (a,b) that satisfy 2a+3b=N.
We can solve this via dynamic programming: let dp[i] = probability to reach exactly i. Then dp[0] = 1 (starting at 0). For i>=2: dp[i] = pdp[i-2] + qdp[i-3]? Actually careful: The process is not a simple recurrence because the jumps are independent? Actually, dp[i] as defined as probability of ever reaching i exactly (starting from 0) can be expressed via recurrence: To reach i, the last step could be a 2 from i-2 or a 3 from i-3. But note that the process could also have visited i-2 or i-3 earlier and then jumped from there. However, the probability of reaching i is not simply pdp[i-2] + qdp[i-3]? Let's analyze.
We consider the random process that continues until hitting or exceeding the target N? But we want dp[i] as the probability that starting from 0, we ever hit exactly i. But this is a Markov chain with absorbing states? Actually, it's a one-dimensional walk with positive steps. The probability to ever hit i from 0 can be defined. It satisfies: For i>0, the hitting probability h(i) from 0 satisfies: h(0)=1 (trivially, start at 0). For i>0, we condition on the first step: from 0, we jump to 2 with prob p, to 3 with prob q. So then h(i) = p * h_{2}(i) + q * h_{3}(i), where h_{2}(i) is probability to ever hit i starting from 2. But that's like h(i-2) if we shift? Actually, due to space homogeneity: Starting from position x, probability to ever hit i (with i > x) depends only on the difference (i - x), because steps are i.i.d. and the process is translation invariant. So let f(d) = probability to ever hit exactly d steps ahead from current position. Then f(0)=1 (already at target). For d>0, f(d) = p * f(d-2) + q * f(d-3)? Not exactly, because after taking a step, you are at either d-2 or d-3 relative to the target? Let's see: Starting at distance d from target (i.e., current position = target - d). From there, you take a step: with prob p you move 2, so distance becomes d-2; with prob q you move 3, so distance becomes d-3. But note: if the step overshoots, then distance becomes negative, which corresponds to having passed the target; we consider that as failure? Yes, overshoot means we are beyond the target, and then we never hit exactly. So for f(d) to be valid, we need to ensure that when we take a step, if the new distance is exactly 0, that's success; if negative, that's failure (0 probability to eventually hit exactly). So indeed, f(d) satisfies: For d>0, f(d) = p * f(d-2) + q * f(d-3), with the convention that f(negative) = 0, and f(0)=1.
But careful: This recurrence holds because after the first step, if you land exactly at the target (d-2=0 or d-3=0), then f(0)=1, so it correctly counts success. If you overshoot (d-2 <0 or d-3<0), then those terms become 0 because you can't recover. So yes, the probability of ever hitting exactly N starting from 0 is f(N) where f(0)=1, and for d>=1: f(d) = pf(d-2) + qf(d-3), with f(d)=0 for d<0.
But is that equivalent to sum over all sequences? Let's test for N=5: f(1)=0 (since negative for both steps? Actually, f(1)= pf(-1)+qf(-2)=0. f(2)= pf(0)+qf(-1)=p1+q0 = p. f(3)= pf(1)+qf(0)=0 + q1 = q. f(4)= pf(2)+qf(1)= pp + q0 = p^2. f(5)= pf(3)+qf(2)= pq + q*p = 2pq. Works.
Thus the DP recurrence gives the same result. However, note that the DP recurrence sums over all sequences, but also respects that if you overshoot earlier you fail. The recurrence automatically accounts for that because if you go negative, f=0.
So we can compute f[N] for given N up to 1e6 using O(N) time and O(1) memory (since only need last few values). That's efficient.
But we must be cautious: The recurrence uses f(d-2) and f(d-3), so we need to compute for d from 0 to N. Base: f[0]=1. For d=1: f[1] = pf[-1] + qf[-2] = 0. f[2] = pf[0] + qf[-1] = p. f[3] = pf[1] + qf[0] = q. etc.
That seems straightforward.
Now, is there any nuance? The problem may consider that the process stops when reaching N; in our recurrence, we condition on the first step, but we treat f(d) as the probability of ever hitting exactly d starting from 0, which is exactly what we need. So DP works.
But wait: The recurrence f(d) = pf(d-2) + qf(d-3) holds only if the process is homogeneous. However, we must also consider that if we overshoot, it's 0. So yes.
Therefore, we can compute answer as f[N] using DP with double precision.
But we should verify with other examples.
Consider N=6: Reachable via 2+2+2, 3+3. Also sequences: (2,2,2) one ordering; (3,3) one ordering; also (2,2,2) is only one ordering (all 2s). Also (2,2,2) probability p^3; (3,3) probability q^2; also possible: (2,2,2) and (3,3) only? But also 2+2+2 is 6, yes. Also 3+3 is 6. Are there any other combos? 2+? Actually, 2+? 2+? 3? 2+3+? would be 5+? need 1, impossible. So only those two. DP: f(4)=p^2, f(5)=2pq, f(6)= pf(4) + qf(3) = p*(p^2) + q*(q) = p^3 + q^2. That's correct.
N=7: Possible? 2+2+3 (sum 7). Also 2+3+2, 3+2+2; also maybe 3+? 3+? need 4, not possible with 2/3 only? 2+2+2+? =6+? need1 not possible. So only combos: three numbers with two 2's and one 3: number of orders = C(3,1)=3. Also maybe 2+2+3 only. So probability = 3 * p^2 q. DP: f(5)=2pq, f(4)=p^2, f(7)= pf(5)+ qf(4) = p2pq + qp^2 = 2p^2 q + p^2 q = 3p^2 q. Good.
Thus DP works.
Constraints: N up to 1e6, p and q are doubles. DP O(N) is fine.
But we need to consider potential precision issues: if p and q are extreme (0 or 1), DP still works. For p=0, q=1: then only jumps of 3. Then f(N) = 1 if N is divisible by 3, else 0. For p=100, q=0: only jumps of 2, then f(N)=1 if N even, else 0. DP will handle because f(d) = p*f(d-2)+... with p=1, q=0: f(d) = f(d-2) for d>=2, and base f(0)=1. So f(d) = 1 if d even else 0. Works.
Now, we need to output with exactly 6 decimal places.
Potential alternative approach: Compute using combinatorial sums only for reachable N, but DP is simpler and efficient.
But we must double-check if the DP recurrence indeed yields the probability of ever hitting exactly N, not the probability of hitting N at some specific time? The recurrence derived by conditioning on the first step is valid for the probability of eventually hitting N starting from 0, because the process is a Markov chain where we only care about the first step, and due to the positive steps, the future is independent. However, there is a subtlety: The recurrence f(d) = pf(d-2) + qf(d-3) is correct only if we condition on not having already overshot. But in the definition of f(d), we consider starting from a position exactly d units before the target, and we ask for the probability that we will eventually hit the target exactly (without overshooting). The first step: we take a step of size 2 with prob p, moving to distance d-2. If d-2 < 0, then we overshoot, so probability from there is 0. If d-2=0, success. If d-2>0, then we ask f(d-2). Similarly for step of 3. So indeed f(d) = p * (if d-2<0? then 0 else if d-2==0 then 1 else f(d-2)) + similarly. And that's exactly the recurrence with f(negative)=0, f(0)=1. So it's fine.
Therefore, we can implement:
Read N and P.
p = P / 100.0
q = 1.0 - p
If N==0: probability 1? But N>0 as per constraints (0<N). So ignore.
Initialize an array or variables for DP. Since we need only up to N, we could allocate double dp[N+1] but N up to 1e6, that's fine (8 MB). But we can also use three variables sliding. But simpler: allocate vector dp(N+1, 0). dp[0]=1. For i from 1 to N:
if i-2 >= 0: add pdp[i-2]
if i-3 >= 0: add qdp[i-3]
dp[i] = that sum.
But careful: For i=1: i-2 = -1, i-3=-2 => dp[1]=0.
For i=2: pdp[0] + qdp[-1] (dp[-1] treat as 0) => p1 = p.
For i=3: pdp[1] + qdp[0] = p0 + q*1 = q.
Works.
But note: For i >=4, dp[i] = pdp[i-2] + qdp[i-3]. However, we must also consider that if i-2 or i-3 is negative, we skip.
Thus we can compute in O(N). We'll get dp[N] which is the probability.
But double-check for N=0? Not needed.
Now, are there any pitfalls about large N and floating-point underflow? p and q are between 0 and 1. dp[i] may become very small for large N if p and q are such that expected step size >? But since steps are at least 2, the number of jumps to reach N is at most N/2, so the probability of any particular sequence is at most max(p,q)^{N/2}, which for p and q not too small maybe not underflow too much. But N up to 1e6, p^500000 could be astronomically small (like if p=0.5, then p^{500000} = 2^{-500000} ~ 10^{-150000}, which is far below double min positive (~1e-308). So many dp values might underflow to 0. But does that matter? The DP sum might also be extremely small, but the problem expects output with 6 decimal places, which could be 0.000000 if the probability is essentially 0. However, there might be cases where probability is not zero but underflows. Could it be that probability remains within double range? Let's examine. The probability of hitting exactly N is the sum over paths. The number of paths grows, but each path probability decays exponentially. The expected number of steps to reach N is roughly N / expected step = N/(2p+3q). The probability of a specific typical path is p^{a} q^{b}. The total probability is at most 1 obviously. Could it become extremely small? Possibly, if p is very small or very large, the only ways to reach N involve many jumps of the less probable step? For example, if p is extremely small (close to 0), then q close to 1, so most jumps are 3. Then to reach N, we need N divisible by 3 essentially. If N is divisible by 3, probability is essentially 1 (since you always jump 3 and exactly hit). Actually, if q=1, probability is 1 for multiples of 3, else 0. So that's not small. For p small but not zero, say p=0.01, q=0.99. Then to hit N, you might need some 2's. For N that is not a multiple of 3, you'll need some 2's to adjust. For large N, the number of 2's required may be limited (like N mod 3 determines minimal number of 2's). For example, if N ≡ 1 mod 3, you need at least two 2's (since 2+2=4 ≡1 mod 3?) Actually, mod 3: 2 ≡ -1, 3≡0. To get sum ≡ 1 mod 3, need an odd number of 2's? Let's see: 2a+3b ≡ 2a (mod 3) ≡ -a (mod 3). So we need -a ≡ N mod 3 => a ≡ -N mod 3. So a is determined modulo 3. The smallest nonnegative a satisfying that is at most 2. So a is at most something like 2? Actually, the general solution: For a given N, all solutions (a,b) to 2a+3b=N are given by a = N - 3k, b = 2k - N? Let's solve: 2a+3b = N => a = (N-3b)/2 must be integer and >=0 => N-3b even and >=0. b can vary. So a can be large if b small. But generally, there are multiple representations if N large. The number of 2's can be as large as N/2 (when b=0) or as small as minimal a. So there may be many sequences. The total probability could be sum over many terms. Could it be that for certain p, q the sum is extremely small? Possibly if one of p or q is tiny, the most likely paths are those with many of the high-probability steps. But there may be many paths with many low-probability steps, but their contributions may be negligible. The dominant contribution might come from some mix. The total probability may not be extremely small, maybe it's moderate. But let's think about worst-case for underflow: Suppose p is extremely small, e.g., p=0.000001, q=0.999999. Then for N large, to get exactly N, you need some combination. But the most likely way is to use mostly 3's and a few 2's. The probability of a sequence with a few 2's is p^{a} q^{b}. Since q is close to 1, q^b is about 1, and p^{a} with a small (like 1 or 2) is about 1e-6 or 1e-12. That's not extremely small; double can handle 1e-12. But what if p=0.5, q=0.5? Then each path probability is (0.5)^{number of jumps}. Number of jumps ~ N / average step (2.5) ~ 0.4N. So probability per path is about 2^{-0.4N}. For N=1e6, that's 2^{-400,000} ~ 10^{-120,000}, which is astronomically tiny. However, the number of paths grows exponentially as well. In fact, the total probability f(N) might not be that tiny; it's the probability that a random walk with steps 2 and 3 ever hits exactly N. This is like a branching process? Actually, f(N) satisfies a linear recurrence with constant coefficients. It can be expressed in terms of the roots of characteristic equation x^3 - p x - q = 0? Wait, the recurrence is f(n) = p f(n-2) + q f(n-3). This is a linear homogeneous recurrence with constant coefficients (for n>=3). But it's not homogeneous because for n<some base? Actually it's homogeneous for n>=3 because no forcing term. But the initial conditions f(0)=1, f(1)=0, f(2)=p, f(3)=q. The solution will be a linear combination of powers of the roots of characteristic equation: r^3 = p r + q? Let's derive: For n>=3, f(n) - p f(n-2) - q f(n-3)=0. Characteristic equation: λ^3 - p λ - q =0? Actually, we assume f(n) ∝ λ^n, then λ^n = p λ^{n-2} + q λ^{n-3} => λ^3 = p λ + q => λ^3 - p λ - q =0. So there are three roots. One root is λ=1 because 1 - p - q = 0? Since p+q=1, indeed λ=1 is a root: 1 - p - q =0. So the characteristic factors as (λ-1)(λ^2+λ+ (p? actually we can factor). The other roots have magnitude less than 1? Let's check: For p,q in (0,1), the other roots likely have magnitude <1. So as n grows, f(n) tends to a constant? Actually, if λ=1 is a root, then there is a constant solution: f(n)=C. But does f(n) converge to a constant? Let's test numerically: p=0.5,q=0.5. Compute f(0)=1; f(2)=0.5; f(3)=0.5; f(4)=0.25; f(5)=0.5; f(6)=0.5? Actually compute: f(6)=p f(4)+ q f(3)=0.50.5=0.125+0.25=0.375. Not constant. So maybe the coefficient for λ=1 is not dominant? Wait, because initial conditions may cause the solution to be a combination of 1^n and other decaying terms. The general solution: f(n) = Aλ2^n + C*λ3^n. As n→∞, f(n) → A if |λ2|<1 and |λ3|<1. So f(n) tends to a constant A. That constant might be positive? Could it be the probability of ever hitting arbitrarily large distances? This is akin to the "renewal" or "gambler's ruin"? For positive steps with mean step μ = 2p+3q, since μ>2, it's positive drift. The probability of ever hitting exactly a large N might tend to a constant? Actually, think: Starting from 0, what is the probability to ever land exactly on some large integer N? Since the steps are 2 and 3, the set of reachable numbers is all integers >= some threshold except some? With gcd(2,3)=1, all integers >=? Actually, with steps 2 and 3, the Frobenius number is 1, so all integers >= 2 are reachable as sums of 2 and 3? Check: 1 not reachable, 2 reachable, 3 reachable, 4 reachable? 2+2=4 yes, 5 reachable, 6 reachable, 7 reachable, etc. So all N>=2 are reachable. The random walk with positive drift will eventually drift to infinity. The probability to hit a specific large N might converge to a constant >0. Indeed, for a random walk with positive increments, the probability that it ever lands exactly at N is like the stationary distribution of the associated ladder heights? There's known results: For a renewal process with step distribution, the renewal function (expected number of renewals at time n) has limit 1/μ. But here we have probability of at least one renewal (hit) at time n, not expected count. However, in a recurrent renewal process when steps are arithmetic, the renewal probability tends to 1/μ (the renewal theorem). Indeed, for an arithmetic distribution with span d=1 (since gcd=1), the renewal probability u_n = P(there is a renewal at time n) satisfies u_n → 1/μ as n→∞, where μ is the mean step. But careful: Renewal theory considers a renewal process where events occur at sums of i.i.d. increments, starting at 0. The renewal measure u_n = P( there exists some k such that S_k = n). That's exactly our f(n). Under the condition that the step distribution is aperiodic (span 1) and μ finite, the elementary renewal theorem says that u_n → 1/μ as n→∞ (Blackwell's renewal theorem). Since our step distribution has support {2,3}, it's aperiodic because gcd(2,3)=1, so span 1. And μ = 2p+3q = 2p+3(1-p) = 3 - p. Actually, 2p+3(1-p)=2p+3-3p = 3 - p. So μ = 3 - p. Since p ∈ [0,1], μ ∈ [2,3]. So 1/μ ∈ [1/3, 1/2]. So for large N, f(N) tends to 1/μ, which is between about 0.333 and 0.5. That is not extremely small. Therefore, for large N, the probability is moderate. So underflow is not a concern: the DP values will converge to around 0.4, not exponentially small. In fact, f(N) for large N is O(1). So double precision is fine.
Let's verify: For p=0.5, μ = 3 - 0.5 = 2.5, 1/μ = 0.4. Our computed f(6)=0.375, f(8)? Let's compute quickly: f(7)=0.375? Actually f(7)=3p^2 q = 30.250.5=0.375. f(8)= p f(6)+ q f(5)=0.50.375+0.50.5=0.1875+0.25=0.4375. f(9)= p f(7)+ q f(6)=0.50.375+0.50.375=0.375. f(10)= p f(8)+ q f(7)=0.50.4375+0.50.375=0.21875+0.1875=0.40625. It seems to approach 0.4. Good.
Thus DP is safe.
Now, we need to output with 6 digits after decimal. Use printf("%.6lf\n", dp[N]) or similar.
Edge Cases: N=0? Not required. N=1 -> 0.0. N=2 -> p. N=3 -> q. Good.
But wait: Our recurrence as defined f(0)=1. But is starting at 0 considered already reached? Yes, but we need to reach N>0, so fine.
Check sample: N=5, P=20 => p=0.2, dp[5] = 0.32 => matches.
Now complexity: O(N) time, O(1) memory if we use sliding window of size 4 (since we need dp[i-2] and dp[i-3]). But careful: we need dp[i-2] and dp[i-3]; if we iterate i from 0 to N, we can keep an array of 4 doubles and update. But we also need dp[0] for base. So we can use an array of length N+1, but that's okay. But for N=1e6, an array of 1e6+1 doubles (8 bytes each) is 8 MB, which is fine. However, if we want to be memory efficient, we can do sliding. But easiest: allocate vector of size N+1. However, be careful with stack allocation if using local array: maybe use vector.
But we must also ensure the recurrence is correct for all i. Could there be any nuance about stopping when reaching N? In our recurrence, we consider f(d) as the probability of ever hitting exactly d. However, the recurrence f(d) = pf(d-2) + qf(d-3) assumes that after a step, the process continues until hitting or overshooting. But if we land exactly at the target, that's success. If we land at a distance d' >0, we then try to hit from there. That is correct.
But is there any possibility that the process could hit the target at a later step after overshooting? No, overshoot means you passed it, and since steps are positive, you can't go back. So it's correct.
Thus DP is correct.
Now, think about large N and the recurrence: We can compute using linear recurrence with matrix exponentiation? But O(N) is fine for 1e6. 1e6 operations is trivial.
But we must be careful about using double precision when p and q are rational with denominator 100. Could there be accuracy issues when N is large (1e6) and we iterate 1e6 steps? The recurrence is linear with coefficients p and q in [0,1]. This is stable? Possibly, if we compute dp[i] as pdp[i-2] + qdp[i-3], rounding errors might accumulate. However, the values are bounded (0 to 1). The recurrence is essentially a linear homogeneous recurrence with one eigenvalue equal to 1 and others less than 1 in magnitude. The component along eigenvalue 1 is constant (the steady-state). The DP should converge to that. Numerical errors may cause slight deviation but should be within 1e-9 after 1e6 steps if using double (approx 15 decimal digits). So should be fine.
Alternatively, we could compute using combinatorial sum over all representations, but that might be complicated because number of representations can be O(N) as well (since b can vary). Actually, number of representations is roughly floor(N/2) maybe? For each b from 0 to floor(N/3), if (N-3b) even and nonnegative, then we have a solution. That's O(N) possible b, and for each we need to compute binomial coefficient C(a+b, a) which could be huge and require big integers or logs. So DP is easier.
But we must check: DP is O(N) which is fine.
Now, we should also think about the initial values: f(0)=1. For negative indices, treat as 0. Implementation:
double dp[N+1];
dp[0] = 1.0;
for (int i = 1; i <= N; i++) {
double val = 0.0;
if (i-2 >= 0) val += p * dp[i-2];
if (i-3 >= 0) val += q * dp[i-3];
dp[i] = val;
}
answer = dp[N];
But careful: For i=1: i-2=-1, i-3=-2 => val=0 => dp[1]=0.
For i=2: val = pdp[0] = p.
For i=3: val = pdp[1] + q*dp[0] = 0 + q.
Good.
Now, we need to output with 6 digits after decimal.
Thus solution is straightforward.
We should test with some values.
Test 1: N=2, P=100 (p=1). Then dp[0]=1; dp[2]=p*dp[0]=1. So answer=1.000000. Since only jump 2 and N=2 exactly, probability=1. Good.
Test 2: N=2, P=0 (p=0,q=1). dp[2]=p*dp[0]=0 => answer=0.000000. Indeed, you can't reach 2 with only jumps of 3. But could you reach 2 with jumps of 3? No. So 0.
Test 3: N=3, P=0 => p=0,q=1 => dp[3]=pdp[1]+qdp[0]=0+1=1 => answer=1.000000.
Test 4: N=4, P=0 => p=0,q=1 => dp[4]=pdp[2]+qdp[1]=0+0=0 => answer=0, because only 3-step jumps, you can get 3,6,... can't get 4. Good.
Test 5: N=4, P=100 => p=1,q=0 => dp[4]=pdp[2]=1dp[2]; dp[2]=p*dp[0]=1 => dp[4]=1 => answer=1. Since two jumps of 2: always. Good.
Test 6: N=6, P=50 => p=0.5,q=0.5. dp[6]=pdp[4]+qdp[3]. dp[2]=0.5; dp[3]=0.5; dp[4]=pdp[2]+qdp[1]=0.50.5+0=0.25; dp[5]=pdp[3]+qdp[2]=0.50.5+0.50.5=0.5; dp[6]=0.50.25+0.5*0.5=0.125+0.25=0.375. So answer 0.375.
Check combinatorial: Possibilities: 2+2+2 (1 sequence) prob=0.125; 3+3 (1 sequence) prob=0.25; total=0.375. Good.
Thus DP works.
Now, there might be a nuance: The teacher asked him to run to X=N. Usually he runs until he reaches or passes N? But we assumed exactly. The sample confirms exactly.
Thus implement.
Now, one more thing: Since N can be as large as 10^6, and we might want to use only a few variables: we need dp[i-2] and dp[i-3]. That means we need to keep at least three previous values. But we also need dp[0] for base. We can use an array of size 4 and update cyclically. However, to avoid index confusion, I'll just allocate a vector of size N+1. That's simple and safe. 8 MB is fine. But we might also think about using a deque? No.
But caution: The problem might be in a contest where memory is limited? Usually 8 MB is okay. But let's consider worst-case: N=10^6, double array of 1e6+1 = 8,000,008 bytes ~ 8 MB. That's within typical memory limits (like 256 MB). So it's fine.
However, we might also consider the possibility of using float to save memory, but double recommended for precision.
Thus final solution: read N, P; compute p = P / 100.0; q = 1 - p; vector dp(N+1, 0); dp[0] = 1; for i=1..N compute; output dp[N] with 6 decimal digits.
But we must ensure that for i up to N, we only access indices >=0. So we can do:
if (i >= 2) dp[i] += p * dp[i-2];
if (i >= 3) dp[i] += q * dp[i-3];
Because dp[i] initially 0.
Now, we should test for N=0? Not needed.
Edge: N=1, dp[1] remains 0.
Now, sample passes.
Let's also test for large N to ensure no overflow. Fine.
One more thought: The DP recurrence might be subject to floating-point errors when p and q are not exact binary fractions. But we only need 6 digits, so it's okay.
Now, I want to verify that the DP indeed yields the same as combinatorial sum for all N. For N that are not reachable, dp[N] should be 0. For example, N=1, dp[1]=0. N=4? For p=0.5, dp[4] computed as 0.25, but is 4 reachable? Yes, 2+2. So okay. For N=5, dp[5]=0.32. So good.
But is there any possibility that dp[N] could be slightly positive due to rounding errors for unreachable N? For unreachable N, dp[N] should be exactly 0 because all ways involve negative indices or unreachable combos. But the recurrence might produce non-zero due to rounding? Let's examine: For N=1, we compute dp[1]=0 exactly. For N=4 when p=0? dp[4]=0. So it should remain 0. Since we start with dp[0]=1 and only add multiplications by p or q, if p and q are exactly representable? p = P/100 may not be exact binary fraction (e.g., P=20 => p=0.2, which is not exactly representable in binary). However, when we compute dp[4] for p=0, it's exactly 0 because p=0 exactly (if P=0). But for other p, unreachable N: Are there unreachable N? With steps 2 and 3, all N >= 2 are reachable. Wait, what about N=1? Not reachable. N=0 reachable. That's it. Because Frobenius number for 2 and 3 is 1, so every integer >= 2 is representable as nonnegative combination of 2 and 3. So the only unreachable N >0 is N=1. So dp[1] should be 0. That's fine.
Thus no issue.
Now, is there any nuance about the process stopping when reaching N? In our recurrence, we assume the process continues even after reaching N? Actually, f(d) is defined as probability of ever hitting exactly d. This assumes that we stop upon hitting? But we condition on first step. In deriving f(d) = p f(d-2) + q f(d-3), we considered that if from distance d we take a step to d-2, and if d-2 = 0, then we have success; else we continue. That's exactly what happens if we stop upon success. So it's fine.
Thus solution is DP.
Now, we can also note that the recurrence can be solved in O(log N) using matrix exponentiation, but not necessary.
One more subtlety: The DP as described computes f(N) for each N exactly as the sum over sequences. However, does it correctly handle the possibility of hitting N after some steps where intermediate positions might exceed N? No, because if any intermediate position exceeds N, then the path is invalid. In the recurrence, when we go from distance d to d-2, if d-2 becomes negative, we treat it as 0 probability, thus that path is discarded. So it's correct.
Thus we can proceed.
Now, check for N=2: dp[2]=p, which matches the probability that the first jump is 2. Good.
So final answer: printf("%.6f\n", dp[N]); Use double.
Now, test some random values to see if it matches combinatorial approach for small N.
Let's compute by hand for N=8, p=0.2: We'll compute DP:
p=0.2, q=0.8.
dp[0]=1
dp[1]=0
dp[2]=0.2
dp[3]=0.8
dp[4]=0.2dp[2]=0.04
dp[5]=0.2dp[3]+0.8dp[2]=0.20.8+0.80.2=0.16+0.16=0.32
dp[6]=0.2dp[4]+0.8dp[3]=0.20.04+0.80.8=0.008+0.64=0.648
dp[7]=0.2dp[5]+0.8dp[4]=0.20.32+0.80.04=0.064+0.032=0.096
dp[8]=0.2dp[6]+0.8dp[5]=0.20.648+0.80.32=0.1296+0.256=0.3856
Let's check combinatorial for N=8: Representations of 8 as 2a+3b. Find all nonnegative integer solutions: b=0 => a=4, gives 4 twos. b=1 => 2a+3=8 => 2a=5 => a=2.5 not integer. b=2 => 2a+6=8 => 2a=2 => a=1 => valid: 1 two and 2 threes. b=3 => 2a+9=8 => a negative. So two solutions: (a=4,b=0) and (a=1,b=2). For (4,0): number of sequences = 1 (all twos), prob = p^4 = 0.2^4 = 0.0016. For (1,2): number of sequences = (1+2 choose 1)=3, prob = 3 * p^1 * q^2 = 30.20.64 = 30.128 = 0.384. Sum = 0.3856. Yes matches. Good.
Thus DP works.
Now, time to implement.
Potential improvement: Use array of floats? But double is safe.
Now, check constraints: N up to 10^6, but also P integer 0..100. So p rational.
Now, about input: First line contains two integers N and P. So read them as ints.
Now, output: Exactly 6 digits after decimal.
Thus final code in C++:
#include
#include
#include
using namespace std;
int main() {
int N, P;
cin >> N >> P;
double p = P / 100.0;
double q = 1.0 - p;
vector dp(N+1, 0.0);
dp[0] = 1.0;
for (int i = 1; i <= N; ++i) {
if (i >= 2) dp[i] += p * dp[i-2];
if (i >= 3) dp[i] += q * dp[i-3];
}
cout << fixed << setprecision(6) << dp[N] << endl;
return 0;
}
Will this be accurate for N=0? Not needed.
Now, test with sample: Input 5 20 -> p=0.2, q=0.8, dp[5]=? Let's compute code: dp[0]=1, dp[1]=0, dp[2]=0.2, dp[3]=0.8, dp[4]=0.2dp[2]=0.04, dp[5]=0.2dp[3]+0.8dp[2]=0.20.8+0.8*0.2=0.16+0.16=0.32. Output: 0.320000. Good.
Now, maybe we need to consider that the process might stop at N, so we shouldn't consider paths that go through N and then continue? But our recurrence stops at success, as we treat f(0)=1. However, if we consider a scenario where you could hit N exactly and then continue, but we still count it as success? Usually we count reaching exactly N at any time as success, regardless of whether you continue. But our recurrence might double count if you could hit N multiple times? With positive steps, you cannot hit N multiple times because after hitting N, any further step would take you beyond N, so you cannot hit N again. So it's fine.
Thus final answer is DP.
But wait, there is another nuance: The teacher asked him to run to X=N. Perhaps the interpretation is that he will keep jumping until he reaches or exceeds N, and we need probability that he lands exactly on N (not overshoot). That's exactly what we have.
Thus solution is correct.
Now, before finalizing, let's think about the possibility that the DP can be computed faster with matrix exponentiation, but not needed.
Now, one more thought: The recurrence f(n) = p f(n-2) + q f(n-3) is valid for n >= 3, but for n=2 and n=3 we set base accordingly. Our loop works.
Now, we need to consider large N (10^6) and the DP might be a bit heavy if we use vector of size 1e6+1, but it's fine.
Now, double-check for edge P=100, p=1, q=0. Then dp[i] = dp[i-2] for i>=2, and dp[i]=0 for i=1. So dp[odd] = 0, dp[even] = 1. Works.
For P=0, p=0, q=1. Then dp[i] = dp[i-3] for i>=3, and dp[i]=0 for i=1,2. So dp[multiple of 3] = 1, else 0. Works.
Now, we also need to consider that for some N, dp[N] may be very close to but not exactly 0 for unreachable N, like N=1. For p=0.2, q=0.8, dp[1]=0. So no issue.
Thus solution is ready.
Now, will there be any floating point error such that dp[N] slightly deviates from expected? For N=1e6, the recurrence might amplify errors? But the eigenvalues: one is 1, the others have magnitude <1. The component along 1 is constant; the other components decay exponentially. The numerical error might come from rounding in each step. However, the recurrence is linear and stable because the dominant eigenvalue is 1, and the others are less than 1, so errors in the subdominant modes will not grow; they may actually be damped. However, there could be error in the constant component due to round-off. But double precision gives about 15 decimal digits; after 1e6 steps, each multiplication and addition may accumulate error. But since the values are bounded and the recurrence is essentially a convolution, error should be small, maybe on the order of 1e-12 or so. Our output needs only 6 digits, so it's safe.
We can also test numerically: Suppose p=0.5, N=1e6, compute dp iteratively. There might be some drift, but the steady-state is 0.4, and after many steps the DP should converge to that. So error should be within 1e-9. So fine.
Thus final solution.
But wait, is there any trick? Let's think about the recurrence again: f(n) = p f(n-2) + q f(n-3). This is a linear homogeneous recurrence. Could we solve analytically? Not needed.
Now, confirm that this recurrence yields f(N) = sum_{a,b} C(a+b,a) p^a q^b with 2a+3b=N. Does the recurrence imply that? Yes, by induction. So it's fine.
Thus final answer.
Now, about implementation details: Use printf for better control:
printf("%.6f\n", dp[N]);
But note: dp[N] is double.
Now, consider potential integer overflow if we used combinatorial approach with big integers and then multiply by p^a q^b? Not needed.
Thus solution stands.
But hold on: The problem statement: "Utkarsh initially stood at X = 0. The teacher asked him to run to X = N. But, to make the process quick, Utkarsh decided that he will make jumps of 2 or 3 steps only... You need to find the probability that he will reach exactly on X = N." This might also be interpreted as: He will continue jumping until he reaches or exceeds N, and we want the probability that he lands exactly on N (i.e., does not overshoot). That's what we did.
One might argue: What if he overshoots but then later, because he can only go forward, he never returns, so overshoot means failure. So yes.
Thus final answer: DP.
But we need to consider that maybe the process is: He decides to make exactly some number of jumps? No, it says "to make the process quick, Utkarsh decided that he will make jumps of 2 or 3 steps only". It doesn't specify a fixed number of jumps; he continues until he reaches N. So it's as we thought.
Now, one more nuance: "Utkarsh decided that he will jump 2 steps with probability P/100 and jump 3 steps with probability 1-P/100." So at each jump, he randomly chooses 2 or 3 with those probabilities.
Thus DP correct.
Now, implement.
But also note: The DP can be optimized to use O(1) memory. For N up to 1e6, it's fine either way.
However, I'll implement with a vector of size N+1. But if N is very large (like 10^6) and we allocate on the stack as local array (double dp[N+1]) may cause stack overflow (since stack size limited). So better to allocate on heap using vector or using new. vector uses heap. So I'll use vector.
Now, final code.
But I want to consider maybe the answer might need to be modulo something? No, it's probability.
Thus final.
Now, we should test with a few more values to ensure:
N=10, P=20 (p=0.2). Let's compute DP quickly or via code? Manually compute? Already have dp up to 8. Continue:
dp[9] = pdp[7] + qdp[6] = 0.20.096 + 0.80.648 = 0.0192 + 0.5184 = 0.5376
dp[10] = pdp[8] + qdp[7] = 0.20.3856 + 0.80.096 = 0.07712 + 0.0768 = 0.15392
Check combinatorial: Representations for 10: solve 2a+3b=10. b=0 => a=5 => combos: 1 (all twos) prob = p^5 = 0.2^5 = 0.00032. b=1 => 2a+3=10 => 2a=7 -> a=3.5 not integer. b=2 => 2a+6=10 => a=2 => combos: C(4,2)=6? Actually a=2,b=2 => total jumps=4, number of orders = C(4,2)=6, prob = 6 * p^2 q^2 = 6 * 0.04 * 0.64 = 6*0.0256 = 0.1536. b=3 => 2a+9=10 => a=0.5 no. b=4 => 2a+12=10 => a negative. So total prob = 0.00032 + 0.1536 = 0.15392. Yes matches.
Thus DP is correct.
Now, final answer.
One more check: If N is very large, say N=10^6, and p=0.5, dp[N] should be about 0.4. So output something like 0.400000 perhaps. But we need exactly 6 digits. So print as required.
Now, we should consider if DP might have a bug for N < 0? Not.
Now, implement.
Now, final solution explanation: Provide reasoning and DP.
Now, produce final answer. ```cpp
#include
#include
#include
using namespace std;
int main() {
int N, P;
cin >> N >> P;
double p = P / 100.0;
double q = 1.0 - p;
// dp[i] = probability to ever reach exactly i
vector<double> dp(N + 1, 0.0);
dp[0] = 1.0;
for (int i = 1; i <= N; ++i) {
if (i >= 2) dp[i] += p * dp[i - 2];
if (i >= 3) dp[i] += q * dp[i - 3];
}
cout << fixed << setprecision(6) << dp[N] << endl;
return 0;
}