Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 500 Accepted Submission(s): 258
Problem Description
Given a,b,c,d, find out the number of pairs of integers (x,y) where a≤x≤b,c≤y≤d and x⋅y is a multiple of 2018.
Input
The input consists of several test cases and is terminated by end-of-file.
Each test case contains four integers a,b,c,d.
Output
For each test case, print an integer which denotes the result.
## Constraint
* 1≤a≤b≤109,1≤c≤d≤109
* The number of tests cases does not exceed 104.
Sample Input
1 2 1 2018 1 2018 1 2018 1 1000000000 1 1000000000
Sample Output
3 6051 1485883320325200
Source
CCPC2018-湖南全国邀请赛-重现赛(感谢湘潭大学)
这个大佬讲的很好,详见:https://blog.csdn.net/EricGipsy/article/details/80409338
#include
int main()
{
long long a,b,c,d;
long long a1,b1,a2,b2,a3,b3;
long long sum;
while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&d))
{
sum=0;
a1=b/2018-a/2018;//区间一中2018的倍数的个数
if(a%2018==0)//注意!!
{
a1++;
}
b1=d/2018-c/2018;//区间二中2018的倍数的个数
if(c%2018==0)//注意!!
{
b1++;
}
sum=a1*(d-c+1)+b1*(b-a+1)-a1*b1;//注意!!a1*b1为区间一和区间二中重复
a2=b/1009-a/1009;//区间一中1019的倍数的个数
if(a%1009==0)//注意!!
{
a2++;
}
b2=d/1009-c/1009;//区间二中1019的倍数的个数
if(c%1009==0)//注意!!
{
b2++;
}
a2=a2-a1;//区间一中1019的倍数的个数-区间一中2018的倍数的个数(即1019的偶数倍数) 可得1019奇数倍数
b2=b2-b1;//区间二中1019的倍数的个数-区间二中2018的倍数的个数(即1019的偶数倍数) 可得1019奇数倍数
a3=b/2-a/2;//区间一中2的倍数的个数
if(a%2==0)//注意!!
{
a3++;
}
b3=d/2-c/2;//区间二中2的倍数的个数
if(c%2==0)//注意!!
{
b3++;
}
sum=sum+b2*(a3-a1)+a2*(b3-b1);//前面的2018倍数个数+区间一1009的奇数倍个数*区间二2的倍数-2018倍数+区间二1009的奇数倍个数*区间一2的倍数-2018倍数
printf("%lld\n",sum);
}
return 0;
}