Mail.Ru Cup 2018 Round 2 C. Lucky Days (number theory / Shu Pei Theorem)

topic

Alice and Bob fortunate given date range and cycling festival,

Enter three integers la, ra, ta (0≤la≤ra≤ta-1,2≤tb≤1e9), on behalf of Alice [la + k * ta, ra + k * ta] in k> = 0 during the lucky ,

Enter three integers lb, fortunately rb, tb (0≤lb≤rb≤tb-1,2≤tb≤1e9), on behalf of Bob [lb + k * tb, rb + k * tb] on k> = 0 during ,

Seek time of maximum two lucky intersection, that is, the maximum number of consecutive days of public

Ideas sources

https://blog.csdn.net/tianyizhicheng/article/details/85220502

answer

Alice at the i-th day, each section may be added cycle ta, Bob at the j-th day, each section can be coupled loop tb

Shu Pei that the Theorem, x * ta + y * tb must be a multiple of gcd (ta, tb), and i and j indicate the gap can be reduced up to several times dis gcd (ta, tb) of

So, as far as possible so that the two sections are aligned, they are discussed at l2 l1 closest to the left and to the right of l1 and l2 at the nearest of the two cases

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int ans,step,dis;
int la,ra,lena,ta;
int lb,rb,lenb,tb;
int cal(int a,int b,int c,int d)
{
	if(min(b,d)<max(a,c))return 0; 
	return min(b,d)-max(a,c)+1;
} 
int main()
{
	scanf("%d%d%d%d%d%d",&la,&ra,&ta,&lb,&rb,&tb);
	step=__gcd(ta,tb);
	if(la>lb)swap(la,lb),swap(ra,rb),swap(ta,tb);
	dis=lb-la;
	la+=dis/step*step,ra+=dis/step*step;
	ans=max(ans,cal(la,ra,lb,rb));
	la+=step;ra+=step;
	ans=max(ans,cal(la,ra,lb,rb));
	printf("%d\n",ans);
	return 0;
}

 

Published 468 original articles · won praise 53 · views 40000 +

Guess you like

Origin blog.csdn.net/Code92007/article/details/104096508