CodeForces - 665A (組隊賽第七場)

Buses run between the cities A and B, the first one is at 05:00 AM and the last one departs not later than at 11:59 PM. A bus from the city A departs every a minutes and arrives to the city B in a ta minutes, and a bus from the city B departs every bminutes and arrives to the city A in a tb minutes.

The driver Simion wants to make his job diverse, so he counts the buses going towards him. Simion doesn't count the buses he meet at the start and finish.

You know the time when Simion departed from the city A to the city B. Calculate the number of buses Simion will meet to be sure in his counting.

Input

The first line contains two integers a, ta (1 ≤ a, ta ≤ 120) — the frequency of the buses from the city A to the city B and the travel time. Both values are given in minutes.

The second line contains two integers b, tb (1 ≤ b, tb ≤ 120) — the frequency of the buses from the city B to the city A and the travel time. Both values are given in minutes.

The last line contains the departure time of Simion from the city A in the format hh:mm. It is guaranteed that there are a bus from the city A at that time. Note that the hours and the minutes are given with exactly two digits.

Output

Print the only integer z — the number of buses Simion will meet on the way. Note that you should not count the encounters in cities A and B.

Examples

Input

10 30
10 35
05:20

Output

5

Input

60 120
24 100
13:00

Output

9

Note

In the first example Simion departs form the city A at 05:20 AM and arrives to the city B at 05:50 AM. He will meet the first 5 buses from the city B that departed in the period [05:00 AM - 05:40 AM]. Also Simion will meet a bus in the city B at 05:50 AM, but he will not count it.

Also note that the first encounter will be between 05:26 AM and 05:27 AM (if we suggest that the buses are go with the sustained speed).

題意:給你兩個車站(A,B)發車的間隔時間和到另一個車站所用的時間,再給你一輛汽車S從A站發車時間,問你這輛車在到達B站期間能遇見多少輛從B站發出的汽車(不包括S剛到達B站且B站剛有輛汽車發出,和S剛出發且有輛汽車到達A站)。

開始想推個公式……一直沒搞出來,最後一個小時換個思路,直接模擬一下B發車的過程,每發一輛車,s1++,到達車站A後s1--;但比賽中少判斷髮車的截至時間,會導致23:59後還會繼續發車…………,就一直沒找到這個bug,具體在代碼註釋。

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <cmath>
#include <stack>
#define ll long long
#define ull unsigned long long
#define exp 0.00000001
#define lc d<<1
#define rc d<<1|1
#define mod 1000000007
using namespace std;
const double PI = acos(-1);
const int mxx = 4e6+5;

int main()
{
    int a, ta, b, tb, h, m;
    scanf("%d%d%d%d", &a, &ta, &b, &tb);
    scanf("%d:%d", &h, &m);
    int s1 = 0, s2 = 0;
    int t = h*60+m - 5*60;//可以發車的時間範圍
    int tt = t + ta;//汽車S最晚到達b站的時間
    for (int i = 0; i <= tt; ++i)
    {//如果已經有車且 時間減去b站總的發車間隔時間是b站汽車到達a站的路程時間,且在發車時間範圍內
        if (s1 > 0 && (i-s2)%tb == 0 && i <= t)
            s1--, s2 += b;//車數目減一,s2加上b站發車的間隔時間
        if (i%b == 0 && i != tt && i <= 1139)//b站發車且不是汽車S到達b站的時間且時間小於最大時間
                                               // 比賽時就少加了i<=1139  
            s1++;//車數目加一
    }
    printf("%d\n", s1);

   return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章