We need to understand problem: We have a stove that automatically turns off after k minutes after turning on. Julia goes to kitchen every d minutes and turns on stove if it's off. So she checks at times multiples of d? Typically, she goes to kitchen at time d, 2d, 3d, ... If she arrives and the stove is off, she turns it on. It turns on instantly and stays on for exactly k minutes (unless she turns it on again before? Actually if it's already on, she does nothing). Then it automatically turns off after k minutes. So the stove state depends on initial? Probably initially stove is off? Does Julia turn it on at time 0? The problem statement: "Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on. During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly." It doesn't specify initial state. But typically, she starts cooking at time 0? We can assume that at time 0 she is at kitchen? The problem might assume she starts at time 0 by turning on the stove? However, it says: "During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off." So she will periodically check. At time 0, maybe she is there to put the chicken? Probably at time 0, she turns on the stove? The statement doesn't explicitly say that she starts at time 0. But we can model as: The stove may be on at time 0 if she turns it on. Usually in such problems, the process begins at time 0 with stove off, and she arrives at times that are multiples of d, starting at time d? But they gave examples: first example k=3,d=2,t=6 -> output 6.5. Let's parse example.
Example 1: k=3,d=2,t=6.
Interpretation: stove stays on for 3 min after turned on. She visits every 2 minutes, turns on if off.
If we simulate: At time 0? She likely is not there? But maybe she is there. Let's simulate typical interpretation: She starts cooking at time 0, so she will turn on the stove at time 0? But then she goes to kitchen every d minutes. Possibly she is initially at the kitchen, so she can turn on the stove at time 0. Then she will come back at times d,2d,... and check.
But the example description: "In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for . Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for . Thus, after four minutes the chicken will be cooked for . Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready ."
Let's reconstruct: They say "the chicken will be cooked for 3 minutes on the turned on stove" so that suggests initially stove is on (maybe turned on at time 0). Then "after this it will be cooked for ..." It seems they describe that first 3 minutes: stove on, then after that it's off for some time, then she turns it on again.
Let's simulate with assumption: At time 0, stove is off? Actually if she is at kitchen at time 0, she could turn on. However, the problem says she goes every d minutes. It doesn't mention she is initially present. But maybe she is present at time 0 to start cooking. Usually in these problems, we assume she starts at time 0 and can turn on stove immediately. But we need to see from example. Let's simulate manually:
k=3, d=2. Suppose at time 0 she turns on the stove. Then stove remains on for 3 minutes, i.e., until t=3 it's on, then automatically turns off at t=3.
She goes to kitchen every 2 minutes: at t=2,4,6,... At t=2, stove is still on (since on from 0 to 3). So she does nothing. At t=4, stove is off (since turned off at t=3). So she turns it on at t=4. Then it stays on for 3 minutes until t=7. At t=6, she goes: stove is on? At t=6, stove is on (since from 4 to 7), so she does nothing. At t=8, stove off? Actually it turns off at 7, so at t=8, off, she turns on, etc.
Now consider cooking: The chicken requires t minutes of on-time, but if stove off, it cooks at half speed? Actually: "chicken needs t minutes to be cooked on the stove, if it is turned on, and 2t minutes, if it is turned off." So cooking speed: when stove is on, rate = 1/t per minute (i.e., 1 unit of "cooking" corresponds to t minutes on). Actually, if it needs t minutes when on, then cooking progress per minute when on = 1/t. When off, it needs 2t minutes, so progress per minute when off = 1/(2t). So half speed.
We need total time to complete 1 unit of cooking.
Given k=3,d=2,t=6.
Simulate using above schedule: On intervals: [0,3] on; then off [3,4]? Actually from 3 to 4 stove off. At t=4 she turns on, so on [4,7]; then off [7,8]? Actually after 7 off, at 8 she turns on, etc.
But the example description says: "the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for ... Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for ... Thus, after four minutes the chicken will be cooked for ... Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready..."
Let's parse: They say after 3 minutes on, then it is cooked for something? They mention "after this it will be cooked for ." Possibly the fraction? Then "Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for . Thus, after four minutes the chicken will be cooked for . Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready ."
So they seem to indicate that after 3 minutes on, plus 1 minute off, total 4 minutes, cooking progress reaches some amount. Then before the fifth minute she turns on, then after 2.5 more minutes it's done.
Let's compute progress: On speed = 1/6 per minute, off speed = 1/12 per minute. On [0,3]: progress = 3/6 = 0.5. Then they mention "after this it will be cooked for ." That might refer to the cooking progress after the on period? Then they talk about one minute off: presumably the next minute (3 to 4) off: progress = 1/12 ≈0.08333, total after 4 minutes = 0.5+0.08333=0.58333. Then "Before the fifth minute Julia will turn on the stove": At t=4? Actually before the fifth minute she turns on, meaning at t=4? Actually at time 4? But they said "before the fifth minute", meaning at time 4? But in our schedule, she turns on at 4. Then "after 2.5 minutes the chicken will be ready". So from time 4, cooking continues on? Actually if she turns on at 4, stove is on for next 3 minutes, but they say after 2.5 minutes from that point it's done. So total time = 4 + 2.5 = 6.5. That matches output.
Check progress: After 4 minutes, progress = 0.5 + (1)/12 = 0.58333... Remaining progress = 1 - 0.58333 = 0.4166667. On speed = 1/6 ≈0.166667 per minute. So time needed = 0.4166667 / (1/6) = 0.4166667 * 6 = 2.5 minutes. Yes.
Now does she turn on at 4? According to d=2, she visits at multiples of 2: 2,4,6,... At t=2, stove still on, so nothing. At t=4, stove is off (since turned off at 3), so she turns on. So schedule matches.
Thus initial condition: at time 0, stove is on. So we assume she turns on at time 0. But problem statement didn't specify, but likely she starts cooking at time 0 and initially she is present and turns on the stove. Alternatively, we can consider that the stove is turned on at time 0. Usually such problems assume that at time 0, the stove is off, but then she would arrive at time 0? Actually she "goes to the kitchen every d minutes". Does she go at time 0? Usually "every d minutes" might include time 0. But it's ambiguous. However, the example shows that at time 0, stove is on. So we will adopt: At time 0, the stove is turned on (maybe by Julia). So initial state: on. And the automatic turn-off after k minutes will occur at time k if not turned off earlier.
But careful: The stove automatically turns off after k minutes after turning on. So if it's turned on at time 0, it will turn off at time k (unless turned off manually? No, only automatic). Then at multiples of d, Julia checks and turns on if off.
Thus the on/off pattern is periodic after a while? But since k and d are arbitrary (up to 1e18), we need to compute total time to cook the chicken (complete 1 unit of cooking). The chicken is cooking continuously at different rates depending on state.
We can treat the cooking process: total "work" needed is 1. On intervals contribute at rate 1/t, off intervals at rate 1/(2t). Equivalent to on contributes "effective time" of 1 per minute? But we can define "cooking units": on minute contributes 1 unit, off minute contributes 0.5 unit, relative to required t. Actually if we define work per minute as 1 when on, 0.5 when off, then total work needed = t (since on for t minutes gives t work). But careful: If we set on rate = 1 per minute, then t minutes on are needed. Off rate = 0.5 per minute. So total work to be done = t. That is easier: The chicken requires t work units, where on contributes 1 per minute, off contributes 0.5 per minute. So we need to accumulate work = t.
Now we have a cyclic pattern of on/off intervals. We need to compute the time taken to accumulate t work.
Let's define:
-
On intervals: when stove is on, length up to k, but may be truncated if a visit occurs during an on period? Actually once on, it stays on for exactly k minutes, then turns off automatically. If Julia visits during on period, she does nothing. So the on interval always has length k, unless perhaps the process ends before completing the full k? But we need to consider that the final interval may be partial.
-
Off intervals: stove is off until the next visit when Julia turns it on. She visits every d minutes. When she finds it off, she turns it on instantly. So the off period length depends on when the stove turned off relative to the visit schedule.
Specifically: Suppose at some time T0 the stove turns off (or initially off? but we start on). Then next visit times are multiples of d. She goes to kitchen at times that are multiples of d. But what about time 0? It might be considered a visit? In example, at time 0, she presumably turned on. But is time 0 a multiple of d? For d=2, 0 is a multiple of 2, so maybe she also visits at time 0. That would be consistent: she visits at times 0, d, 2d, ... So initial state off? But then she would turn on at time 0. So yes, we can treat that as she visits at time 0 and turns on if off. So pattern: she visits at times n*d for integer n >= 0. At each visit, if stove is off, she turns it on; if on, she does nothing. The stove, once turned on, stays on for exactly k minutes, then turns off automatically at that instant. It does not get turned off by her.
Thus we have a deterministic state machine.
We need to determine the on/off schedule from time 0 onwards, and compute the time needed to accumulate work = t.
We can think of cycles. The pattern of on and off intervals may become periodic after some initial transient? Let's analyze.
Let's denote times of visits: V_n = n*d, n=0,1,2,...
At time 0, she visits, stove is off (initially off, presumably? But if it's off, she turns on. So stove becomes on at t=0+. It will remain on for k minutes, turning off at time k.
But note: The stove can also be turned on at a visit if it's off. So at time 0, if we assume initially off, she turns it on -> on from 0 to k.
At time d, she visits again. The stove may be on or off depending on whether k > d. If k > d, stove is still on, so she does nothing, and it will continue on until time k (if not turned off earlier). If k <= d, stove would have turned off at time k (< d), so at time d it's off, she turns it on, and it will stay on for k minutes, i.e., from d to d+k. However, careful: If it turned off at time k, then between k and d it's off, then at d she turns on.
But what about the auto-off after turning on at d? It will turn off at d+k.
Now, after that, the next visit at 2d, etc.
Thus the pattern depends on k relative to d.
We need a general method to compute total cooking time.
Observe that the process is deterministic and can be described in terms of "phases" between visits.
One approach: The on/off pattern repeats every LCM of something? But since the stove stays on for exactly k minutes, and visits occur every d, the pattern might become periodic after some point, with period possibly L = something like the smallest positive multiple of d that is also a multiple of (something)? Actually consider the times when stove is turned on. At each turn-on event, the stove will stay on for k minutes, then turn off. The next turn-on will occur at the next visit after it turns off.
Let’s define turn-on times: T_i (i=0,1,...). T_0 = 0 (first turn-on). Then the stove stays on during [T_i, T_i + k) (assuming it may be cut short by a later turn-on? Actually once on, it stays on for exactly k minutes, regardless of any visits. But what if a turn-on occurs while stove is already on? That would be redundant, but Julia only turns on if off, so she won't turn on if it's already on. So no overlapping. So the stove is on from T_i to T_i + k, then off until the next turn-on time T_{i+1} which is the first visit time V such that V >= T_i + k and V is a multiple of d? Actually after it turns off at T_i + k, it remains off. At the next visit time that is >= T_i + k, Julia will find it off and turn it on. That visit is at time ceil((T_i + k)/d)*d? But careful: She visits at multiples of d: times d,2d,... If T_i + k is exactly a visit time, then at that moment she is there? The sequence of visits: at times 0,d,2d,... If T_i + k is a multiple of d, then at that exact time, she visits. At that moment, the stove just turned off? The problem says: "The stove switches on and off instantly." And "Julia goes to the kitchen every d minutes and turns on the stove if it is turned off." So if at a visit time the stove is off (including if it turned off at exactly that moment), she turns it on. So the turn-on can happen at the same time as the turn-off. So T_{i+1} = the smallest multiple of d that is >= T_i + k? Actually we must consider if T_i + k is less than or equal to that multiple. However, note that at time T_i + k, it turns off. At the next visit time after that, she will turn it on. But if T_i + k is exactly a multiple of d, then that multiple is a visit time, and at that time the stove will be off (since it turned off at that instant). So she will turn it on at that same time, so T_{i+1} = T_i + k. If T_i + k is not a multiple of d, then the next visit is at some time > T_i + k, and T_{i+1} = ceil((T_i + k)/d)*d.
But is it possible that a turn-on occurs at a time when the stove is still on due to a previous turn-on? That would require that the next visit occurs before T_i + k. But if T_i + k > next visit, then at that visit, the stove is still on, so she does nothing. So that visit does not cause a turn-on. So indeed, the next turn-on occurs at the first visit time at or after T_i + k. Since visits are periodic with period d, the off period length is: L_off = (ceil((T_i + k)/d)*d) - (T_i + k), which is between 0 and d-? Actually if T_i + k is multiple of d, then L_off = 0, else L_off = d - ((T_i + k) mod d).
Thus the cycle from one turn-on to the next: on for k, then off for some duration f_i, then turn-on at next multiple. The off duration f_i is determined by the remainder of (T_i + k) modulo d. But T_i itself is a multiple of d (since turn-on times are always visit times, and initial T_0 = 0 which is multiple of d). Because she turns on only at visit times. So T_i is a multiple of d. Let T_i = a_i * d, where a_i is integer.
Then T_i + k = a_i d + k. The next turn-on time T_{i+1} = ceil((a_i d + k)/d)d = d * ceil(a_i + k/d) = d * (a_i + ceil(k/d))? Not exactly: ceil((a_i d + k)/d) = a_i + ceil(k/d)? Because (a_i d + k)/d = a_i + k/d. Ceil of that is a_i + ceil(k/d) if k/d not integer? Actually careful: ceil(a + x) = a + ceil(x) if a is integer. Since a_i is integer, ceil(a_i + k/d) = a_i + ceil(k/d) if k/d is not an integer? Actually if k/d is integer, then ceil(a_i + integer) = a_i + integer, so it's still a_i + k/d. So generally, ceil(a_i + k/d) = a_i + ceil(k/d) if k/d is not an integer? Wait, consider a_i=0, k=3, d=2: k/d=1.5, ceil = 2. So a_i + ceil(k/d) = 2. That's correct: ceil(0+1.5)=2. But if k/d is integer, say k=4,d=2, k/d=2, ceil=2. Then a_i + ceil(k/d) = a_i+2. ceil(a_i+2)=a_i+2. So it works. However, is it always a_i + ceil(k/d)? Let's test: a_i=1, k=3,d=2 => (a_i d + k)= 2+3=5, /d=2.5, ceil=3, a_i+ceil(k/d)=1+2=3. Yes. So T_{i+1} = d * (a_i + ceil(k/d)). But wait, what about the case when k/d is integer? Then ceil(k/d)=k/d. Then T_{i+1} = d(a_i + k/d) = a_i d + k = T_i + k. That matches because if k is multiple of d, then T_i + k is a multiple of d, so the next turn-on is at that exact time (off duration 0). So yes.
Thus T_{i+1} = d * (a_i + ceil(k/d)). Since a_i = T_i/d.
But this suggests that the off duration f_i = T_{i+1} - (T_i + k) = d * (a_i + ceil(k/d)) - (a_i d + k) = d * ceil(k/d) - k.
Compute: f_i = d * ceil(k/d) - k. That's independent of i! Because it doesn't involve a_i. Indeed, T_{i+1} - T_i - k = d*ceil(k/d) - k. So the off duration is constant for each cycle after the first? But careful: This derivation assumed that T_i is a multiple of d, which holds for i=0 (0). Then we computed T_{i+1} based on T_i. Then T_{i+1} is also a multiple of d (since it's a multiple of d). So by induction, all T_i are multiples of d. And the off period length f = d * ceil(k/d) - k is constant for every cycle. Let's verify with examples.
Example 1: k=3,d=2. ceil(k/d)=ceil(1.5)=2. Then f = 2*2 - 3 = 4-3=1. That matches: after on for 3 minutes, off for 1 minute until next turn-on at time 4 (since 0+3=3, next multiple of 2 >=3 is 4, off 1). Next cycle: starting at T_1=4 (multiple of 2). On for 3: until 7. Next multiple of 2 >=7 is 8, off for 1. So f=1 constant.
Example 2: k=4,d=2. ceil(4/2)=2, f=2*2-4=0. So off duration 0: after on, next turn-on exactly at the same time (since k multiple of d). Actually on for 4, turns off at 4, which is a multiple of 2, so at time 4 she visits? Wait, visits at multiples: 0,2,4,6,... At time 4, she visits and finds off (since turned off at 4), turns on immediately. So off duration 0. So pattern: on intervals contiguous? Actually it would be on from 0 to 4, then immediate on from 4 to 8, etc. So effectively always on. That matches example 2: k=4,d=2, stove always on because after 4 minutes, it turns off but at that moment she turns it on (since 4 is a multiple of 2). So it's effectively on continuously. So f=0.
But wait: What about the initial condition? At time 0, she turns on, okay. So the cycle from turn-on to next turn-on is: on for k, off for f, where f = (d - (k mod d)) mod d? Actually d * ceil(k/d) - k = if k mod d = 0 then f=0 else f = d - (k mod d). Because ceil(k/d) = floor((k+d-1)/d). So f = d * ceil(k/d) - k = if k % d == 0: 0 else: d - (k % d). Yes.
Thus the process is periodic with period P = k + f = k + (d - (k mod d)) if k mod d != 0, else P = k. But note: If f = 0, then period = k. But careful: The cycle from turn-on to next turn-on is exactly: on for k, then off for f, and then next turn-on at time T_i + k + f. Since f = d - (k mod d) if k mod d !=0, else 0. So the cycle length L = k + f.
But is L necessarily a multiple of d? Let's check: if k mod d !=0, then f = d - (k mod d). So L = k + d - (k mod d) = (k - (k mod d)) + d = d * floor(k/d) + d = d*(floor(k/d)+1). So L is a multiple of d. If k mod d =0, then f=0, L = k, which is multiple of d as well? Since if k mod d =0, then k is multiple of d. So L = k is multiple of d. So indeed L is always a multiple of d. That makes sense because turn-on times are multiples of d, and the cycle length from one turn-on to next is a multiple of d (since T_i are multiples of d, and T_{i+1} = T_i + L, and L must be multiple of d to keep T_{i+1} multiple of d). So L = d * ceil(k/d). Actually from T_{i+1} = d * (a_i + ceil(k/d)), so T_{i+1} - T_i = d * ceil(k/d). Indeed L = d * ceil(k/d). Because T_i = a_i d, T_{i+1}= (a_i + ceil(k/d)) d. So L = d * ceil(k/d). But earlier we had L = k+f, and we can check: k+f = k + dceil(k/d)-k = dceil(k/d). Yes consistent. So cycle length L = d * ceil(k/d). However, note that within this cycle, the stove is on for k minutes, and off for f = L - k minutes.
Thus the process is periodic from the very beginning? Let's verify: Starting at time 0, stove on for k minutes, then off for f minutes, then at time L, stove is turned on again (since L is multiple of d). Then repeats. So indeed after time 0, the pattern repeats every L minutes: on for k, off for f, on for k, off for f, ... Provided that at the moment of turn-off, the next visit occurs exactly at the end of off period? Let's simulate: At time 0, on. At time k, off. Then off period until next turn-on at time L. But does Julia visit at some time before L? She visits at multiples of d. Since L = d * ceil(k/d), and k < L (unless f=0, then L=k). The multiples of d between k and L: If f>0, then k is not a multiple of d. The next multiple after k is ceil(k/d)*d = L. So indeed the next visit after time k (which is after stove turns off) is at L. So no intermediate visits. So off period is exactly from k to L. So pattern is consistent.
Thus the process is perfectly periodic with period L, starting at time 0. Within each period, we have an on-interval of length k, and an off-interval of length f = L - k.
But we must confirm initial condition: Is it possible that at time 0, stove is off and she turns on? That's what we assumed. But if she visits at time 0, that is a multiple of d (0), so yes. So pattern holds.
Thus the cooking process consists of repeating cycles of length L = d * ceil(k/d). In each cycle, the stove is on for k minutes, off for f = L - k minutes.
Now, we need to compute total time to accumulate t work units (where on minute gives 1 unit, off gives 0.5). The work per cycle: W_cycle = k * 1 + f * 0.5 = k + 0.5*f.
And the cycle length in time: L.
So if t is large, we can compute number of full cycles needed, and then partial cycle.
But careful: The chicken may finish cooking in the middle of an on or off interval.
Also note: t is the required work, i.e., total "effective minutes" at on-rate. Since t can be up to 1e18, we need to handle large numbers.
We need to compute total cooking time.
Let’s denote required work = t (in units where on contributes 1 per minute, off contributes 0.5 per minute). The actual time needed is T.
We have a periodic pattern. So we can compute the number of full cycles we can complete before reaching t.
Let’s define:
L = d * ceil(k/d). But ceil(k/d) = (k + d - 1) // d in integer division. However, careful: k and d can be up to 1e18, but we can compute ceil(k/d) as (k + d - 1) // d using integer arithmetic if we are careful with overflow? 1e18+1e18-1 = 2e18-1, fits within 64-bit? 2e18 < 2^63 ~9.22e18, so safe in 64-bit signed. But we might use Python big ints anyway.
Alternatively, we can compute f = (d - (k % d)) % d, and L = k + f.
Work per cycle: w = k + f/2.
Time per cycle: L.
Now, we can compute how many full cycles can be completed before the work exceeds t. Since w and L are rational numbers.
Let cycles_full = floor(t / w). But t and w are not necessarily integers; w may be fractional. But we can treat as rational numbers. However, we need high precision.
Better: We can compute the total work after N cycles = N * w. But we want to find maximum integer N such that N * w <= t. Then remaining work = t - N * w.
Then we need to see how much additional time to finish that remaining work: it will be taken from the next cycle, starting with on interval then off. So we check if remaining work <= k (the on part). If so, time needed = remaining work (since on rate 1). Else, time needed = k (full on part) + (remaining_work - k) / 0.5? Because after on part, off part rate 0.5, so the extra work beyond k must be obtained in off interval at half speed. But careful: The off interval has length f. If remaining_work - k <= f * 0.5, then the needed extra time in off = (remaining_work - k) / 0.5 = 2*(remaining_work - k). But if remaining_work - k > f * 0.5, then we would need more than one cycle? But since we defined N as maximum full cycles before t, we should have remaining_work <= w (since otherwise we could do another full cycle). So remaining_work <= w. And w = k + f/2. So remaining_work - k <= f/2. So it will always fit within the off part. So the additional time = min(remaining_work, k) if remaining_work <= k, else k + 2*(remaining_work - k). But we can just compute: If remaining_work <= k: time_add = remaining_work. Else: time_add = k + 2*(remaining_work - k). Since remaining_work - k <= f/2, and off interval length f may be greater than 2*(remaining_work - k)? Actually the off interval length is f, and we need time in off interval = 2*(remaining_work - k). This time must be <= f, because 2*(remaining_work - k) <= 2*(f/2) = f. So it fits. So that's fine.
Thus total time = N * L + time_add.
Edge cases: If w = 0? Not possible because k>=1, so w >0.
But is this always correct? We need to consider if the pattern always starts with on interval. Yes, at time 0, stove is on for k minutes. However, what if the required work t is very small, less than k? Then N=0, remaining_work = t <= k, so time_add = t. So total time = t. That seems correct: the stove is on from start, so cooking at full speed until done.
But wait: Could there be a scenario where the stove is initially off? The problem statement does not specify explicitly that Julia turns on at time 0. However, the example aligns with this assumption. Typically, at time 0, she is present to start cooking and turns on. The problem statement: "During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off." It doesn't mention the initial action. But it is natural to assume that at time 0 she is there and turns it on, because she is starting to cook. Alternatively, we could treat that the first visit is at time 0, so same.
But could there be an alternative interpretation where she first goes to kitchen at time d, not at time 0? The phrase "every d minutes" often implies that she goes at times d, 2d, 3d, ... but not necessarily at 0. In many problems, the first visit is at d. However, example: With k=3, d=2, t=6, if she first visited at 2, then initial state: at time 0, stove is off (maybe she hasn't turned on). She would put chicken on? But she might need to start cooking at time 0. But she might not be present at time 0 to turn on. If she only visits at times 2,4,..., then at time 0, the stove is off, and she is not there to turn on. So the chicken would be cooking from time 0 with stove off? That would mean from time 0 to 2, it's off, cooking at half speed. Then at time 2, she turns on, and it stays on for 3 minutes (until 5), then off at 5, next visit at 4? Actually 4 is before 5, so at 4 she would find it on and do nothing, then at 6 she visits? Let's simulate that scenario: First visit at 2. At time 0-2: off. At time 2, she turns on, on until 5 (since k=3). Then at 5, off. Next visit at 4? Actually if first visit at 2, then visits at 4,6,8,... At time 4, stove is still on (since from 2 to 5). So no action. At time 5, stove turns off. At time 6, she visits and turns on. So on from 6 to 9, etc. So the pattern would be: off [0,2], on [2,5], off [5,6], on [6,9], off [9,10]... That gives a different schedule. Let's compute cooking time for example: k=3,d=2,t=6. Work needed: 6. Off rate=0.5, on=1. Off [0,2]: work = 2*0.5=1. So after 2 min, work=1. Then on [2,5]: 3 min on -> work 3, total after 5 min = 4. Then off [5,6]: 1 min off -> work 0.5, total after 6 min = 4.5. Then on [6,9]... we need total 6, remaining 1.5. On from 6: at rate 1, time needed 1.5. So finish at 7.5. Total time = 7.5, not 6.5. So this does not match example. Thus initial visit must be at time 0. So indeed she visits at time 0. So our interpretation is correct: visits at times 0, d, 2d, ...
Therefore our periodic model stands.
Now we must be careful: Is it always true that the stove is turned on at time 0? At time 0, she visits and finds it off (presumably), so turns it on. So yes.
Now we have a periodic schedule. But we must also consider if the chicken finishes exactly at the end of an off interval or on interval; the formula works.
Now the problem: Given k, d, t, where t is the required work (i.e., minutes on equivalent). Compute total cooking time.
But careful: The problem statement says: "the chicken needs t minutes to be cooked on the stove, if it is turned on, and 2t minutes, if it is turned off." So if stove is on all the time, cooking time = t. If off all the time, cooking time = 2t. In our model, we set work needed = t (on minutes equivalent). Because on contributes 1 unit per minute, off contributes 0.5 per minute. So indeed total work to be done = t.
Now compute L, f.
Let’s denote:
Let ceil_kd = (k + d - 1) // d (integer division). Then L = d * ceil_kd.
Alternatively, compute r = k % d. If r == 0: f = 0, L = k. Else: f = d - r, L = k + f.
Then w = k + f/2.
Now, we need to compute number of full cycles: N = floor(t / w). But t and w are rational. Better to avoid floating point errors.
We can compute using integer arithmetic by scaling.
Let’s represent work in terms of half-units maybe. Since off contributes 0.5 per minute, we can define a base unit of 0.5. Let work needed = 2t (in half-units). On contributes 2 per minute, off contributes 1 per minute. Then we can treat everything as integers if t is integer. Indeed t is integer (given as integer up to 1e18). So 2t is integer.
Similarly, per cycle:
On minutes: k contributes 2 * k half-units.
Off minutes: f contributes 1 * f half-units.
Total half-units per cycle: W_half = 2k + f.
Cycle length L in minutes.
We need to accumulate at least 2t half-units.
So we can compute number of full cycles N = floor((2t) / (2k + f)). Since both numerator and denominator are integers.
But careful: Is it always integer division? Yes, 2t and (2k+f) are integers. So N = (2t) // (2k + f). But is that the correct number of full cycles? Because the work per cycle is exactly (2k+f) half-units. However, note that the total work after N full cycles is N*(2k+f) half-units. This may be less than or equal to 2t. But we need to be careful: If we take N = floor(2t / (2k+f)), then after N cycles, total work = N*(2k+f) <= 2t. Then remaining work in half-units = 2t - N*(2k+f).
Then we need to compute extra time needed to finish remaining half-units.
In extra part, we start with on interval of k minutes (rate 2 half-units per minute), then off interval of f minutes (rate 1 half-unit per minute). But careful: The cycle order: on then off. So after N full cycles, we are at time T = NL. The state at that time is: we are at the start of a new cycle, i.e., stove is turned on at time T (since each cycle ends with off? Let's check: A full cycle consists of on then off. At the beginning of a cycle, the stove is on (just turned on). After the on period, it goes off, and at the end of the off period, the next turn-on occurs exactly at the start of the next cycle. So after completing a full cycle, we end at the moment of turn-on for the next cycle? Actually: start cycle at time T_i (turn-on). Then on for k minutes, then off for f minutes, then at time T_i + L, the stove is turned on again (next turn-on). So if we complete a full cycle (i.e., go through both on and off and stop exactly at the end of off period), then at that moment the stove is about to be turned on? Actually at the exact time T_i + L, the stove is turned on instantly. So if we consider the cycle inclusive of both intervals and then the next turn-on, the state at time T_i+L is on (just turned on). But we might define the cycle as from start of on to start of next on. If we take N full cycles as N repetitions of that pattern, after N cycles we are at time NL, and at that moment the stove is on (since the N-th cycle's off period ends exactly at NL with turn-on). So at time NL, the stove is on. That's consistent.
Thus after N full cycles, we are at the beginning of an on period. So for the remaining work, we first use on minutes, then if needed off minutes.
So we need to compute time_add: if remaining half-units <= 2k, then time = remaining_half / 2? Wait careful: On rate is 2 half-units per minute. So if we need R half-units remaining, and we are in on period, the time needed to cover R half-units if we stay in on is R/2 minutes, but only up to k minutes. So if R <= 2k, then we can cover it all during the on period, time = R/2 (since on gives 2 per minute). However, if R > 2k, then we use all on minutes (k minutes) gaining 2k half-units, then we need R - 2k half-units from off period at rate 1 per minute, so extra time = (R - 2k) minutes. Because off gives 1 half-unit per minute, so minutes needed = (R - 2k) (since half-units). But note off period length is f, which we assume is enough because R <= total per cycle = 2k+f, and we took N such that R <= 2k+f. So indeed if R > 2k, then R - 2k <= f. So total time_add = k + (R - 2k) = R - k? Wait: Let's compute: time_add = on_time + off_time = k + (R - 2k) = R - k? But that would be less than k? For example, R=2k+something, then R - k = k + something, but careful: off_time = (R - 2k) minutes, because each minute gives 1 half-unit. So total time_add = k + (R - 2k) = R - k. But is that correct? If R = 2k + x, with 0 < x <= f, then time_add = k + x. But R - k = (2k + x) - k = k + x. So yes. For R <= 2k, time_add = R/2. So we have two cases.
But we can unify: Let remaining work in original units (t units) be R_orig = t - Nw. But w = k + f/2. Then we can compute similarly: If R_orig <= k, time_add = R_orig; else time_add = k + 2(R_orig - k). That's simpler.
In half-units: R = 2 * R_orig. If R <= 2k, time_add = R/2 = R_orig; else time_add = k + (R - 2k) = k + 2*(R_orig - k) as above.
So we can compute with rational numbers.
But we need to be careful about floating point errors. Since k, d, t up to 1e18, we can compute using integer arithmetic for the division and then compute final result as a double or rational exactly.
But we also need to consider potential large numbers, but double has 53 bits of mantissa (~16 decimal digits), so up to 1e18, integers up to 2^53 ~ 9e15 can be represented exactly. 1e18 > 9e15, so not all integers up to 1e18 can be represented exactly in double. However, we can use 128-bit integers or rational fractions to compute precisely and then output as floating point with required tolerance 1e-9. The relative error requirement is 1e-9, so using double with careful calculations might still be okay because we are performing a few operations. But we must avoid catastrophic cancellation. However, we can compute in high precision using Python's decimal or fractions? Since we are writing solution in Python, we can use integers and fractions to compute exact rational number and then convert to float with high precision. Python's float is double precision; when converting from a rational that may not be exactly representable, the error is at most half-ULP, which for numbers up to 1e18 might be about 0.125? Actually double can represent integers exactly up to 2^53 ≈ 9e15. Beyond that, not all integers are representable exactly. So if we compute a large integer division and then produce a floating result by dividing, we might get rounding errors. However, the answer may be fractional, possibly with denominator 2, 4, etc. For large numbers, the fractional part may be .5, .25, etc. Double can represent those exactly if denominator is power of two and the numerator fits within 53-bit mantissa? Actually any number that is a binary fraction with denominator a power of two can be represented exactly if the numerator fits in mantissa bits after scaling. For example, .5 is exact. But if denominator is not power of two, it's repeating binary, so may have rounding error. But the required precision is 1e-9 relative or absolute, and numbers are up to 1e18, so absolute error may be allowed up to 1e-9? Actually condition: The checker program will consider your answer correct if |x - y| / max(1, |y|) <= 1e-9. So for large y (say up to 1e18), absolute error can be as high as 1e9? Wait, relative error times max(1,|y|). If y is large, say 1e18, then absolute error allowed is up to 1e9? Because |x-y|/max(1,|y|) <= 1e-9 => |x-y| <= 1e-9 * max(1,|y|). If y=1e18, then allowed absolute error is 1e9! That's huge. But typical such problems require high precision, but that formula: Actually many Codeforces problems use "relative or absolute error must not exceed 1e-9", meaning either relative error <= 1e-9 or absolute error <= 1e-9. But they often phrase: "The relative or absolute error must not exceed 10^{-9}". However, the note says: "The checker program will consider your answer correct if |x - y| / max(1, |y|) <= 10^{-9}." That is typical Codeforces. So it's relative error normalized by max(1, |y|). That means if y >= 1, allowed relative error is 1e-9. So absolute error allowed is 1e-9 * y, which for y up to 1e18 is up to 1e9. That's too large; something seems off. Actually typical is that the checker uses max(1,|y|) to avoid division by zero. So if y=1e18, allowed absolute error is 1e9, which is huge, but that would make the problem trivial; but they wouldn't require such precision then. Wait, 1e-9 relative error means 1e-9 * y, which for y=1e18 is 1e9. That's enormous. But such a large allowed error would mean that integer outputs could be off by billions and still be accepted, which is unlikely. Actually I recall typical Codeforces uses absolute or relative error 1e-9: they check with condition |x - y| <= 1e-9 OR |x - y| / |y| <= 1e-9. However, they often say "if the relative or absolute error doesn't exceed 10^{-9}". But then in output specification they sometimes give formula: Let's assume your answer is x and jury's is y. The checker considers correct if |x - y| <= 1e-9 or |x - y| / |y| <= 1e-9. They use "or" to make it tolerant for small y. However, the problem statement says: "Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10 - 9. Namely, let's assume that your answer is x and the answer of the jury is y. The checker program will consider your answer correct if |x - y| / max(1, |y|) <= 10^{-9}." This is a common formula: they divide by max(1,|y|) to avoid division by zero and to treat small numbers. So it's essentially relative error when |y| >= 1, and absolute error when |y| < 1. So for large y, the allowed absolute error is 1e-9 * |y|, which is indeed 1e-9 * y. For y=1e18, that's 1e9, which is huge. That can't be right because then they wouldn't need high precision. But maybe typical answers are not that large? t can be up to 1e18, and cooking time may be up to maybe 2t (since off periods) which could be up to 2e18. So allowed absolute error would be 2e9, which is still huge. But the problem expects answer with many decimals? Usually they require high precision. But given that max(1,|y|) is used, for large y the allowed error is relative. 1e-9 relative error means 1e-9 * y, which for y=2e18 is 2000. That's 2000, still large, but not billions: 2e18 * 1e-9 = 2e9, actually 2e18 * 1e-9 = 2e9 = 2,000,000,000. That's 2 billion. That is huge. That would allow error of billions. But typical solutions use double and that would be fine. However, the problem's sample outputs have decimals like 6.5, 20.0. So they expect exactly .5 etc. But double can represent .5 exactly, so it's fine.
Given that, using double is safe. However, we should still be careful to avoid loss of precision in intermediate steps. But given the tolerance, it's likely fine.
But to be rigorous, we can compute using rational arithmetic exactly with integers and then produce a float or direct division. In Python, we can use fractions or compute as integer numerator and denominator and then use Decimal or just float with division? Float might introduce small error but within tolerance.
But better to compute exactly using integer arithmetic for the integer part and then compute remainder as fraction.
Since answer can be fractional .5 etc., we can compute as a rational number exactly: total_time = N * L + extra, where extra = either remaining (if <= k) or k + 2*(remaining) - 2k? Actually as derived.
Let's define:
Let work per cycle in half-units: cycle_work_half = 2k + f.
Let total_work_half = 2t.
Let N = total_work_half // cycle_work_half. (integer division). Let rem_half = total_work_half - N * cycle_work_half.
Now compute extra_time:
If rem_half <= 2k: then extra_time = rem_half / 2.0 (since on speed gives 2 per minute, so minutes = rem_half / 2). Since rem_half is integer, result may be .5 if rem_half odd.
Else: rem_half > 2k, then extra_time = k + (rem_half - 2k) (since off gives 1 per minute). Because rem_half - 2k <= f, integer.
Thus total_time = N * L + extra_time.
We need to output as float with sufficient precision.
But we must verify that N is integer and that we are not missing the possibility that the chicken finishes exactly at the end of an off period. That case is covered: If rem_half == 0, then after N cycles work exactly matches, and extra_time = 0. That yields total_time = NL. At that moment, the stove is turned on exactly at the start of next cycle? Actually after N cycles, we are at time NL, with stove on (just turned on). But work is exactly done, so cooking finishes at that instant. That's fine.
Now we need to check if the pattern might have an initial off period? No, because at time 0 we turn on. So it's correct.
But is there any nuance about the stove being "warm" while off? The problem says: "While the cooker is turned off, it stays warm." That implies the cooking continues at half speed, as given. So our interpretation is correct.
Now we must verify with examples.
Example 1: k=3,d=2,t=6.
Compute r = k % d = 3 % 2 = 1, f = d - r = 1, L = k+f = 4, cycle_work_half = 2k+f = 6+1=7? Wait 2k = 6, plus f=1 => 7. total_work_half = 2t = 12. N = 12 // 7 = 1. rem_half = 12 - 7 = 5. Check: 2k = 6, rem_half=5 <= 6, so extra_time = rem_half / 2 = 2.5. total_time = N*L + extra = 4 + 2.5 = 6.5. Good.
Example 2: k=4,d=2,t=20.
k%d=0 => f=0, L=k=4? Wait L = k + f = 4, but also dceil(k/d)=2ceil(4/2)=22=4. cycle_work_half = 2k+f = 8. total_work_half = 40. N = 40 // 8 = 5. rem_half = 0. total_time = 54 = 20.0. Good.
Another test: k=5,d=3,t=10.
Compute r=5%3=2, f=3-2=1, L=5+1=6. cycle_work_half=2*5+1=11. total_work_half=20. N=20//11=1, rem=9. 2k=10, rem=9 <=10, so extra = 9/2=4.5. total=6+4.5=10.5. Let's simulate quickly: on 5 minutes (work 5), off 1 minute (0.5), total 5.5 in 6 min. Need 10 total work. After 6 min work 5.5, remaining 4.5. Then on again: 5 minutes on would give 5, but we only need 4.5, so time 4.5 on, total time=6+4.5=10.5. Seems plausible.
Now check case where t is small: k=10,d=3,t=5. f = d - (k%d) = 3 - 1 = 2? Actually k=10,d=3: 10%3=1, f=2, L=12. cycle_work_half=20+2=22. total_work_half=10. N=0. rem=10. 2k=20, rem<=20, extra=10/2=5. total=5. So just on for 5 minutes. Since t=5, and stove is on for first 5 minutes (k=10 so on entire time), so time=5. Good.
Now check case where t is exactly w? e.g., k=3,d=2,t=3.5? But t is integer per constraints. So no.
But need to consider when k is very large relative to d? Works fine.
Now we must consider that our derivation of constant f assumed that the next turn-on is at ceil((T_i + k)/d)*d. This yields f = d * ceil(k/d) - k, which is constant. We argued that because T_i is multiple of d, indeed f depends only on k and d, not on i. So cycles are identical. So approach is correct.
Thus algorithm: compute f and L as described. Compute N = (2t) // (2k + f). Compute rem = (2t) % (2k + f). Compute extra_time as:
if rem <= 2k:
extra = rem / 2.0
else:
extra = k + (rem - 2k) # because off minutes needed = rem - 2k, each minute gives 1 half-unit
But note that rem can never exceed 2k + f, and f = L - k. Actually 2k+f = cycle_work_half. And we already have condition rem <= 2k+f. But we branch on rem <= 2k (on only) vs rem > 2k (needs off). So it's fine.
But careful: If f=0, then rem can be up to 2k (since cycle_work_half = 2k). So rem <= 2k always. In that case extra = rem/2, which is correct because there is no off period, only on.
Now total_time = N * L + extra.
But need to ensure that we use floating point for extra, and output with sufficient precision. Since N and L can be up to 1e18, NL can be up to 1e36? Actually k,d,t up to 1e18. L = d * ceil(k/d). In worst case, k=1e18, d=1, then ceil(k/d)=1e18, L = d * 1e18 = 1e18. Actually d up to 1e18, k up to 1e18, so L can be up to dceil(k/d). If d=1, L = 1 * k = k = 1e18. If d is larger, L could be as high as? For k=1, d=1e18, then ceil(k/d)=1, L = d = 1e18. So L <= max(k, d?) Actually L = d * ceil(k/d). Since ceil(k/d) <= (k+d-1)/d, so L <= d * ((k+d-1)/d) = k + d -1. So L <= k + d - 1. So L <= 2e18 - 1. So N can be at most t / w roughly, where w = k + f/2 >= k/2? Actually w >= k/2 because off part may be zero. So N <= 2t/k? But t up to 1e18, k as low as 1, so N could be up to 2e18. Then NL could be up to (2e18) * (2e18) = 4e36, which is huge and beyond 64-bit integer. But can that happen? Let's examine bounds: t up to 1e18, k up to 1e18, d up to 1e18. The worst-case for NL: N = floor(2t/(2k+f)). Since 2k+f >= 2? At minimum, k=1, f can be up to d-1, but d up to 1e18. So 2k+f could be as small as 2? Actually k=1, d could be 1, then f=0, so 2k+f=2. So N could be as large as t (since 2t/2 = t). If t=1e18, N=1e18. L = k+f = 1 + 0 = 1? Actually d=1, f=0, L=1. So NL = 1e18, manageable. But consider k=1, d=1e18. Then r = k%d = 1, f = d - 1 = 1e18 - 1, L = k+f = 1e18. cycle_work_half = 21 + (1e18 - 1) = 1e18 +1? Actually 2 + 1e18 -1 = 1e18 +1. So 2k+f = 1e18+1. 2t = 2e18. N = floor(2e18/(1e18+1)) = 1 (since 1e18+1 > 1e18? Actually 2e18/(1e18+1) ≈ 1.999..., floor = 1). So N=1, N*L = 1e18. So not huge.
What about k=1e18, d=1? f=0, L=k=1e18, cycle_work_half=2e18, N = floor(2t / (2e18)) <= 1, since t <= 1e18, 2t <= 2e18, so N is either 0 or 1. So N*L <= 1e18.
Thus NL won't exceed about 2e18? Actually we need to check more systematically: N = floor(2t / (2k+f)). Since 2k+f = L + k? Actually L = k+f, so 2k+f = k + L. Since L >= k? L = dceil(k/d) >= k. So 2k+f = k+L >= 2k? Actually L >= k, so k+L >= 2k. Anyway, N <= 2t / (2k+f) <= 2t / (2k) = t/k. So N <= t/k. Also N <= 2t / (L) because 2k+f = k+L, but not needed. So N * L <= (t/k) * L. But L <= k+d <= k + 1e18. So NL could be up to (t/k)(k+d). If k is small, say k=1, then N <= t, L <= 1+d, which can be up to 1+1e18 ≈ 1e18. So NL <= t(1e18) ≈ 1e36? But wait, if k=1, t=1e18, N <= t = 1e18. L could be as high as d + (d-? Actually L = dceil(k/d) = dceil(1/d) = d1 = d. So L = d. And d up to 1e18. So NL could be up to 1e18 * 1e18 = 1e36. That is huge! But is N actually equal to t when k=1? Let's compute: k=1, d arbitrary. f = (d - (1 mod d)) % d. Since 1 mod d = 1 if d>1, else if d=1, 1%1=0. For d>1, f = d-1. Then cycle_work_half = 21 + f = 2 + (d-1) = d+1. L = 1 + (d-1) = d. So w = (d+1)/2? Actually work per cycle in original units: w = k + f/2 = 1 + (d-1)/2 = (d+1)/2. So each cycle of length d gives work (d+1)/2. That's > d/2. N = floor(t / w) = floor(2t/(d+1)). For t=1e18, d large, say d=1e18, then 2t=2e18, d+1≈1e18, N ≈ 2. Actually floor(2e18/(1e18+1)) ≈ 1? Actually 2e18/(1e18+1) ≈ 1.999..., floor = 1. So N is not t. Because w is about d/2, and t is 1e18, d is 1e18, so only a few cycles needed. So N won't be huge. The worst-case for NL likely occurs when both k and d are small? Let's explore.
We need to bound NL. Since N = floor(2t / (2k+f)) and L = k+f. So NL <= (2t/(2k+f)) * (k+f). Since (k+f) may be larger than (2k+f)/2? Let's compute ratio: (k+f)/(2k+f) =? For f = d - (k mod d) if k mod d !=0 else 0. This ratio is at most 1? Actually 2k+f >= k+f for k>=0? 2k+f >= k+f because k>=0, so ratio <=1. So NL <= 2t * ((k+f)/(2k+f)). But (k+f)/(2k+f) <= 1/2? Not necessarily; if k=0? But k>=1. For fixed k and f, (k+f)/(2k+f) = (k+f)/(2k+f). For f large relative to k, say k=1, f large, then (1+f)/(2+f) ≈ 1 for large f? Actually (1+f)/(2+f) = 1 - 1/(2+f) < 1, approaching 1 as f→∞. So ratio can be close to 1. So NL could be as large as about 2t. But 2t <= 2e18. Wait: If (k+f)/(2k+f) close to 1, then NL <= 2t * ~1 = 2e18. Actually t up to 1e18, so 2t up to 2e18. So NL cannot exceed about 2e18? Let's test worst-case: k=1, f huge, say d=1e18, f ≈ 1e18-1. Then 2k+f ≈ 1e18+1, k+f ≈ 1e18. So ratio ≈ 1e18/(1e18+1) ≈ 1. So NL <= 2t * ratio ≈ 2e18. But N = floor(2t/(2k+f)) ≈ 2e18/(1e18+1) ≈ 1.999..., floor =1. So NL = L ≈ 1e18. That's within 1e18. But consider k=1, d=1? Then f=0, L=1, cycle_work_half=2, N = floor(2t/2)=t, so NL = t = up to 1e18. So NL <= 1e18. Consider k=1e18, d=1, f=0, L=1e18, cycle_work_half=2e18, N <=1, NL <=1e18. So NL seems bounded by max(t, L)? Actually maximum NL could be up to t? Let's try to maximize NL. Since each cycle yields work w = k + f/2, and length L = k+f. So the efficiency (work per time) is w/L = (k + f/2)/(k+f) = (