C. Lucky Days

Mail.Ru Cup 2018 Round 2

C. Lucky Days

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [la;ra][la;ra] are lucky, then there are some unlucky days: [ra+1;la+ta−1][ra+1;la+ta−1], and then there are lucky days again: [la+ta;ra+ta][la+ta;ra+ta] and so on. In other words, the day is lucky for Alice if it lies in the segment [la+kta;ra+kta][la+kta;ra+kta] for some non-negative integer kk.

The Bob's lucky day have similar structure, however the parameters of his sequence are different: lblb, rbrb, tbtb. So a day is a lucky for Bob if it lies in a segment [lb+ktb;rb+ktb][lb+ktb;rb+ktb], for some non-negative integer kk.

Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.

Input

The first line contains three integers lala, rara, tata (0≤la≤ra≤ta−1,2≤ta≤1090≤la≤ra≤ta−1,2≤ta≤109) and describes Alice's lucky days.

The second line contains three integers lblb, rbrb, tbtb (0≤lb≤rb≤tb−1,2≤tb≤1090≤lb≤rb≤tb−1,2≤tb≤109) and describes Bob's lucky days.

It is guaranteed that both Alice and Bob have some unlucky days.

Output

Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.

Examples

input

Copy

0 2 5
1 3 5

output

Copy

2

input

Copy

0 1 3
2 3 6

output

Copy

1

Note

The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.

 题意: 求两人的lucky day最长重合长度

#include <bits/stdc++.h>
using namespace std;
#define ll long long

ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}

//思想:尽可能使两个人的开始点一致, 最后分情况考虑:
//如果 la == lb 那么最长公共部分为min(ra, rb) - la 
//如果 la < lb 或者 la > lb 那么最长公共部分为min(ra, rb) - max(la, lb)
//还有一种情况 如果ta == tb 则要不两人的lucky day永远不会重合,要不只有一部分相交,
//这时候就要移动一下小的那个看看是否有更长的重合部分了 

int main() {
	
	ll La, Ra, Ta, Lb, Rb, Tb;
	scanf ("%lld %lld %lld %lld %lld %lld", &La, &Ra, &Ta, &Lb, &Rb, &Tb);
	ll T = gcd(Ta, Tb); //利用gcd缩短起点的距离 
	
	ll L0 = La%T; //起点 
	Ra = L0 + (Ra - La);  //终点 
	
	ll L1 = Lb%T;
	Rb = L1 + (Rb - Lb);
	ll ans = 0;
	
	{
		ll L = max(L0, L1), R = min(Ra, Rb);
		ans = max(ans, R - L + 1);
	}
	
	{
		ll L = max(L0 + T, L1), R = min(Ra + T, Rb);
		ans = max(ans, R - L + 1);
	}
	
	{
		ll L = max(L0, L1 + T), R = min(Ra, Rb + T);
		ans = max(ans, R - L + 1);
	}
	
	printf ("%lld\n", ans);
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值