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;
}

猜你喜欢

转载自blog.csdn.net/Kuguotao/article/details/89395402
今日推荐