You are given two integers a and b, and q queries. The i-th query consists of two numbers l_i and r_i, and the answer to it is the number of integers x such that l_i ≤ x ≤ r_i, and ((x mod a) mod b) ≠ ((x mod b) mod a). Calculate the answer for each query.
Recall that y mod z is the remainder of the division of y by z. For example, 5 mod 3 = 2, 7 mod 8 = 7, 9 mod 4 = 1, 9 mod 9 = 0.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Then the test cases follow.
The first line of each test case contains three integers a, b and q (1 ≤ a, b ≤ 200; 1 ≤ q ≤ 500).
Then q lines follow, each containing two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ 10^{18}) for the corresponding query.
Output
For each test case, print q integers — the answers to the queries of this test case in the order they appear.
Example
Input
2
4 6 5
1 1
1 3
1 5
1 7
1 9
7 10 2
7 8
100 200
Output
0 0 0 2 4
0 91
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import sys
def compute_gcd(a, b):
while b:
a, b = b, a % b
return a
def main():
input = sys.stdin.read().split()
idx = 0
T = int(input[idx])
idx += 1
for _ in range(T):
a = int(input[idx])
b = int(input[idx+1])
q = int(input[idx+2])
idx +=3
gcd_val = compute_gcd(a, b)
p = (a * b) // gcd_val
# Compute good array
good = [0]*p
for x in range(p):
moda = x % a
modb_x = x % b
f = moda % b
g = modb_x % a
if f != g:
good[x] = 1
# Compute prefix array
prefix = [0] * p
prefix[0] = good[0]
for i in range(1, p):
prefix[i] = prefix[i-1] + good[i]
total_per = prefix[-1]
# Process queries
output = []
for __ in range(q):
l = int(input[idx])
r = int(input[idx+1])
idx +=2
def count(x):
if x < 0:
return 0
q_x = x // p
rem = x % p
return q_x * total_per + prefix[rem]
res = count(r) - count(l-1)
output.append(str(res))
print(' '.join(output), end=' ')
print()
if __name__ == "__main__":
main()
Wait, looking at the code above, after each test case's queries are processed, we print the result with ' '.join(output), and then have a print() which adds a new line. But maybe in the sample input's output lines have a trailing space?
Looking at the sample:
Sample Output includes lines like "0 0 0 2 4 " (ends with space), but in coding competition, trailing spaces may be allowed. However, in the code above:
When the output is printed via ' '.join(output) will join with spaces, but the print statement adds an end=' ', but then a print().
Wait no, the current code has a loop where after appending all outputs, the line:
print(' '.join(output), end=' ')
print()
Wait no, the code as written in the above code is:
code
output = []
for __ in range(q):
...
output.append(str(res))
print(' '.join(output), end=' ')
print()
Wait actually, no, the code:
Wait the code written in the previous answer has in the loop over T:
code
print(' '.join(output), end=' ')
print()
Wait that would print the joined list followed by space, then a new-line with the print(). Thus the line ends with a space and a new-line which may have an extra space. However, in the sample's first line, the output is like "0 0 0 2 4 " (ends with space). So perhaps the correct way is to separate with spaces and not have trailing space.
Alternatively, perhaps the ' '.join is sufficient, and the print() already adds the new line.
Wait adjusting the code:
The correct way is to print the list joined by spaces, and then a new-line. So replacing the last lines with:
print(' '.join(output))
Which would handle it properly. In my initial code, I had a mistake in the last prints.
This is likely a bug in code above. Let's see.
In the code provided:
code
output = []
for __ in range(q):
...
print(' '.join(output), end=' ')
print()
This way the joined list plus a space and then a new line → leading to a trailing space per line. Which must not be correct.
The correct approach is to just print ' '.join(output), without the end.
Thus fixing the code:
Inside the loop over test cases:
After collecting the output list, we do:
print(' '.join(output))
Thus, here's the corrected code:
python
import sys
def compute_gcd(a, b):
while b:
a, b = b, a % b
return a
def main():
input = sys.stdin.read().split()
idx = 0
T = int(input[idx])
idx += 1
for _ in range(T):
a = int(input[idx])
b = int(input[idx+1])
q = int(input[idx+2])
idx +=3
gcd_val = compute_gcd(a, b)
p = (a * b) // gcd_val
# Compute good array
good = [0]*p
for x in range(p):
moda = x % a
modb_x = x % b
f = moda % b
g = modb_x % a
if f != g:
good[x] = 1
# Compute prefix array
prefix = [0] * p
prefix[0] = good[0]
for i in range(1, p):
prefix[i] = prefix[i-1] + good[i]
total_per = prefix[-1]
# Process queries
output = []
for __ in range(q):
l = int(input[idx])
r = int(input[idx+1])
idx +=2
def count(x):
if x < 0:
return 0
q_x = x // p
rem = x % p
return q_x * total_per + prefix[rem]
res = count(r) - count(l-1)
output.append(str(res))
print(' '.join(output))
if __name__ == "__main__":
main()
This way, the outputs are joined by spaces and printed with a single line per test case, matching the sample correctly.
For example, in the first sample input's first case, the output is "0 0 0 2 4", which is what the code now produces (without trailing space). The sample's output has a trailing space, but in coding platforms, trailing spaces are usually accepted, but better to match correctly.
However, in the problem's sample output given: the first line's output is "0 0 0 2 4 " (with space), but the second line "0 91" (no space after 91, so perhaps an error in the original problem's sample, or perhaps my misinterpretation.
In any case, the correct way is to have space-separated numbers with no trailing space, hence the code should have ' '.join(output) and no space at the end. The sample shows some lines with trailing but we can proceed with this code.