/
Time Limit: 2 sec / Memory Limit: 1024 MiB
問題文
2 つの信号、信号 1 と信号 2 があり、信号 i (i=1,2) は B_i 秒の青信号と R_i 秒の赤信号を繰り返します。
すなわち信号 i がある瞬間に赤信号から青信号に変わったならば、その後の B_i 秒間は青信号であり、その次の R_i 秒は赤信号であり、以下同様に交互に青信号と赤信号を繰り返します。
今、2 つの信号が同時に赤信号から青信号に変わりました。
今から T 秒後までの間に信号 1 と信号 2 の両方が青信号になっている時間は何秒ありますか?
制約
- 1\leq B_i,R_i\leq 100
- 1\leq T\leq 10^4
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
B_1 R_1 B_2 R_2 T
出力
2 つの信号が同時に赤信号から青信号に変わった瞬間から T 秒後までの間に信号 1 と信号 2 の両方が青信号になっている時間は何秒あるかを、整数として出力せよ。
入力例 1
2 3 5 1 10
出力例 1
3
2 つの信号が同時に赤信号から青信号に変わった瞬間から 10 秒間の間にそれぞれの信号は次のように変化します。
- 変わった瞬間から 2 秒後までの間、信号 1 も 信号 2 も青信号である。
- 2 秒後から 5 秒後までの間、信号 1 は赤信号であり、信号 2 は青信号である。
- 5 秒後から 6 秒後までの間、信号 1 は青信号であり、信号 2 は赤信号である。
- 6 秒後から 7 秒後までの間、信号 1 も 信号 2 も青信号である。
- 7 秒後から 10 秒後までの間、信号 1 は赤信号であり、信号 2 は青信号である。
よって、ともに青信号である時間は、変わった瞬間から 2 秒後までの間と6 秒後から 7 秒後までの間をあわせて 3 秒間となります。
入力例 2
100 100 100 100 10000
出力例 2
5000
ともに青信号である時間と赤信号である時間が交互に 100 秒間ずつ繰り返されます。
Problem Statement
There are two traffic lights, light 1 and light 2. Light i (i=1,2) repeatedly shows green light for B_i seconds and red light for R_i seconds.
In other words, if light i turns from red to green at some point, it remains green for the next B_i seconds, and then remains red for the succeeding R_i seconds; this way, it alternates between green and red light indefinitely.
Now, the two lights have simultaneously turned from red to green.
Within the next T seconds, how many seconds do both light 1 and light 2 show green?
Constraints
- 1\leq B_i,R_i\leq 100
- 1\leq T\leq 10^4
- All input values are integers.
Input
The input is given from Standard Input in the following format:
B_1 R_1 B_2 R_2 T
Output
Print the duration, in seconds as an integer, for which both light 1 and light 2 show green within the T seconds right after the two lights simultaneously turn from red to green.
Sample Input 1
2 3 5 1 10
Sample Output 1
3
Within the 10 seconds right after the two traffic lights simultaneously turn from red to green, the lights transition as follows:
- From 0 to 2 seconds after the switch, both light 1 and light 2 are green.
- From 2 to 5 seconds after the switch, light 1 is red and light 2 is green.
- From 5 to 6 seconds after the switch, light 1 is green and light 2 is red.
- From 6 to 7 seconds after the switch, both light 1 and light 2 are green.
- From 7 to 10 seconds after the switch, light 1 is red and light 2 is green.
Therefore, both lights show green from 0 to 2 and from 6 to 7 seconds after the switch, for a total of 3 seconds.
Sample Input 2
100 100 100 100 10000
Sample Output 2
5000
Both lights show green for 100 seconds, followed by red for 100 seconds; this sequence repeats indefinitely.