We need to interpret the problem. It says: "Ankit will give you an integer N (Number of Snacks he brought) and then an array D of floating-point values telling that he thinks ith number of Snack can be faulted with a chances of D[i]. And an array F containing floating-point values denoting that chances if ith Snack is faulted then Overall Snack will be faulted by F[i]."
Then task: "answer him the indexing value you think that can have faulted Snack followed by a space with the probability of the Snack to be faulted."
So we need to output an index i (probably 1-based) and the probability that the snack i is the faulted one? Or something like that.
But the description: "if ith Snack is faulted then Overall Snack will be faulted by F[i]." That is ambiguous.
Let's think: We have N snacks. Each snack has a prior probability D[i] that it "can be faulted"? Actually D[i] is "chances of ith Snack can be faulted". That might be the prior probability that snack i is faulty. Then F[i] is "chances if ith Snack is faulted then Overall Snack will be faulted by F[i]". That sounds like conditional probability: Given that snack i is faulty, the probability that the overall snack (maybe the whole collection?) is faulted? But that doesn't make sense.
Maybe it's a Bayesian inference problem: We have N snacks, each snack has a probability D[i] of being faulty (prior). Then if a snack is faulty, there is a probability F[i] that it causes some observed event (like the team lost a wicket). The overall event (the team lost a wicket) occurred? Actually from story: Ankit ate a bad snack and his team lost a wicket. So the event is that a fault occurred (the snack was bad) and that caused a negative effect. But we need to find which snack is most likely to be the bad one given that some overall fault occurred? The problem says: "Then your task is to answer him the indexing value you think that can have faulted Snack followed by a space with the probability of the Snack to be faulted."
Interpretation: We want to compute the posterior probability that snack i is the faulty one, given that a fault has been observed (the team lost a wicket), and we have prior probabilities D[i] that snack i is faulty (maybe independent?), and conditional probabilities F[i] that if snack i is faulty, then the overall fault occurs (or maybe the event of losing a wicket occurs). But we also need to consider that only one snack can be faulty? Or multiple? Usually these problems are about one being faulty and we need to find which one is most likely given an observation.
The sample: N=3, D = [0.2, 0.3, 0.1], F = [0.5, 0.6, 0.9]. The output: "1 0.486". Let's compute. If we assume that exactly one snack is faulty (i.e., one of them is the bad snack) and the D[i] might be probabilities of being the faulty one (they must sum to 1? They sum to 0.2+0.3+0.1=0.6, not 1. So maybe D[i] is independent probability that each snack is faulty, possibly multiple faulty. But then the event "overall Snack will be faulted" might be interpreted as the event that we detect a fault (maybe after eating, the team loses a wicket). But that depends on which snacks are faulty. Could be complex.
Alternatively, maybe it's a simple formula: For each i, compute D[i] * F[i] normalized? Let's compute:
D[1]F[1] = 0.20.5 = 0.1
D[2]F[2] = 0.30.6 = 0.18
D[3]F[3] = 0.10.9 = 0.09
Sum = 0.37. Posterior probabilities: For i=1: 0.1/0.37 ≈ 0.27027; i=2: 0.486486; i=3: 0.243243. Output says index 1 with probability 0.486. That's not matching. If we pick index 2, that would be ~0.486, which matches the number. So output index 1? Wait output says "1 0.486". That would be index 1 and probability 0.486, but index 1's posterior is 0.270. So not that.
Maybe we compute something like D[i] * (1-F[i])? Or maybe we pick the index with maximum D[i] * (1-F[i])? Let's test: 0.2*(1-0.5)=0.1; 0.3*(1-0.6)=0.12; 0.1*(1-0.9)=0.01. max 0.12 for index 2. Not index 1.
Maybe we compute something like (1-D[i]) * F[i]? 0.80.5=0.4; 0.70.6=0.42; 0.9*0.9=0.81. Max index 3, not 1.
Maybe the output is index 1 with probability 0.486 = (0.20.5)+(0.30.6)+(0.1*0.9) = 0.1+0.18+0.09=0.37? No.
0.486 is approximately 0.486. Could be product of all D[i]? 0.20.30.1=0.006, no.
Maybe it's the probability that the overall snack is faulted? But we need to output an index.
Wait: Sample output: "1 0.486". So index=1, probability=0.486.
Let's compute using Bayes but with a different interpretation: Suppose each snack has a probability D[i] of being "bad". And if it's bad, then there's a probability F[i] that it causes a fault (the team loses). But maybe the event is that a fault happened (the team lost a wicket), and we want the most likely snack that caused it. But we need to output the index and the probability that this snack is the one that caused the fault (i.e., that it is bad and caused fault). But the probability that snack i caused the fault given that a fault occurred is (D[i] * F[i]) / sum_j (D[j] * F[j]). As computed, for i=2 that yields 0.486. So index 2 would be the answer if we choose the max posterior and output that index with its posterior probability. But sample says index 1. Unless they use 0-based indexing? If we use 0-based indexing, index 0 corresponds to first snack with posterior 0.270? No.
Maybe they want the index with the minimum something? Or they want the index that has the highest probability of being faulted? But then they'd output index 2, not 1.
Maybe they want the index that has the maximum value of something like D[i] * (1-F[i])? Or maybe they want the probability that the snack is faulty given that the overall snack is NOT faulted? That would be D[i](1-F[i])/(sum_j D[j](1-F[j])). That yields 0.1,0.12,0.01 sum=0.23, posterior: 0.1/0.23=0.4348, 0.12/0.23=0.5217, 0.01/0.23=0.0435. So index2 ~0.522, not 0.486.
Maybe they want the probability that the snack is faulty given that the overall snack is faulted? Already computed: index2 ~0.486. So that matches the probability number but not index.
Maybe there's a mistake in sample? Or maybe they use 1-based indexing and they output index 1 meaning the first snack, but the probability they output is 0.486, which matches posterior for index 2. Could it be that they sorted or something? No.
Let's carefully read the problem statement: "He will give you an integer N (Number of Snacks he brought) and the an array D of floating-point values telling that he thinks ith number of Snack can be faulted with a chances of D[i]. And an array F containing floating-point values denoting that chances if ith Snack is faulted then Overall Snack will be faulted by F[i].
Then your task is to answer him the indexing value you think that can have faulted Snack followed by a space with the probability of the Snack to be faulted."
Interpretation: "overall Snack will be faulted by F[i]" might mean: if snack i is faulted, then the entire snack (the whole batch) is considered faulted with probability F[i]. Or maybe the "Overall Snack" refers to a meta-snack? Hmm.
Another angle: Perhaps D[i] is the probability that the snack is not faulty? No.
Given the story: Ankit ate a bad snack and his team lost a wicket. He worries that if he eats another bad snack, his team will lose again. He wants to avoid eating bad snacks. So he has N snacks. He has some prior belief about each snack being bad (D[i]). And also some conditional probability that if the snack is bad, then the overall effect (losing wicket) occurs (F[i]). He ate one snack and observed a loss. Now he wants to know which snack is most likely to be the bad one? Or he wants to know for each snack the probability that it is bad? The problem says: "answer him the indexing value you think that can have faulted Snack followed by a space with the probability of the Snack to be faulted." So we need to output a single index and its probability (presumably the one that is most likely to be faulty, or maybe the one with highest risk?).
But the sample output gives index 1 (first snack) with probability 0.486. That probability is close to 0.486, which is also the posterior probability of the second snack in our Bayes calculation. Could it be that they consider the snacks numbered starting from 0? If index 0 is the first snack, then the output "1" would refer to the second snack. But they wrote "1" and in sample they used 3 snacks, they'd likely use 1-based indexing. But many programming problems use 1-based indexing. However, the output "1" could be second if they start from 0? Typically, "1" is first.
Maybe the calculation is not Bayes; maybe we compute for each i, the probability that the snack is faulty after the event that the overall snack is faulted (i.e., after a loss). And we output the index with the maximum probability. That would be index 2 (if 1-based) or index 1 (if 0-based). So if they use 0-based indexing, index 1 corresponds to second snack, and that matches posterior ~0.486. So the output "1" would be 0-based index of the snack with highest posterior. That is plausible: In many programming contest problems, they use 1-indexed inputs but 0-indexed outputs? Usually they specify. Not here. But many Indian problems use 1-indexing for output. However, we can test: If they intended 0-indexed output, they'd output "1" for second snack, and probability 0.486. That matches the Bayes posterior for index 2.
But wait, they said "indexing value you think that can have faulted Snack". That could be 0-based? Usually "indexing value" might refer to array index (0-based in many languages). But the sample shows "1". Could be either.
Let's see if the probability value matches exactly 0.486? Compute posterior for index 2: 0.18 / 0.37 = 0.486486486... rounded to three decimals: 0.486. Yes. So that matches. Posterior for index 1 (0-based) is exactly 0.486 (rounded). So the output is index 1 with probability 0.486.
Thus it seems they output the index (0-based) of the snack with the highest posterior probability, and that probability (rounded to 3 decimals). If there is a tie? Not specified. Probably we output the smallest index? Or any? We'll need to handle ties. Usually they'd output the one with smallest index? Not sure. We'll see.
But we need to verify the model: We have N snacks. Each has a prior probability of being faulty: D[i] (maybe not necessarily summing to 1). Also each has a conditional probability that if it is faulty, then the overall event (the snack being faulted? Actually "Overall Snack will be faulted" might be interpreted as: if snack i is faulted, then the whole set of snacks is considered faulted with probability F[i]. That seems odd.
Alternatively, perhaps the scenario: Ankit eats one snack. He doesn't know which one is bad. He has a belief about each snack being faulty. When he eats a snack, if it's faulty, then there's a chance that his team loses a wicket (the "overall snack" being faulted?). So after observing a loss, he wants to update his belief about which snack he ate? But he knows which snack he ate? Actually he ate a snack and observed a loss. He knows which snack he ate? The story: "Ankit and his friends found that healthier the Snack they eat, their Team leads. So on this concept they are eating alot of Snacks. Suddenly Ankit got that he ate up a bad Snack and his team lost a Wicket." So he realized he ate a bad snack after the team lost a wicket. So he knows that the snack he ate was bad (since it caused loss). But he has many snacks, and he wants to avoid eating another bad snack. He needs help to figure out which snack is most likely to be the bad one? Actually he knows which one he ate? He might have eaten one snack and realized it was bad. But the problem statement: "He will give you an integer N (Number of Snacks he brought) and the an array D of floating-point values telling that he thinks ith number of Snack can be faulted with a chances of D[i]. And an array F containing floating-point values denoting that chances if ith Snack is faulted then Overall Snack will be faulted by F[i]." It seems like he is asking for each snack the probability that it is faulted? But he wants the index you think that can have faulted Snack and its probability.
Maybe it's a classic problem of finding the most likely faulty component given test results. There is known formula: P(component i is faulty | test indicates fault) = (prior_i * sensitivity_i) / sum(prior_j * sensitivity_j). Where sensitivity is probability test indicates fault given component is faulty. And we assume test indicates fault (which we observed). So D[i] is prior probability that component i is faulty (maybe independent but we treat as probability that it is the faulty one among N? Usually prior probabilities sum to 1 if they are mutually exclusive (exactly one faulty). But here they don't sum to 1. Could be that D[i] is the probability that snack i is faulty, and there could be multiple faulty snacks. But then the event "overall snack will be faulted" maybe means that if any snack is faulty, the overall snack is faulted? That would be a system reliability problem. But then we need to compute probability that snack i is faulty given that overall system is faulty? That would be different.
Suppose we have N snacks, each can be faulty independently with probability D[i]. Then the event "Overall Snack will be faulted" might mean that the collection (or the combined effect) is considered faulted, which maybe occurs if at least one snack is faulty? But then F[i] is described as "if ith Snack is faulted then Overall Snack will be faulted by F[i]". That seems like the probability that given snack i is faulty, the overall snack is faulted. But if overall fault occurs if any snack is faulty, then F[i] would be 1 because if snack i is faulty then overall is definitely faulty. But F[i] values are less than 1 (0.5, 0.6, 0.9). So overall fault is not deterministic.
Maybe it's a system where each faulty snack contributes to a fault with some probability, and the overall fault occurs if any of them triggers. But if multiple snacks are faulty, the probability of overall fault could be computed as 1 - product over faulty snacks of (1 - F[i])? That would be complicated.
Simpler: The problem likely expects the Bayesian update where we consider exactly one snack is faulty, and D[i] are the prior probabilities that snack i is the faulty one (they should sum to 1? In sample they sum to 0.6, not 1. So maybe they are not normalized; but in Bayesian update we can treat them as weights (like likelihoods). Actually, if we treat D[i] as the probability that snack i is faulty (maybe independent), we need to consider that there could be zero or multiple faulty snacks. However, the posterior probability that a particular snack i is faulty given that a fault occurred might be computed using inclusion of complementary events. That seems more complicated.
Let's test the possibility: Assume each snack independently has a chance D[i] of being faulty. Also, if a snack is faulty, it has a chance F[i] of causing a fault (like a test detects it). The event we observed is that a fault occurred (overall snack faulted). The fault could be caused by any faulty snack; it's enough that at least one faulty snack triggers its fault. However, it's possible multiple trigger but we just know that fault occurred. Then we want to know which snack is most likely to be faulty? But we need to compute probability that snack i is faulty given fault occurred. That would be:
P(i faulty | fault) = P(i faulty and fault) / P(fault). But P(fault) = 1 - P(no fault). No fault occurs if for every snack either it's not faulty, or it's faulty but doesn't cause fault. So for each snack j, the probability that it does not cause fault = (1 - D[j]) + D[j]*(1 - F[j]) = 1 - D[j]*F[j]. Assuming independence, P(no fault) = ∏_{j} (1 - D[j]*F[j]). So P(fault) = 1 - ∏ (1 - D[j]*F[j]).
Now P(i faulty and fault) = D[i] * F[i] * ∏{j≠i} (1 - D[j]F[j])? Not exactly, because if snack i is faulty, fault could also be caused by other snacks. But we need the probability that snack i is faulty (regardless of others) and fault occurs. That's simply D[i] times probability that fault occurs given that snack i is faulty? Actually P(i faulty ∧ fault) = D[i] * P(fault | i faulty). Given i is faulty, fault occurs if either i causes it (with probability F[i]) or any other snack causes it. So P(fault | i faulty) = 1 - P(no fault from any snack given i faulty). Given i faulty, for j≠i, the probability that j does not cause fault is (1 - D[j]) + D[j](1 - F[j]) = 1 - D[j]*F[j] (since independence of j's state). For i itself, even if faulty, it may not cause fault: probability that i does not cause fault given i faulty = 1 - F[i]. So P(no fault | i faulty) = (1 - F[i]) * ∏{j≠i} (1 - D[j]*F[j]). Therefore P(fault | i faulty) = 1 - (1 - F[i]) * ∏{j≠i} (1 - D[j]*F[j]). So P(i faulty ∧ fault) = D[i] * [1 - (1 - F[i]) * ∏{j≠i} (1 - D[j]*F[j])].
That's messy. But maybe they assume that only one snack can be faulty? That simplifies to the Bayes formula we used. That matches sample if indexing is 0-based. And D[i] would then be prior probabilities (may not sum to 1, but they are treated as likelihoods? Actually in Bayesian inference with mutually exclusive hypotheses, prior probabilities must sum to 1. But they gave D = [0.2,0.3,0.1] summing to 0.6, not 1. So maybe they are not priors but something else? Could be that D[i] is the probability that snack i is faulted, but there is also a probability that none are faulty. However, in the "exactly one faulty" scenario, we can normalize D to sum to 1: normalized priors would be [0.2/0.6=0.333, 0.5, 0.1667]. Then posterior for index2: (0.50.6)/(0.3330.5+0.50.6+0.16670.9) = (0.3)/(0.1665+0.3+0.15)=0.3/0.6165≈0.486. That's the same as before? Actually earlier we computed with raw D (0.2,0.3,0.1) because denominator sum(D[i]*F[i]) gave 0.37, and posterior for index2 = 0.18/0.37≈0.4865. But with normalized priors, we get 0.3/0.6165≈0.4865. Indeed it's the same because normalization factor cancels: posterior = ( (D[i]/sumD) * F[i] ) / ( sum_j (D[j]/sumD * F[j]) ) = (D[i]*F[i]) / (sum_j D[j]*F[j]). So indeed the posterior is independent of the normalization of D, as long as we treat the priors as proportional to D[i]. So D[i] can be unnormalized weights. That's fine. So the model: Hypotheses: exactly one snack is faulty; prior probability that snack i is the faulty one is proportional to D[i] (i.e., P(H_i) = D[i] / sum D). But we don't need to compute sum D because it cancels.
Now we observe evidence: the overall snack is faulted (i.e., a fault is detected). Given H_i (snack i is faulty), the probability of this evidence is F[i] (since if snack i is faulty, then overall snack is faulted with chance F[i]). Then posterior P(H_i | evidence) = (P(H_i)*F[i]) / sum_j (P(H_j)*F[j]) = (D[i]*F[i]) / sum_j (D[j]*F[j]).
Thus we compute for each i, value = D[i]F[i]. Find the index with maximum value (or maybe maximum posterior probability, which is proportional to D[i]F[i] since denominator common). So the index with max product D[i]F[i] yields the most likely faulty snack. In sample, DF: 0.20.5=0.1, 0.30.6=0.18, 0.1*0.9=0.09. Max is 0.18 at index 1 (0-based) i.e., second snack. The output index is 1. So it matches 0-based indexing.
Thus the problem is: Given arrays D and F, compute for each i, product_i = D[i]*F[i]. Find the index i (0-based) that maximizes product_i. If there are ties, which index to output? Not specified. Typically we output the smallest index. But we can check with examples? Not given. We'll assume smallest index (or maybe any index, but to be safe we pick the smallest index with max product). Then compute the probability = posterior = (D[i]*F[i]) / sum(D[j]*F[j]), rounded to three decimal places.
But we need to confirm with sample: sum = 0.37, posterior for index 1 = 0.18/0.37 ≈ 0.486486 → rounding to three decimals: 0.486. Yes.
Thus the solution: For each test case, read N, then read array D of length N, then array F of length N. Compute products p[i] = D[i] * F[i]. Find max index i (0-based) with maximum p[i] (if multiple, maybe choose smallest). Compute total = sum(p). Then probability = p[max_i] / total. Print (max_i+1?) Wait output index 1 in sample corresponds to 0-based index 1. If we printed 1 (0-based) that would be second snack. But typical problem statements in such contests often expect 1-based indexing. But sample shows output "1" and that matches 0-based index of max. If they wanted 1-based they'd output "2". So they are using 0-based indexing. But they said "indexing value" which might mean array index (starting from 0). However, many Indian problems (especially from Hackathon) might use 1-based. But sample suggests 0-based. Let's check the input: "3" snacks, then "0.2 0.3 0.1" and "0.5 0.6 0.9". They didn't label indices. So output "1" could be index 1 (0-based) or snack number 1 (1-based). Without additional info, we can infer from the fact that the probability matches posterior for index 1 (0-based). So it's likely they expect 0-based indexing. But could it be that they want the index of the snack that is most likely to be faulted, and they number snacks starting from 0? Usually they start from 1. But perhaps because they said "indexing value" it means the array index, which is 0-based in many languages. But they didn't specify. However, in many problems from hackerearth or such, they use 1-indexed output. But this is a custom problem. Let's analyze the story: "He will give you ... an array D ... telling that he thinks ith number of Snack can be faulted with a chances of D[i]." The phrase "ith number of Snack" suggests first snack is 1st. So they probably think indexing starts at 1. Then the output would be "1" meaning the first snack. But that doesn't match our computation. So maybe they meant 0-indexed? Hmm.
Maybe I miscalculated: For snack 1, D=0.2, F=0.5, product=0.1. For snack 2, product=0.18. For snack 3, product=0.09. So the max product is for snack 2. If they output index 2 (1-indexed) that would be "2". But they output "1". So they did not output the max product. Unless they want the index of the snack with minimum product? That would be snack 1 (0.1) -> index 1 (1-indexed) or 0 (0-indexed). Probability for that snack would be 0.1/0.37 = 0.270, not 0.486. So not that.
Maybe they want the index with maximum posterior probability, but they number snacks starting from 0? Then index 1 corresponds to snack 2. So they output 1. That makes sense.
Let's see the problem statement: "Then your task is to answer him the indexing value you think that can have faulted Snack followed by a space with the probability of the Snack to be faulted." The phrasing "indexing value" might be ambiguous. In programming, arrays are often 0-indexed. They might be expecting the array index (starting from 0). Usually they'd say "index (0-based)". But they didn't. However, sample output "1" matches 0-based index of max. I'm leaning that they want 0-based index.
But we need to be sure: Could there be another interpretation where they want the index with maximum D[i]? D: 0.2, 0.3, 0.1. Max D is 0.3 at index 1 (0-based). That also gives index 1. But probability would be something else: maybe they want D[i]? 0.3 not 0.486. So no.
What about index with maximum F? F: 0.5,0.6,0.9 max at index 2 (0-based) which would be 2.
What about max of (1-D[i])*F[i]? That gave max at index 2.
So only max product yields index 1 and probability 0.486.
Thus the solution: For each test case, compute p_i = D_i * F_i, find index with max p_i (if tie, choose smallest index maybe), compute total = sum(p_i), compute prob = p_max / total, round to 3 decimals, output index (0-based) and probability.
But what if total is 0? That would happen if all p_i are zero, i.e., all D_i or F_i are zero. But constraints: D[i], F[i] can be 0.0. So possible all zero. But then probability would be undefined. But maybe they guarantee at least one non-zero? Not specified. Could be that we need to handle division by zero. Possibly they guarantee at least one non-zero product. But to be safe, if total=0, then maybe we output something like index 0 and probability 0? But likely test cases avoid that. Also N≥3, maybe some non-zero.
Now about rounding: "Probability should have three decimal after floating-point." So we need to output with exactly three decimal places, rounding to nearest (with ties? typical rounding half up). We can use printf("%.3f").
Now about tie-breaking: Not specified. In many such problems, if there is a tie, output the smallest index. Or maybe output any index, but to be deterministic we pick smallest index with max.
Now confirm with sample: We computed p = [0.1,0.18,0.09]; max index = 1 (0-based); total = 0.37; prob = 0.18/0.37 = 0.486486... rounding to three decimals: 0.486. Good.
Thus algorithm is straightforward.
But we need to be careful about floating point precision. D and F are floating point. We'll read as double.
Now, is there any twist? The problem says "Note :- To be Eligible for Prizes you have to register and create your home address maptag." That's just advertisement.
Also "Important Update : The problem is based upon a concept of Probability. All candidates are requested to please go through the Problem Statement carefully once again." So indeed it's probability problem, likely Bayes.
Thus we implement.
We'll parse T, then for each test case:
- Read integer N.
- Read line of N floats for D. Could be on one line, but maybe split across lines? Usually they are on one line. Input sample shows them on same line.
- Read line of N floats for F.
- Compute arrays.
Implementation: Use vector D(N), F(N). Read using cin (or scanf). Compute sum = 0.0; max_val = -1.0; max_idx = 0; For i from 0 to N-1: prod = D[i] * F[i]; sum += prod; if prod > max_val + epsilon (to avoid floating errors) but values are simple, maybe direct > works. But better use > with epsilon: if (prod > max_val + 1e-12) then update. Or just use > since numbers are not from calculations but direct reading, but after multiplication there might be small errors. Could also use >= with tie-breaking: we want smallest index for max, so we update only if > max_val (strictly greater). If equal, keep earlier.
-
After loop, compute prob = (max_val / sum). Use double.
-
Output: cout << max_idx << " " << fixed << setprecision(3) << prob << endl;
But careful: max_idx we computed as 0-based. In sample they output "1". So that's fine. But we should ensure that they expect 0-based. If they expect 1-based, we would output max_idx+1. Let's examine typical problem statements. They often say "indexing value" and then sample output "1". If they intended 0-based, they might say "0-based index". They didn't. However, the sample output shows index 1, which is ambiguous. Let's think: In many problems, they ask to output "the index (1-based)" and give example like "1" for first element. But here the first element's product is 0.1, probability 0.270, so not that. So it's not the first element. So it must be 0-based, or they intentionally want the second element but wrote 1? That seems unlikely. They'd check the output carefully. So I think they intend 0-based indexing.
But wait, could there be another interpretation: Maybe they want the index of the snack that is most likely to be faulted after considering both D and F, but maybe they want to output the index of the snack with maximum posterior probability, but the posterior probability is computed as (D[i](1-F[i]))/(sum D[j](1-F[j]))? That gave max at index 1 (0-based) with 0.5217, not 0.486. So no.
What about posterior probability of being not faulty? Not.
Given that the computed probability is 0.486 exactly as product/sum, it's convincing.
Thus solution.
Edge Cases:
-
If sum is 0, division by zero. But if all D[i] or F[i] are zero, then all products zero. Then posterior undefined. Perhaps we then output index 0 and probability 0.000? But maybe constraints avoid that. But we can handle: if sum == 0, then set prob = 0.0.
-
If max_val is 0 but sum>0? Then prob=0.
-
Input may have multiple test cases, need to output each on new line.
Now complexity: O(T*N), trivial.
Implementation details:
Reading: after reading N, we may need to read the rest of the line. Use cin for floats.
Because T up to 25, N up to 30.
We'll write in C++.
Now, confirm rounding: 0.486486 to three decimals is 0.486 (since fourth digit is 4, less than 5). 0.4865 would round to 0.487. So we need to use standard rounding.
Now test with a custom case: Suppose D = [0.5,0.5], F=[1.0,1.0] (though constraint <1.0, but maybe inclusive? It says <1.0, but F[i] could be 0.999... but we can test). Then products: 0.5,0.5, sum=1.0, max at index0 (if we pick first), prob=0.5 -> output "0 0.500". That would be 0-based. If they expect 1-based they'd output "1 0.500". Hard to know.
But the sample uses N=3 and output "1". For 0-based, that's second. For 1-based, it would be first. Since we think second is correct, they used 0-based.
Let's double-check: Could the posterior probability for the first snack be something else? Maybe they compute probability that the snack is faulted given that overall snack is NOT faulted? That would be D[i](1-F[i])/sum(D[j](1-F[j])). For i=0: 0.20.5=0.1, i=1:0.30.4=0.12, i=2:0.1*0.1=0.01 sum=0.23, prob i=1 = 0.12/0.23=0.5217 not 0.486. So not.
Maybe they compute probability that overall snack is faulted and it's due to snack i? That is product/sum, same.
Thus I'm confident.
Now, maybe they want the index with minimum product? No.
Thus final solution.
But hold on: The problem statement says: "Then your task is to answer him the indexing value you think that can have faulted Snack followed by a space with the probability of the Snack to be faulted." Could it be that we need to output the index of the snack that is most likely to be faulty, and the probability that that snack is faulty (i.e., the posterior probability). That's exactly what we reasoned.
Thus answer: compute for each i, P_i = D[i] * F[i] / sum(D[j] * F[j]), pick i with max P_i (or equivalently max D[i]*F[i]), output i (0-indexed) and P_i (rounded to 3 decimals).
Now, confirm with sample: i=1 (0-indexed), P=0.486.
All good.
Now, also note: The problem mentions "the probability of the Snack to be faulted." That probability might be the posterior probability.
Thus implement.
Let's write the code.
Pseudo:
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector D(N), F(N);
for (int i=0; i<N; i++) cin >> D[i];
for (int i=0; i<N; i++) cin >> F[i];
double sum = 0.0, maxp = -1.0;
int idx = 0;
for (int i=0; i<N; i++) {
double p = D[i] * F[i];
sum += p;
if (p > maxp) {
maxp = p;
idx = i;
}
}
double prob = (sum > 0) ? maxp / sum : 0.0;
cout << idx << " " << fixed << setprecision(3) << prob << endl;
}
return 0;
}
But is the index output as integer? Yes.
Now test sample:
Input:
1
3
0.2 0.3 0.1
0.5 0.6 0.9
Our code would output "1 0.486" (since idx=1). Good.
Potential issue: Input may have newlines after N; we use cin which skips whitespace.
Now, consider tie-breaking: if multiple indices have same product, we pick the first due to > (strictly greater). That yields smallest index. That's fine.
Now, confirm with possible ties: if all equal, idx=0. Probability = 1/N? Actually if all products equal, maxp/sum = (product)/(N*product)=1/N. That's fine.
Now, about reading: The arrays are on separate lines but could be multiple lines, but cin ignores whitespace.
Now, regarding constraints: D[i] and F[i] are floating point less than 1.0, could be 0.0.
Now, I'll produce final answer.
But wait: The problem might require 1-indexed output? Could there be another interpretation that yields index 2 for the sample? Let's consider if they want the index of the snack with maximum D[i] (0.3) which is second snack, so index 2 if 1-indexed, but they output 1. So not that. If they want maximum of D[i]*F[i], that is second snack. So if 1-indexed, they'd output 2. So they output 1, so it's 0-indexed.
Thus final.
I'll produce the solution explanation accordingly.
Now I'll craft the final response: explanation of solution, algorithm, complexity, and then code.
Also note that we need to round to three decimals. In C++, we can use with setprecision and fixed.
Now, provide final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<double> D(N), F(N);
for (int i = 0; i < N; ++i) cin >> D[i];
for (int i = 0; i < N; ++i) cin >> F[i];
double total = 0.0, maxProd = -1.0;
int bestIdx = 0;
for (int i = 0; i < N; ++i) {
double prod = D[i] * F[i];
total += prod;
if (prod > maxProd) { // strict > gives the smallest index in case of tie
maxProd = prod;
bestIdx = i;
}
}
double probability = (total > 0) ? maxProd / total : 0.0;
cout << bestIdx << " " << fixed << setprecision(3) << probability << "\n";
}
return 0;
}