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只有两个因子2和1009,于是我们可以单独对x进行讨论
1、x为奇数:a、x为1009的倍数,此时y只能是2的倍数
b、x不为1009的倍数,此时y只能是2018的倍数
2、x为偶数:a、x为2018的倍数,此时y可以为任何数
b、x不为2018的倍数,此时y只能是1009的倍数
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define int long long
signed main()
{
int a,b,c,d;
while(cin>>a>>b>>c>>d)
{
int x1,x2,x1009,x2018;
x1009=b/1009-(a-1)/1009;
x2018=b/2018-(a-1)/2018;
x2=b/2-(a-1)/2;
x1=b-a+1-x2;
x1009-=x2018;
x1-=x1009;
x2-=x2018;
int y2,y,y2018,y1009;
y1009=d/1009-(c-1)/1009;
y2018=d/2018-(c-1)/2018;
y=d-c+1;
y2=d/2-(c-1)/2;
printf("%lld\n",x1009*y2+x1*y2018+x2018*y+x2*y1009);
}
}