We are given a problem: For given n, a, b (positive integers up to about 1e6), we need to count numbers from 1 to n (positive integers up to n) that cannot be represented as ai + bj, where i,j are nonnegative integers. In other words, the set S = {x in [1,n] : x cannot be expressed as a linear combination of a and b with nonnegative coefficients}. Compute the count of non-representable numbers.
This is a classic problem related to the Frobenius coin problem. For two positive coprime numbers a and b, the largest integer not representable as nonnegative combination is ab - a - b. But here a and b may not be coprime? The problem statement: "given positive integers a and b" - no mention of coprime. The classic formula holds only if gcd(a,b)=1. If gcd(a,b) = d > 1, then only multiples of d are representable, so the count of non-representable numbers from 1 to n is n minus the count of numbers ≤ n that are multiples of d? Wait: If a and b share a common divisor d>1, then any linear combination ai + bj is divisible by d. So representable numbers are a subset of multiples of d. But also, not every multiple of d might be representable if the coin problem is considered modulo something? Actually, with two numbers a,b, if d = gcd(a,b), then the set of representable numbers is exactly all multiples of d that are at least some Frobenius number for the reduced problem? More precisely, let a' = a/d, b' = b/d with gcd(a',b')=1. Then any representable number is divisible by d. And any multiple of d that is at least some threshold can be represented. Specifically, if N is a multiple of d, say N = dM, then N is representable as ai + bj if and only if M is representable as a'i + b'j. So the set of representable numbers is { dM : M is representable with a',b' and M ≥ 0 }. The Frobenius number for a',b' is a'b' - a' - b'. So the largest integer not representable in that reduced form is a'b' - a' - b'. So for multiples, the largest integer multiple of d not representable is d(a'b' - a' - b'). So numbers up to n that are representable are exactly those numbers divisible by d and at most n such that the quotient is representable (i.e., at least the Frobenius threshold? Actually, representability for a given M: M can be any integer ≥ 0 that is representable as a'i+b'j. The nonrepresentable M are those that are nonnegative integers not in the semigroup generated by a' and b'. The set of nonrepresentable M is finite up to a'b' - a' - b' (the Frobenius number). So the nonrepresentable numbers up to n are: (i) all numbers not divisible by d; (ii) among numbers divisible by d, those whose quotient M is not representable (i.e., M <= a'b' - a' - b' and M not in the semigroup). So the count is: count = (n - floor(n/d))? Wait: total numbers from 1 to n = n. Representable numbers: numbers of the form dM, with M ≥ 0, and M is representable. So we need to count how many k ∈ [1,n] satisfy that k mod d = 0 and (k/d) is in the semigroup generated by a' and b'. So we need to compute count of such numbers efficiently.
Constraints: up to 1e6 for n, a, b. So O(n) per test case is feasible? Possibly multiple test cases? The input is a sequence of lines, and the last line is 0 0 0. There could be many lines, but typical competitive programming problem: number of test cases maybe up to 10^4? But n up to 1e6, O(n) per case might be too slow if many test cases. However, there is a more clever method.
Given n, a, b, we need the number of integers in [1,n] that cannot be expressed as ai + bj.
We can use the following: For given a,b (positive), the set of representable numbers is periodic modulo something? Actually, the semigroup generated by a and b is co-finite if gcd=1; otherwise it's a subset of multiples of d. But for counting up to n, we could compute the count of representable numbers by inclusion-exclusion? No.
Better approach: For two numbers a,b, the representability can be determined using the classic result: For any integer x ≥ (a-1)(b-1) (when gcd=1), it is representable. More precisely, if gcd(a,b)=1, then the largest nonrepresentable is ab - a - b. So all numbers > ab - a - b are representable. So for n large enough, we can compute the count of nonrepresentable numbers as something like: count_nonrep = (some constant) for numbers <= threshold, and 0 for beyond? Actually, all numbers > ab - a - b are representable, so nonrepresentable numbers are only those from 1 to min(n, ab - a - b) that are not representable. So we can compute the count for up to threshold T = ab - a - b (if gcd=1). For n up to 1e6, and a,b up to 1e6, ab could be up to 1e12, too big. But note that n is at most 1e6, so we only need to consider numbers up to n. If gcd=1, we can find all representable numbers up to n by iterating over i from 0 to floor(n/a) and marking ai + bj for j? That is O(n) if we use a boolean array of size n+1. Since n <= 1e6, that's fine. But if there are many test cases, say 1000, each with n=1e6, then total 1e9 operations, might be too high. But maybe input is small. However, we should aim for an efficient solution.
Alternative: Use formula based on counting multiples and the Frobenius number. For gcd=1, the number of nonrepresentable numbers up to n is: (if n >= ab - a - b + 1) then count = (ab - a - b + 1)/2? Wait, there is known result: The number of positive integers not representable as a linear combination of a and b (with gcd=1) is (a-1)(b-1)/2. Actually, the number of nonrepresentable positive integers (i.e., the count of integers not in the semigroup) is exactly (a-1)(b-1)/2. This is a known result: For coprime a and b, the number of positive integers that cannot be expressed as ax+by (x,y>=0) is (a-1)(b-1)/2. Indeed, the Frobenius number is ab - a - b, and the total count of nonrepresentables is (a-1)(b-1)/2. But that counts all positive integers up to infinity? Wait, all nonrepresentable numbers are between 1 and ab-a-b inclusive, because beyond that all are representable. So total number of positive integers not representable is exactly the number of integers from 1 to ab-a-b that are not in the semigroup. And it's known that the number of gaps is (a-1)(b-1)/2. Yes, that's correct. So for coprime a,b, the count of nonrepresentable numbers among all positive integers is (a-1)(b-1)/2. But we only need up to n. If n is >= ab-a-b, then the answer is (a-1)(b-1)/2, because all numbers beyond that are representable. However, note that ab-a-b could be huge (up to ~1e12) which exceeds n (max 1e6). So for many cases, n will be less than ab-a-b. Then we need to count nonrepresentable numbers up to n, which is a subset of those gaps. So we need a way to count the number of representable numbers ≤ n efficiently.
If gcd != 1, we can reduce: let d = gcd(a,b). Then representable numbers are multiples of d. Among multiples of d, the quotient must be representable by a' and b' (coprime). So the count of nonrepresentable numbers ≤ n is: (numbers not divisible by d) + (numbers divisible by d but whose quotient is not representable). The total numbers not divisible by d from 1..n is n - floor(n/d). Among the multiples of d, say M = d*k, with k from 1 to floor(n/d). M is representable iff k is representable as a'*i+b'*j. So the number of nonrepresentable numbers divisible by d is the number of k in [1, floor(n/d)] that are not representable by a',b'. So total nonrepresentable count = (n - floor(n/d)) + (nonrepresentable_count_for(a',b') up to floor(n/d)). So the problem reduces to coprime case for counting up to N = floor(n/d) with a',b'.
Thus the core is: Given coprime positive integers a, b (with a,b up to maybe 1e6/d, but still up to 1e6) and an integer N (<= floor(n/d) <= n <= 1e6), compute the count of numbers 1..N that are not representable as ai + bj.
Now N could be as large as 1e6. We need to compute this efficiently per test case.
We could precompute something? For each test case, we can compute an array of size N+1? Since N ≤ 1e6, and number of test cases unknown, but typical may be okay. However, maybe we can find a formula or a O(a) algorithm. Since a,b up to 1e6, but if a is small relative to N, we can compute number of representable numbers by iterating over i from 0 to N/a and for each i compute the smallest j? Actually, there's known method: For fixed a, the representability of numbers mod a can be determined: For each residue r modulo a, the smallest number congruent to r mod a that is representable is some value (depending on b). Because b is invertible modulo a (since gcd=1), the set of representable numbers includes all numbers greater than some bound for each residue. More precisely, for each residue class r (0 <= r < a), the numbers that are congruent to r mod a and are representable are exactly those numbers >= some threshold t_r, and all numbers in that class that are >= t_r are representable. This is because if we can represent some number x0 in class r, then by adding a we can represent x0 + a, x0+2a, etc. So for each r, there is a minimal representable number in that class (maybe 0? but 0 is representable with i=j=0 but we consider positive numbers; 0 is not counted). So for positive numbers, we need to find for each r the smallest positive representable number (if any). Since gcd=1, all residues appear eventually. Indeed, all residues modulo a are representable for numbers >= something. The minimal representable number in class r can be found by solving bj ≡ r (mod a) => j ≡ b^{-1} * r (mod a). The smallest nonnegative j gives a number bj that is ≡ r mod a, but that number might be < r? Actually, bj will be the number, but we need to add multiples of a to make it positive? Wait, we need numbers of the form ai + bj. For fixed residue r, we can set j = (r * b^{-1}) mod a (call that j0), then ai + bj0 can be any number of the form ai + bj0. The smallest such positive number? Actually, for each r, the set S_r = {ai + bj0 : i >= 0} is an arithmetic progression with difference a, starting from bj0 (when i=0). But bj0 may be >0. However, if bj0 = 0 (when r=0, j0=0 gives 0), then the positive numbers are a, 2a,... So the smallest positive representable number in class r is: if r=0, it's a (since 0 not counted). Else, we compute the smallest positive number x ≡ r (mod a) that is representable. This can be done by considering j from 0 to a-1 (since mod a), because b mod a is invertible; the mapping j -> bj mod a is a permutation of residues. So for each residue r, there exists a unique j in [0, a-1] such that bj ≡ r (mod a). Then for that j, the numbers ai + bj for i>=0 give all numbers congruent to r mod a that are >= bj. But note bj may be less than r? Actually, bj could be any value, but its residue is r. So the smallest positive number in that class is either bj if bj > 0 and is the first term (i=0) or if bj = 0 (which happens only for r=0) then it's a (i=1). So threshold t_r = bj > 0, else a (for r=0). But is it always true that all numbers >= t_r in that residue class are representable? Yes, because once we have one representable number x0 in that class, we can add a repeatedly to get all numbers x0 + k*a. So the set of representable numbers in that class is exactly {x0, x0 + a, x0 + 2a, ...} intersected with positive integers. So for each residue r, the representable numbers are those congruent to r mod a that are >= L_r, where L_r is the smallest positive representable number in that class.
Thus, for N, the number of representable numbers <= N can be computed by summing over residues r=0..a-1: count_r = number of integers x in [1,N] with x ≡ r mod a and x >= L_r. If L_r > N, then 0. Otherwise, the count is floor((N - L_r)/a) + 1 (since L_r, L_r+a, L_r+2a,... <= N). So total representable = sum_{r} max(0, floor((N - L_r)/a) + 1). Then answer = N - representable.
We can compute L_r for each residue r efficiently if we can compute the modular inverse of b modulo a. Since a up to 1e6, we can loop over j from 0 to a-1 and compute r = (bj) mod a, then L_r = bj (if bj > 0) but careful: For r=0, we need L_0 = a (since b0 = 0, but 0 is not positive; next is a). However, if bj > 0 for some j gives r=0? Because b mod a might have multiples that are multiples of a. Since gcd(b,a)=1, bj mod a = 0 only when j ≡ 0 mod a. So the only j that gives r=0 is j = 0 (since j in 0..a-1). So L_0 = a (or we can treat L_0 as 0 but then we need to ensure positive; easier: compute base = bj; if base == 0 then base = a). For other r, base = bj > 0? bj could be zero only if j=0. So for j>0, bj >0. So L_r = bj. But note: Could there be a case where bj < r? That doesn't matter; L_r is just that number.
But careful: The minimal representable number in a residue class may not be given by the j in [0,a-1] with smallest bj? Since the representation is ai + bj, with i >=0. For a fixed residue r, we can also use i>0 and j smaller? Actually, the formula for the minimal positive representable number in class r is: min_{i>=0, j>=0, ai+bj ≡ r (mod a)} ai + bj > 0. Since ai ≡ 0 (mod a), the condition reduces to bj ≡ r (mod a). So j must satisfy that. Among all j that satisfy the congruence (i.e., j ≡ j0 (mod a) ), the representable numbers are of the form ai + bj. For a given j, the smallest number for that j is when i=0, giving bj. For larger j (j = j0 + ka), the number bj might be larger than ai + bj0 for some i? Because we can also increase i to offset. Actually, for a given residue r, the set of numbers representable is { ai + bj : j ≡ j0 (mod a), i>=0 }. If we let j = j0 + ta, then bj = bj0 + tab. Then ai + bj = ai + bj0 + tab = a(i + tb) + bj0. So this is essentially the same as taking i' = i + tb, j' = j0. So the set is exactly { ai + bj0 : i >= 0 }. Therefore, the minimal positive representable number for that residue is indeed bj0 if bj0 > 0, else we need i>=1 gives a. So the minimal L_r is bj0 if bj0 >0, else a if j0=0. Since j0 is the unique j in [0, a-1] satisfying bj ≡ r (mod a). So we can compute L_r easily.
Thus, for coprime a,b, we can compute answer in O(a) time, which could be up to 1e6. That is okay per test case if only a few cases. But worst-case a=1e6, and maybe many test cases? Could be up to, say, 10 cases? Then O(1e7) is fine. But what if there are thousands? Might be too slow. But we can also note that we can compute the count more directly using formula: number of representable numbers up to N is something like: sum_{k=0}^{⌊N/a⌋} (⌊(N - ak)/b⌋ + 1) but that sums over i, which is O(N/a) if we loop over i? That could be O(N) worst-case if a=1, but a=1 trivial? Actually if a=1, all numbers are representable (since 1i + b*j covers all numbers). So answer is 0. Similarly if b=1. So we can handle small a or b separately.
But we should probably adopt the O(min(a,b)) method? Actually, the method using residues requires O(a). If a could be as large as 1e6, that's okay. But what about the case where a and b are not coprime? Then we reduce to a' = a/d, b' = b/d. The a' could still be up to 1e6 (if d=1). So worst-case O(a') with a' up to 1e6. Could be okay for up to maybe 100 test cases? 100*1e6 = 100 million, borderline but maybe okay in C++ but Python might be slower. Need efficient Python solution.
Let's think about constraints: The problem statement says "The integers n, a and b are all positive and at most one million, except those in the last line." So each of n, a, b <= 1e6. So a and b up to 1e6. That's fine. The number of test cases is not given, but typical problems might have up to 10^4 lines? That would be huge, but 10^4 * 1e6 = 10^10 impossible. However, given that each line includes n, a, b and n is up to 1e6, it's plausible that the number of test cases is moderate, maybe up to 1000? Even 1000*1e6 = 1e9, still too high for Python.
We need to find a more efficient approach. Perhaps we can derive a formula that counts nonrepresentable numbers up to N for coprime a,b without iterating over all residues. There is known result: The number of representable numbers up to N is something like: floor(N/a) + floor(N/b) - floor(N/lcm(a,b))? That's for numbers representable as either multiple of a or b? But here we have combinations, not union. Not that.
Another known: The set of representable numbers is all numbers >= (a-1)(b-1) except some gaps. But to count up to N, we can use the formula based on the sum of floors: The number of solutions to ai + bj <= N with i,j >=0. That is sum_{i=0}^{floor(N/a)} (floor((N - ai)/b) + 1) but that counts the pair (i,j) including j=0. That gives the count of distinct numbers? However, different pairs (i,j) could yield the same number? Could they? For a and b distinct, it's possible that ai1 + bj1 = ai2 + bj2 with (i1,j1) ≠ (i2,j2) if a and b have common factor? But if gcd=1, representations could be non-unique? Actually, it's possible: e.g., a=2,b=3, 6 = 23+30 = 20+3*2. So yes, there can be multiple representations. So that sum counts each representation, not distinct numbers. So not directly usable.
But we can count distinct numbers by marking or by using the complement counting method described earlier (residue classes). The residue method is essentially O(a) to compute L_r. That is likely the intended solution given constraints? Let's test complexity: In the worst case, a=1e6, we compute L_r array of size a. That's 1e6 operations, which in Python could be maybe 0.1 second? Actually, 1e6 simple operations is fine (maybe 0.02-0.03 sec). If we have 1000 test cases, 1e9 operations would be too much. But maybe the number of test cases is limited because the input lines contain three ints each, and typical online judges limit total input size. The maximum input lines if each line uses about 20 bytes, 1000 lines is 20KB, that's small; could be 10,000 lines (200KB). Could be more. But 1e4 * 1e6 = 1e10, impossible. So we need to ensure per test case complexity not linear in a or b if they are large and many cases.
Alternatively, we might compute the answer using a formula that involves the Frobenius number and some summation. There is known result: The number of nonrepresentable numbers up to N (for coprime a,b) is given by something like:
count_nonrep(N) = (N+1) - sum_{k=0}^{a-1} max(0, floor((N - b*k')/a) + 1) where k' is inverse? Actually, the residue method is essentially that. But maybe we can compute the sum analytically.
The representable numbers are exactly those numbers that can be written as ai + bj. Since gcd=1, the semigroup is symmetric, and there's a formula for the sum of representable numbers etc. But counting them up to N might be expressed as:
Let inv = b^{-1} mod a. Then for each residue r, the minimal representable number is: if r == 0: a else (b * ((inv * r) % a)). But note that ((inv * r) % a) gives j0 in [0, a-1]. So L_r = b * j0, except when j0==0 (i.e., r==0) then L_r = a.
So representable count = ∑_{r=0}^{a-1} max(0, floor((N - L_r)/a) + 1) with L_0 = a, L_r>0 for others.
We can compute this sum without iterating over all residues if we can sort L_r? But maybe we can compute directly: For each possible j in [0, a-1], the residue r = (bj) mod a, and L_r = bj if j>0, else a. So instead of iterating over residues, we can iterate over j from 0 to a-1. That's still O(a). But note that b may be as large as 1e6, but a might be similarly large.
But perhaps we can swap roles: Use the smaller of a and b as the modulus to reduce complexity to O(min(a,b)). Since we can choose to use residues modulo min(a,b). Because the method is symmetric: We can compute using a as modulus, but we could also use b as modulus. So we can set m = min(a,b) and then the complexity becomes O(m). So worst-case m = 1e6/2? Actually min can be up to 1e6. Still large.
But if a and b are both large and many test cases, maybe we can precompute something? However a and b vary per test case.
Is there a way to compute count_nonrep up to N in O(log a) or something? Possibly using the formula: Count of representable numbers up to N = N - sum_{k=0}^{a-1} max(0, floor((N - k*b_inv? ))? Not sure.
Let's attempt to derive a closed form.
Given coprime a, b. The set of nonrepresentable numbers is known to have size (a-1)(b-1)/2. For N less than the Frobenius number, the count of representable numbers can be expressed using the formula:
R(N) = ∑_{i=0}^{⌊N/a⌋} (⌊(N - a*i)/b⌋ + 1) - D(N)
where D(N) corrects for overcounting duplicates? Actually, the sum counts each representation. If we want distinct numbers, it's not trivial to subtract duplicates. However, there is a known identity: The number of integers ≤ N that are representable is equal to (N+1) - (1/a) ∑_{k=0}^{a-1} (something). Wait, there's a known formula using the "coin problem" and the "Frobenius number". There's a formula for the number of nonrepresentable numbers up to N:
Let f(N) = # { x ∈ [0,N] : x is representable } (including 0). Then there is a formula:
f(N) = (N+1) - ∑_{r=0}^{a-1} floor((N - r * b^{-1} mod? )?) Not sure.
Alternatively, we can think of the generating function: (1 - x^a)^{-1} (1 - x^b)^{-1} = ∑_{n≥0} r(n) x^n where r(n) is number of representations. But that's not distinct.
But for distinct numbers, we care about indicator if at least one representation. The set of numbers not representable are those where the number of representations is zero.
There is known concept of Apéry set of the numerical semigroup. For a numerical semigroup generated by a,b, the Apéry set with respect to a is { w(r) : r=0,...,a-1 } where w(r) is the smallest element in the semigroup congruent to r mod a. That's exactly L_r (or maybe 0 for r=0? Actually, w(0) = 0 because 0 is in the semigroup. But for positive, we want w(0) = a). But the Apéry set (with respect to a) is defined as { s ∈ S : s - a ∉ S } which yields the minimal elements in each residue class. For coprime case, the Apéry set elements are: w(r) = b * ((inv_b * r) mod a), except w(0)=0 (since 0 is in S). So our L_r for positive numbers is w(r) if w(r) > 0 else a. So indeed, the representable numbers up to N can be counted as:
R_pos(N) = ∑_{r=0}^{a-1} max(0, ⌊(N - w(r))/a⌋ + 1 ) - 1? Because w(0)=0 gives count of numbers 0, a, 2a,... ≤ N: floor(N/a)+1 including 0. But we want positive only, so for r=0, we should start from a. So we can set w'(0)=a, and w'(r)=w(r) for r≠0 (since w(r) > 0). So the formula above stands.
Thus, we need to compute sum_{j=0}^{a-1} floor((N - L_j)/a) + 1 for each j where L_j ≤ N. This is essentially sum_{j} max(0, floor((N - L_j)/a) + 1). This sum can be computed by iterating j from 0 to a-1. That's O(a). But we can also compute the sum more efficiently if we can sort L_j? They are of the form bj mod a pattern. Since b and a are coprime, the set { (bj) mod a : j=0..a-1 } is a permutation of residues. So L_j for j>0 takes values bj which are multiples of b, but mod a they give all residues. The L_j values themselves are the numbers bj for j=0..a-1 except L_0 we replace with a. But bj can be as large as b(a-1) which could be up to about 1e12 (since a,b up to 1e6). That's huge but we only care about comparing to N (≤ 1e6). So many L_j will be > N, making floor negative, so they contribute 0. Only those j where bj ≤ N (or L_0 = a ≤ N) contribute. So we only need to consider j such that bj ≤ N (and j>0) plus possibly j=0 if a ≤ N. So the number of contributing j is at most floor(N/b) + 1. Since N ≤ n/d and n ≤ 1e6, N is ≤ 1e6. b could be as low as 1, then floor(N/b) = N up to 1e6, but b=1 is trivial case (all numbers representable). For b moderate, floor(N/b) can be small. Actually, worst-case for this iteration: b=1 => floor(N/b)=1e6, which is large but we can handle b=1 separately. For b large, floor(N/b) small. So we could iterate over j from 0 to floor(N/b) to compute the residues and L values? But careful: The mapping j -> residue is not one-to-one if we only consider j up to floor(N/b). However, the L_j for j>0 are exactly the numbers bj. The condition bj ≤ N ensures L_j ≤ N. For j > floor(N/b), bj > N, so they do not contribute. So we only need to consider j in [0, floor(N/b)]. But note that for residues r, the minimal L_r might be given by a j that is > floor(N/b) if the minimal L_r is > N, then that residue class contributes 0. So indeed, we only need to check those j that produce L_r ≤ N. However, is it guaranteed that the minimal L_r for a given residue is always the smallest bj that yields that residue? Since the minimal representable number in residue r is the smallest positive number of the form bj (with j from 0..a-1) that is ≡ r (mod a), and then possibly plus multiples of a? Actually, as argued, the minimal representable number in class r is either bj0 (where j0 is the unique j in [0,a-1] with bj0 ≡ r mod a) if bj0 > 0, else a for r=0. So indeed L_r = bj0 (or a for r=0). So if bj0 > N, then L_r > N, so no numbers in that class ≤ N are representable. Therefore, we can compute L_r for residues by scanning j from 0 to a-1, but we can limit j to those where bj ≤ N? Not exactly, because j0 might be > floor(N/b) but bj0 might still be ≤ N? Wait, if j0 > floor(N/b), then bj0 > N (since bj0 > bfloor(N/b) ≥ N - (b-1)? Actually, floor(N/b) = q such that bq ≤ N < b*(q+1). So if j0 ≥ q+1, then bj0 ≥ b(q+1) > N. So indeed if j0 > floor(N/b), then bj) mod a, and L_r = bj.
Now, we need to ensure that we don't double count: Since each j corresponds to a distinct residue, and we are only including those where L_j ≤ N, we get contributions from those residues that have L_r ≤ N. The residues not covered have L_r > N, contribute 0. So representable count = Σ_{j=0}^{⌊N/b⌋} (if j < a?) Actually, we need to consider j up to a-1, but since a can be less than floor(N/b), we cannot iterate j beyond a-1 because mapping repeats? But the mapping j -> residue is periodic with period a, because bj mod a repeats every a increments of j. However, for j > a-1, the residue r for j is same as for j mod a, but the L_r would be bj which is larger than b*(j mod a). The minimal representable number for that residue is L_{r} = bj0 where j0 is the unique representative in [0,a-1]. So for j > a-1, bj is not the minimal representable for that residue; it's a larger number in the same arithmetic progression. So including all j up to floor(N/b) would overcount because the same residue class would be counted multiple times? Let's reconsider: The representable numbers in a given residue class r are exactly { L_r + at : t >= 0 }. So the contribution from that class is number of t such that L_r + at ≤ N. That's floor((N - L_r)/a) + 1. That's a function of L_r only. L_r is the minimal number for that class, which corresponds to the smallest j0 in [0,a-1] with bj0 ≡ r (mod a). So we should count each residue class exactly once, using L_r. If we iterate over j from 0 to floor(N/b), we might encounter multiple j that yield the same residue r. But the minimal L_r is the smallest bj for that residue, which will be the first encountered j (the smallest j) that yields that residue. For larger j, L_r is not the minimal; those numbers are already covered by the arithmetic progression starting from the minimal. So we must not include them separately. So our sum should be over residues, not over all j. So we cannot just sum over j up to floor(N/b) because that would count the same residue multiple times if multiple j produce the same residue and all have bj ≤ N. However, note that since the mapping j→r is a bijection on [0,a-1], for j in [0, a-1], each residue appears exactly once. For j >= a, the residues repeat, but the residues already have a minimal L_r from j mod a. So if we sum over j=0..floor(N/b), we would be summing contributions for each number bj as if it were the minimal for its residue, but that's incorrect because for residues where j mod a = j0 but j > j0, the minimal is actually bj0 (which is smaller). The numbers bj for j > j0 are also representable, but they are already accounted for by the arithmetic progression from bj0. So if we sum contributions for each j, we would be counting many numbers multiple times (maybe all representable numbers, counting each once per representation?). Actually, if we compute for each j the number of i such that ai + b*j ≤ N, that sum over j gives total number of representations (including multiple representations). But we need distinct numbers. So the earlier method of summing over residues gives distinct numbers by counting each number once based on its residue class.
Thus, the more efficient way to compute distinct representable numbers is to sum over residues, but we can limit residues to those where L_r ≤ N. And L_r = b * j0 where j0 ∈ [0, a-1] is the unique j giving that residue. So we need to compute j0 for each residue? That requires either iterating over j from 0 to a-1 or computing modular inverses. But we can perhaps compute the minimal L_r for each residue by using the fact that as j runs from 0 to a-1, we get all residues exactly once. So we can simply iterate j from 0 to a-1, compute L_r = b*j if j>0 else a, and if L_r ≤ N, add its contribution. That's O(a). But as argued, a could be 1e6 and many test cases, maybe too many.
But we can also choose to use the smaller modulus. Since we can equally use b as modulus. So we can set mod = min(a,b). That gives O(min(a,b)). In worst-case, a and b both large (~1e6) but min still 1e6. So still large.
Now, note that N ≤ 1e6. So if a > N, then the residue method with modulus a will have many residues with L_r > N? Actually, if a > N, then for residues r that are > N? Wait, L_r = bj0, with j0 ∈ [0,a-1]. Since a > N, many j0 will be > N? But j0 can be up to a-1, which could be > N. However, L_r = bj0. Since b≥1, L_r could be large. But we only care about residues where L_r ≤ N. The number of such residues is limited? Let's think: For j0 from 0 to a-1, L_r = bj0 (or a for j0=0). The condition bj0 ≤ N implies j0 ≤ floor(N/b). So there are at most floor(N/b)+1 possible j0 that satisfy this. Since N ≤ 1e6, floor(N/b) ≤ 1e6 / 1, but if b is 1, floor(N/b)=1e6, but then a could be also 1e6. In that case, number of j0 with bj0 ≤ N is about 1e6, which is again O(N). So still potentially 1e6 operations. But if b is large, floor(N/b) is small. So the number of residues with L_r ≤ N is at most floor(N/b)+1 (plus maybe for r=0 we have a, which may be ≤ N if a ≤ N). So we could compute the contributions by iterating over j0 from 0 to min(a-1, floor(N/b))? However, this will only give us those residues whose minimal L_r is ≤ N. But we must also consider residues where L_r > N but there could still be representable numbers ≤ N? If L_r > N, then no number in that residue class is ≤ N because the smallest representable number is already > N. So yes, they contribute 0. So the set of residues that contribute is exactly those for which the unique j0 (the minimal) satisfies bj0 ≤ N (or j0=0 with a ≤ N). So we need to know which residues those are. However, we cannot directly know which residues correspond to those j0 without mapping j0 to residue. We can iterate over j0 from 0 to floor(N/b) and for each, compute the residue r = (bj0) % a, and then the contribution for that residue is floor((N - L)/a) + 1, where L = (bj0 if j0>0 else a). But careful: It could happen that for a given residue, its minimal j0 might be larger than floor(N/b), but there is another j (greater than j0) with bj ≤ N? Since j0 is the smallest j giving that residue, if j0 > floor(N/b), then bj0 > N, so all numbers in that residue are ≥ b*j0 > N, so indeed no representable numbers ≤ N. So we are safe.
But what about residues where the minimal j0 is less than or equal to floor(N/b), but there is also a larger j (still ≤ floor(N/b)) that gives the same residue? Since the mapping j→ residue modulo a has period a, the residues repeat every a. For j0 in [0,a-1] it's the minimal j for that residue. For j = j0 + ka, with k>=1, they produce the same residue. Could it be that j0 > floor(N/b) but j0 + ka (for some k negative? not possible) or smaller? Actually, j0 is the unique representative in [0,a-1], so it's the smallest nonnegative j giving that residue. So any other j giving that residue will be j0 + ta for some t>=1. So if j0 > floor(N/b), then for any t>=1, j = j0 + ta ≥ j0 > floor(N/b), so bj > N as well. So indeed no representable numbers ≤ N from that residue. So indeed, we only need to consider j in the range [0, min(a-1, floor(N/b))] because those give the minimal L_r for each residue and also satisfy L_r ≤ N. But wait: For residue r=0, the minimal representable number is a (since j0=0 gives L=0, but we use a). That's not of the form bj. So we handle separately.
Thus, to compute representable count, we can:
- If a or b = 1: answer is 0 because all numbers representable.
- Else, compute d = gcd(a,b). Then n' = floor(n/d), a' = a/d, b' = b/d. Then answer = (n - n') + f(n', a', b'), where f(N, a, b) is count of nonrepresentable numbers from 1..N for coprime a,b.
Now compute f(N, a, b) for coprime a,b.
We want count_nonrep = N - R(N), where R(N) = number of representable positive integers ≤ N.
Compute R(N) via:
Let modulus = min(a, b)? Actually we can choose whichever is smaller to iterate over j0. But we derived that we can iterate over j from 0 to floor(N / max(a,b))? Let's think more systematically.
Given coprime a,b. We'll use the residue method with modulus m = a (or b). If we use modulus a, we need to compute for each j0 in [0, a-1] the L = (j0==0? a : b*j0) and if L ≤ N, add floor((N - L)/a) + 1.
Number of terms = number of j0 with bj0 ≤ N (plus maybe j0=0 with a ≤ N). So that count is about min(a, floor(N/b) + 1). If a is large but b is also large? Actually, floor(N/b) is at most N (when b=1) but b=1 is trivial case already handled. For b≥2, floor(N/b) ≤ N/2 up to 5e5. If a is 1e6 and b is 2, floor(N/b) could be up to 5e5 (since N ≤ 1e6). So we could iterate up to 5e5. That's okay. But if b is very small (like 2), a could be 1e6, and floor(N/b) = up to 500k, which is still okay. If b=1, we handle separately. So worst-case non-trivial b is 2, giving 500k iterations. That's okay for a single test case, maybe for many test cases it's heavy but still maybe acceptable if test cases ~100 (500k100 = 50 million) okay in Python? 50 million operations maybe borderline but possibly okay with optimized code. However, if many test cases have b=2 and a=1e6, floor(N/b) ~ 500k, times number of test cases, could be large. But maybe not.
Alternatively, we can choose modulus = min(a,b) to minimize iterations. For a=1e6, b=2, min is 2, then we could use modulus b=2, which would have only 2 residues. That would be much faster! Because we can use the other modulus. Indeed, the method is symmetric: we can use either a or b as the modulus. So we should use the smaller of a and b as the modulus to minimize the loop size. Because the loop goes up to the modulus size (i.e., the number of residues). But wait, the number of residues equals the modulus value. So if we use modulus = min(a,b), then the loop size is min(a,b). For a=1e6, b=2, min=2 => loop size 2, great. For a=500k, b=500k, min=500k, loop size 500k. But that's still large. However, N ≤ 1e6, and a,b up to 1e6, so min could be up to 1e6. So worst-case min = 1e6. That's the same as before. But maybe we can combine with the idea that we only need to consider residues where L ≤ N, and the number of such residues is at most floor(N / max(a,b))? Let's analyze.
If we pick modulus = a (assuming a <= b). Then the residues count is a. But we can limit to j0 such that bj0 ≤ N (or a ≤ N for residue 0). Since j0 is in [0, a-1], the condition bj0 ≤ N gives j0 ≤ floor(N/b). So the number of j0 we need to consider is at most floor(N/b)+1. If a > floor(N/b), then the number of j0 to process is floor(N/b)+1, which could be less than a. For a=1e6, b=500k, floor(N/b) ≤ 2 (since N ≤ 1e6, b=500k => floor(N/b) ≤ 2). So we only need to consider j0=0,1,2 maybe. So we can process only those j0 rather than all a residues. But we need to compute the residue for each such j0, and also ensure we don't double count residues? Since the mapping j0 → residue is a bijection, different j0 give different residues. So we can iterate j0 from 0 to min(a-1, floor(N/b)), compute L = bj0 (if j0>0 else a), and compute contribution. But wait, what about residues whose minimal j0 is > floor(N/b) but there is a larger j (multiple of a added) that yields a representable number ≤ N? That's impossible as argued, because the minimal L for that residue is bj0 > N, so all numbers in that class are > N. So we indeed only need to consider j0 up to floor(N/b). But we must also consider j0=0 with L=a, but condition a ≤ N? If a > N, then residue 0 contributes nothing, but we might still have j0=0 but a > N so we skip. So we can limit j0 to those satisfying L ≤ N.
Thus, we can compute R(N) by iterating over j from 0 to min(a-1, floor(N/b)), but also need to consider that j=0 case uses L=a (not b0). However, note that j=0 gives residue r = 0. The minimal L for residue 0 is a, not 0. So we can treat separately: for j=0, contribution = if a ≤ N then floor((N - a)/a) + 1 = floor(N/a) (since (N-a)/a +1 = N/a? Actually floor((N - a)/a)+1 = floor(N/a - 1) +1? Let's compute: If N >= a, then floor((N - a)/a) + 1 = floor(N/a - 1) + 1 = floor(N/a) (since floor(x-1) +1 = floor(x) if x not integer? Actually, for integer N, floor(N/a - 1) = floor(N/a) - 1 if N mod a != 0? Let's derive: Let q = N // a, r = N % a. Then (N - a) // a = (aq + r - a) // a = (a*(q-1) + r) // a = q-1 + (r // a) = q-1 (since r < a). Then +1 = q. So yes, contribution = floor(N/a). That makes sense: numbers a,2a,..., up to qa ≤ N, count = q. So for residue 0, contribution = floor(N/a). So we can compute directly without loop: if a <= N, add floor(N/a). But careful: In the residue sum method, we also have contributions from other residues. So we can compute: R(N) = (if a <= N then floor(N/a)) + sum_{j=1}^{Jmax} [if bj <= N then floor((N - bj)/a) + 1], where Jmax = min(a-1, floor(N/b)). However, does this sum over j from 1 to Jmax count each residue exactly once? Since mapping j -> residue is bijective on [0, a-1], and we are only considering j up to Jmax, we are including residues whose minimal j0 is ≤ Jmax. For residues whose minimal j0 > Jmax, we skip them (they contribute 0). So that's correct. But we must ensure that we are not missing residues where the minimal j0 is > Jmax but there exists some j' > Jmax (still ≤ a-1) that is the minimal? Wait, minimal j0 is by definition in [0,a-1]. If minimal j0 > Jmax, then it's > floor(N/b) (since Jmax = min(a-1, floor(N/b)), but floor(N/b) could be less than a-1. So minimal j0 > floor(N/b) means bj0 > N, so L_r > N, so no contribution. So it's safe to skip.
But what about the case where a is smaller than floor(N/b)? Then Jmax = a-1 (since min(a-1, floor(N/b)) = a-1). That means we iterate over all j=0..a-1? Actually, j from 1 to a-1 (we treat j=0 separately). That's O(a) operations. So worst-case occurs when a <= b and a is small relative to N? Wait, Jmax = min(a-1, floor(N/b)). If a is small, say a=1000, b=1000, floor(N/b) could be up to 1000 (since N ≤ 1e6). So Jmax = min(999, maybe 1000) = 999. So loop size ~a = 1000, fine. If a is large (say a=1e6) and b is also large (b close to a), then floor(N/b) could be about N/b ≤ 1e6/500k = 2, so Jmax = 2. So loop size small. The worst-case loop size is when a is large and b is small. For example, a=1e6, b=2. Then floor(N/b) ≤ 5e5, and a-1 = 999,999, so Jmax = min(999,999, 500,000) = 500,000. So loop size ~500k. That's large but still manageable per test case? 500k operations is okay. If we have many such cases, it could be heavy. But maybe the number of test cases is limited.
Alternatively, we can swap roles: if b is smaller than a, we could use modulus b to get loop size up to min(b, floor(N/a)). That could be smaller if b is small and a is large? In the example a=1e6, b=2, we could use modulus b=2, then we would loop over j=0..1 (