2018
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-湖南全国邀请赛-重现赛(感谢湘潭大学)
解题思路:
2018 可以拆分成 20181 12018 21009 10092
所以先数出两个区间内1,2,1009,2018,的倍数。
然后相乘,注意容斥。
2018是2的倍数,也是1009的倍数,所以乘2018的时候要减去1009和2的个数。
AC代码:
#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
const int N = 1e6+10;
ll cnt1[2019],cnt2[2019];
ll sum1,sum2,sum3,sum4;
int main(){
ll a,b,c,d;
while(cin>>a>>b>>c>>d)
{
cnt1[1] = b-a+1; cnt2[1] = d-c+1;
cnt1[2] = b/2 - (a-1)/2; cnt2[2] = d/2 - (c-1)/2;
cnt1[1009] = b/1009 - (a-1)/1009; cnt2[1009] = d/1009 - (c-1)/1009;
cnt1[2018] = b/2018 - (a-1)/2018; cnt2[2018] = d/2018 - (c-1)/2018;
sum1 = cnt1[1]*cnt2[2018];
sum2 = cnt1[2]*(cnt2[1009]-cnt2[2018]);
sum3 = cnt1[1009]*(cnt2[2] - cnt2[2018]);
sum4 = cnt1[2018]*(cnt2[1] - cnt2[2] - cnt2[1009] + cnt2[2018]);
cout<<sum1+sum2+sum3+sum4<<endl;
}
return 0;
}