codeforces1055C. Lucky Days

版权声明:转载请注明出。。。不转不是中国人~~~ https://blog.csdn.net/SwustLpf/article/details/84064459

文章目录

题目链接:

http://codeforces.com/contest/1055/problem/C
竟然被这道题卡住了T_T

其实就是能移动gcd(ta,tb)的长度,然后尽量使他们对齐就行了
然后不一定刚好对齐,所以在不多出一点和多出一点这两种情况中选最大就行了

#include"bits/stdc++.h"
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
const int MOD=1e9+7;
LL f(LL l1,LL r1,LL l2,LL r2)
{
	return max(0LL,min(r1,r2)-max(l1,l2)+1);
}
int main()
{
	LL la,ra,ta,lb,rb,tb;
	while(cin>>la>>ra>>ta>>lb>>rb>>tb)
	{
		la++,ra++,lb++,rb++;
		LL d=__gcd(ta,tb);
		LL ans=0;
		if(rb==ra)ans=f(la,ra,lb,rb);
		else if(ra<rb)
		{
			LL t=(rb-ra)/d;
			la+=t*d;
			ra+=t*d;
			//不一定刚好对齐,所以在不多出一点和多出一点选最大 
			ans=f(la,ra,lb,rb); 
			ans=max(ans,f(la+d,ra+d,lb,rb));
		}
		else if(ra>rb)
		{
			LL t=(ra-rb)/d;
			lb+=t*d;
			rb+=t*d;
			ans=f(la,ra,lb,rb);
			ans=max(ans,f(la,ra,lb+d,rb+d));
		}
		cout<<ans<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/SwustLpf/article/details/84064459
今日推荐