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.
题解:容斥定理,分情况讨论
1.当x是2018的倍数时,y是任意值
2.当x是1009的倍数且要抛去2018的倍数的情况时,y是2的倍数
3.当x是2的倍数抛去2018的倍数时,y是1009的倍数
4.x其他情况,y是2008的倍数
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
long long a,b,c,d;
while(~scanf("%lld %lld %lld %lld",&a,&b,&c,&d)){
long long ans = 0;
long long x1,x2,x3,x4,q1,q2,q3,q4;
// 如果x是2018的倍数,y是任意数
x1 = b / 2018 - (a-1) / 2018; // [a,b] 中2018的倍数的个数
q1 = d - c + 1;
ans += x1 * q1;
//如果x是1009的倍数不是2018的倍数,y是2的倍数
x2 = b / 1009 - (a-1) / 1009 - x1;
q2 = d / 2 - (c-1)/2;
ans += x2 * q2;
//如果x是2的倍数不是2018的倍数,y是1009的倍数
x3 = b / 2 - (a - 1) / 2 - x1;
q3 = d / 1009 - (c - 1) / 1009;
ans += x3 * q3;
//如果x是上面情况以外,y必须是2018的倍数
x4 = b - a + 1 - x1 - x2 - x3;
q4 = d / 2018 - (c - 1 ) /2018;
ans += x4 * q4;
cout <<ans <<endl;
}
return 0;
}