2018
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 587 Accepted Submission(s): 313
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
题意:给出a,b,c,d,求出有多少对(x,y)满足a<=x<=b,c<=y<=d,x*y是2018的倍数。
2018只有4个因数1,2,1009,2018,可以通过1009和2018求出结果
2018的倍数只可以通过两种情况得到
1 a1*2018*b1
2 a2*1009*b2
对于第二种a一定是奇数,否则和第一种重复,b一定是除2018的倍数之外的偶数,所有只要分别在两区间求出a1*2018,b1,a2*1009,b2的个数并求出结果。
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxm = 100005;
#define ll long long
int main()
{
ll a, b, c, d, ans;
while (scanf("%lld%lld%lld%lld", &a, &b, &c, &d) != EOF)
{
ans = 0;
ll s1, s2, f1, f2, p1, p2;
s1 = b / 2018 - (a - 1) / 2018;
s2 = d / 2018 - (c - 1) / 2018;
ans += s1*(d - c + 1) + s2*(b - a + 1) - s1*s2;
f1 = b / 1009 - (a - 1) / 1009 - s1;
f2 = d / 1009 - (c - 1) / 1009 - s2;
p1 = b / 2 - (a - 1) / 2 - s1;
p2 = d / 2 - (c - 1) / 2 - s2;
ans += f1*p2 + f2*p1;
printf("%lld\n", ans);
}
return 0;
}