碎碎念
You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.
Input is given from Standard Input in the following format:
A B C D
Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.
4 9 2 3
2
55 and 77 satisfy the condition.
10 40 6 8
23
314159265358979323 846264338327950288 419716939 937510582
532105071133627368
void coder_solution() {
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long a, b, c, d, e, f, g, h;
cin >> a >> b >> c >> d;
if(a % c == 0) {
e = a / c - 1;
} else {
e = a/ c;
}
if (a % d == 0) {
f = a / d - 1;
} else {
f = a / d;
}
g = d * c / __gcd(d, c);
if (a % g == 0) {
h = a / g - 1;
} else {
h = a / g;
}
cout << b - a + 1 - (b / c - e) - (b / d - f) + (b / g - h);
}
#include <iostream>
using namespace std;
long long GCD(long long x, long long y) { return y ? GCD(y, x%y) : x; }
long long num(long long n, long long c, long long d) {
long long G = GCD(c, d);
long long L = c / G * d;
return n - n / c - n / d + n / L;
}
int main() {
long long a, b, c, d;
cin >> a >> b >> c >> d;
cout << num(b, c, d) - num(a-1, c, d) << endl;
}