PROBLEM SPECIFICATION:
You are given three positive integers 'n' , 'a' and 'b' . In the given range from 1 to 'n' how many distinct numbers are completely divisible by
'a' or 'b' or both?
NOTE: The range includes both 1 and 'n'.
INPUT SPECIFICATION:
You will be given 't' test cases. Each test case will contain three integers 'n' 'a' and 'b'.
OUTPUT SPECIFICATION:
In a single line print the count of numbers that are divisible by 'a' and 'b'.
We need to understand problem statement: "how many distinct numbers are completely divisible by 'a' or 'b' or both?" So essentially count numbers in [1,n] divisible by a or b. But then output spec says: "print the count of numbers that are divisible by 'a' and 'b'." That seems contradictory. Wait: The problem spec initially says "completely divisible by 'a' or 'b' or both?" and then output spec says "print the count of numbers that are divisible by 'a' and 'b'." Which one? The sample: n=24, a=5, b=3. Count divisible by 5 or 3 or both: numbers divisible by 3: 3,6,9,12,15,18,21,24 => 8 numbers. Divisible by 5: 5,10,15,20 => 4 numbers. Divisible by both (i.e., lcm=15): only 15. So union = 8+4-1=11. Sample output is 11. So answer matches union (or). However output spec says "and". That's probably a mistake; they meant "or". Usually this is a typical problem: count numbers divisible by a or b. But maybe they want count of numbers divisible by both? But sample output 11 wouldn't match both (just 1). So it's union. So we need to compute count = floor(n/a) + floor(n/b) - floor(n/lcm(a,b)). However constraints: n up to 10^15, a,b up to 10^6. t up to 100. Must handle cases where a or b = 0: print -1. If both zero? Also print -1. So we check for zero. Also note that a and b could be zero? The constraint says 0 ≤ a<10^6, so a can be 0. Similarly b. So we need to detect if a==0 or b==0, then output -1. Also note that a and b may be large up to 1e6, but lcm can be up to approx 1e12? Actually a,b up to 1e6, lcm could be up to about 1e12 (since lcm(a,b) ≤ ab, which is 1e12). That multiplied by something? We compute n/lcm, but lcm might overflow 64-bit if we compute ab? But ab up to 1e12 fits in 64-bit (signed 64-bit goes up to ~9e18). So we can use 64-bit integer (long long in C++). But careful: ab could be up to 1e12, which is safe. However, we need to compute lcm = a / gcd(a,b) * b. That could overflow 64-bit if a/gcdb > 9e18? Since a,b ≤ 1e6, a/gcdb ≤ a*b ≤ 1e12, safe. So using 64-bit is fine.
But there's nuance: What if a or b is 0? gcd(0,b) is b (if b>0). But lcm(0,b) is typically 0? Actually lcm(0,b) is undefined (some define as 0). But we treat zero case separately: output -1.
Also note: a and b are positive integers? Problem says "positive integers", but then note says if a or b or both are zero print -1. So maybe they are positive normally but input may have zeros. So handle that.
Edge cases: n could be 0? Range from 1 to n, so if n < 1? But n positive? It says positive integers n, a, b. But constraints: n < 10^15, doesn't say >0 explicitly. Typically range from 1 to n, so n must be ≥1. So we can assume n≥1. But if n=0, the range is empty? But they say range includes 1 and n, so n≥1. So okay.
We need to output for each test case the count. So for each test case read n, a, b. Check if a==0 or b==0 -> print -1. Else compute:
count = n/a + n/b - n/lcm(a,b).
But careful: Division truncates for integers. Use integer division.
Now what about large n? Up to 10^15. n/a etc fit in 64-bit.
But wait: The problem statement might have a twist: It says "completely divisible by 'a' or 'b' or both?" and then "print the count of numbers that are divisible by 'a' and 'b'." That's ambiguous. But sample clarifies it's union.
Thus solution: For each test case, if a==0 or b==0: print -1 else compute lcm, then answer.
But note: Could there be overflow computing lcm? a/gcd * b might be > 2^63-1? a,b up to 1e6, product up to 1e12, safe. Actually 1e6*1e6 = 1e12. So fine.
But what about multiple test cases? Input format: first line t, then t lines each with n a b. Or maybe on one line? Sample shows: "1\n24 5 3". So first line t, then each test case line. So we parse accordingly.
Potential pitfalls: Use long long (signed 64-bit). In C++ we can use long long.
Alternatively, we could compute floor division for each part.
Now think about large t up to 100, it's fine.
Thus solution is simple.
But let's also consider the possibility that a or b might be > n? In that case floor(n/a)=0, etc. That's fine.
Now if a and b both zero, print -1.
One more nuance: The note says: In case,if 'a' or 'b' or both of them are zero, print '-1'. So if either is zero, output -1. That includes if only one is zero.
Thus final solution.
Now test some edge cases:
Case: t=1, n=1, a=1, b=2 -> count = 1/1=1 + 1/2=0 - 1/lcm(1,2)=1/2=0 => 1. Actually numbers: 1 divisible by 1, so answer 1. OK.
Case: n=1000000000000000, a=1000000, b=999983. lcm might be large but still safe.
Case: a=0, b=5 -> output -1.
So solution is done.
But wait: Could there be any tricky condition regarding "distinct numbers"? That's automatically handled by the inclusion-exclusion.
Now, the problem might be about "completely divisible by a or b or both?" i.e., union. But the output spec says "divisible by 'a' and 'b'." That is contradictory. In many programming contest problems, the phrasing might be wrong. But we go with sample.
So we produce code accordingly.
Now let's think about time complexity: O(t * log(min(a,b))) for gcd.
Now we need to output each answer on its own line.
That's it.
Thus answer: For each test case, if a==0 or b==0, print -1; else compute gcd using Euclidean algorithm, lcm = a / gcd * b, then ans = n/a + n/b - n/lcm; print ans.
But careful: a and b are up to 1e6, but we store as long long anyway.
One more nuance: There's a possibility that lcm overflows if we compute a * b first then divide by gcd. Better compute lcm = a / gcd * b. Since a/gcd is integer and <= a, and product with b is ≤ a*b/gcd. So safer.
Now code in C++:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll x, ll y) {
while(y) {
ll t = y;
y = x % y;
x = t;
}
return x;
}
int main() {
int t; cin >> t;
while(t--) {
ll n, a, b; cin >> n >> a >> b;
if(a == 0 || b == 0) {
cout << -1 << endl;
continue;
}
ll g = gcd(a, b);
ll lcm = a / g * b; // this order prevents overflow
ll ans = n / a + n / b - n / lcm;
cout << ans << endl;
}
return 0;
}
But careful: lcm might be > n? Then n/lcm = 0, fine.
Now check sample: Input: 1\n24 5 3 => output 11.
All good.
But wait: The problem statement also says "n<10^15". That means n can be up to 10^15 - 1. But we used long long, which in many compilers is 64-bit, max ~9.22e18, so it's safe.
One more nuance: The input may have t test cases, but the sample shows only one line. The constraints say 0<t<100, so t is at least 1.
Thus the code is final.
But let's read the problem statement again to ensure there is no other nuance: "You are given three positive integers 'n' , 'a' and 'b' . In the given range from 1 to 'n' how many distinct numbers are completely divisible by 'a' or 'b' or both? NOTE: The range includes both 1 and 'n'. INPUT SPECIFICATION: You will be given 't' test cases. Each test case will contain three integers 'n' 'a' and 'b'. OUTPUT SPECIFICATION: In a single line print the count of numbers that are divisible by 'a' and 'b'." Indeed contradictory. Could it be that they actually want the count of numbers divisible by both? But sample says 11, which is union not intersection. So it's union. The output spec likely mis-wrote "and" but meant "or". So we go with union.
Thus final answer as described.
Now note: The problem says "print the count of numbers that are divisible by 'a' and 'b'." Could be interpreted as numbers divisible by both a and b? That would be intersection, which is floor(n/lcm(a,b)). For sample that would be 1. But sample output is 11, so definitely union. So it's a mistake.
Thus solution correct.
Also note: The note says if a or b or both are zero print -1. So we handle that.
One more corner: If a and b are both zero, obviously -1. Also if only one zero, -1.
Thus solution done.
But the user asks to "Write complete code in C++". So we will output the code.
Now, could there be any tricky part about distinct numbers? For instance, if a and b are same? Then union = floor(n/a). Because lcm = a, so floor(n/a)+floor(n/a)-floor(n/a) = floor(n/a). That's correct.
If a divides b? Then union = floor(n/b) + floor(n/a) - floor(n/b) = floor(n/a)? Actually if b multiple of a, then numbers divisible by a or b: numbers divisible by a include those divisible by b, so union = numbers divisible by a. Count = floor(n/a). Our formula: floor(n/a) + floor(n/b) - floor(n/lcm). Since lcm = b (since b is multiple of a, gcd = a, lcm = a/gcdb = a/ab = b). So expression = floor(n/a) + floor(n/b) - floor(n/b) = floor(n/a). Good.
If both are 1? Then lcm=1, count = n + n - n = n. So numbers from 1 to n all divisible by 1, count = n. Good.
Thus all good.
Now code.
But the user might expect the code to handle multiple test cases. Also we need to flush output? Not needed.
We should consider using fast I/O for large t but t is only 100, so not necessary. But it's good practice: ios_base::sync_with_stdio(false); cin.tie(0);
We'll include that.
Also note: The input might contain large numbers, but reading using cin is fine.
Thus final code.
Now check for any potential overflow in n/a or n/b? n is up to 1e15, a min 1, so n/a up to 1e15, fits in long long.
Now also note: if a or b is 0, we output -1, but also what if a or b is negative? The problem says positive integers, but constraints say 0 ≤ a<10^6, so they're non-negative. So only zero check.
Alright.
Let's produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Function to compute greatest common divisor
ll gcd(ll a, ll b) {
while (b) {
ll temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
code
int t;
cin >> t;
while (t--) {
ll n, a, b;
cin >> n >> a >> b;
// If any of a or b is zero, output -1 as per note
if (a == 0 || b == 0) {
cout << "-1\n";
continue;
}
ll g = gcd(a, b);
ll lcm = a / g * b; // safe against overflow for a,b <= 1e6
ll ans = n / a + n / b - n / lcm;
cout << ans << "\n";
}
return 0;