2018
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 507 Accepted Submission(s): 263
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
2018的因子只有1,2,1009,2018。那么我们可以对x进行分类讨论:
奇数且不能整除1009: 1 3 5 7 9 ......... 1009 1009*3 1009*5....
偶数且不能整除2018: 2 4 6 8 .......... 2018 2018*2 2018*3.......
整除2018: 2018 2018*2 2018*3....
奇数且整除1009: 1009 1009*3 1009*5....
很明显,绿色部分重复了,我们需要去重处理一下。时间复杂度O(1)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
LL a,b,c,d;
while(scanf("%lld%lld%lld%lld",&a,&b,&c,&d)==4)
{
LL ans=0;
LL x=0,y=0;
x=(b/1009-(a-1)/1009);
y=(b/2018-(a-1)/2018);
x-=y;
ans+=((b+1)/2-a/2-x)*(d/2018-(c-1)/2018);
ans+=(b/2-(a-1)/2-y)*(d/1009-(c-1)/1009);
ans+=y*(d-c+1);
ans+=x*(d/2-(c-1)/2);
printf("%lld\n",ans);
}
}