Given
a,b,c,da,b,c,d, find out the number of pairs of integers
(x,y)(x,y) where
a≤x≤b,c≤y≤da≤x≤b,c≤y≤d and
x⋅yx⋅y is a multiple of
20182018.
InputThe input consists of several test cases and is terminated by end-of-file.
Each test case contains four integers a,b,c,da,b,c,d.
OutputFor each test case, print an integer which denotes the result.
## Constraint
* 1≤a≤b≤109,1≤c≤d≤1091≤a≤b≤109,1≤c≤d≤109
* The number of tests cases does not exceed 104104.
Sample Input
1 2 1 2018 1 2018 1 2018 1 1000000000 1 1000000000Sample Output
3 6051 1485883320325200
考点:容斥原理+基础数论
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
long long a,b,c,d;
while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&d)){
long long ans=0,ans1=b-a+1,ans2=d-c+1;
long long k1=0,k2=0,k3=0,k4=0,k11=0,k22=0;
k1=ans1/1009; if((((b-b%1009)-k1*1009)>=a)) k1++;
k11=ans1/2018;if((((b-b%2018)-k11*2018)>=a)) k11++;
k2=ans2/1009; if((((d-d%1009)-k2*1009)>=c)) k2++;
k22=ans2/2018;if((((d-d%2018)-k22*2018)>=c)) k22++;
k3=ans1/2;if((a%2==0)&&(b%2==0)) k3++;
k4=ans2/2;if((c%2==0)&&(d%2==0)) k4++;
//cout<<k1<<' '<<k2<<' '<<k3<<' '<<k4<<endl;
//cout<<k11<<' '<<k22<<endl;
if(!k11&&!k22) ans=k1*k4+k2*k3;
else if(!k11&&k22) ans=k22*ans1+k4*k1+(k2-k22)*k3;
else if(k11&&!k22) ans=k11*ans2+k1*k4+(k1-k11)*k4;
else ans=k11*ans2+k22*ans1-k11*k22+(k1-k11)*(k4-k22)+(k2-k22)*(k3-k11);
cout<<ans<<endl;
}
}