A prime number n (11, 19, 23, etc.) that divides by 4 and has 3 has an interesting property. The results of calculating the remainder of the square of a natural number (1, 2, ..., n -1) of 1 or more and less than n divided by n are the same, so they are different from each other. The number is (n -1) / 2.
The set of numbers thus obtained has special properties. From the resulting set of numbers, choose two different a and b and calculate the difference. If the difference is negative, add n to the difference. If the result is greater than (n -1) / 2, subtract the difference from n.
For example, when n = 11, the difference between 1 and 9 is 1 − 9 = −8 → −8 + n = −8 + 11 = 3. The difference between 9 and 1 is also 9 −1 = 8 → n − 8 = 11 − 8 = 3, which is the same value 3. This difference can be easily understood by writing 0, 1, ···, n -1 on the circumference and considering the shorter arc between the two numbers. (See the figure below)
The "difference" in the numbers thus obtained is either 1, 2, ..., (n -1) / 2, and appears the same number of times.
[Example] When n = 11, it will be as follows.
Calculate the remainder of the square of the numbers from 1 to n-1 divided by n.
Calculate the difference between different numbers for 1, 3, 4, 5, 9 obtained in 1.
If the calculation result is negative, add n = 11.
In addition, if the result of the calculation is greater than (n-1) / 2 = 5, subtract from n = 11.
Find the number of appearances
Count the number of occurrences of the calculation results 1, 2, 3, 4, and 5, respectively.
From these calculation results, we can see that 1, 2, 3, 4, and 5 appear four times. This property is peculiar to a prime number that is 3 when divided by 4, and this is not the case with a prime number that is 1 when divided by 4. To confirm this, a program that takes an odd number n of 10000 or less as an
input, executes the calculation as shown in the example (finds the frequency of the difference of the square that is too much divided by n), and outputs the number of occurrences. Please create.
Input
Given multiple datasets. One integer n (n ≤ 10000) is given on one row for each dataset. The input ends with a line containing one 0.
Output
For each data set, output the frequency of occurrence in the following format.
Number of occurrences (integer) of (a, b) where the difference between the squares of the remainder is 1
The number of occurrences (integer) of (a, b) where the difference between the squares of the remainder is 2
::
::
The number of occurrences (integer) of (a, b) where the difference between the squares of the remainder is (n-1) / 2
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We have to parse the problem carefully. It's about prime numbers that are 3 mod 4? But the problem says: "A prime number n (11, 19, 23, etc.) that divides by 4 and has 3 has an interesting property." This translation seems to indicate a prime number n such that n ≡ 3 (mod 4). Then it says: "The results of calculating the remainder of the square of a natural number (1, 2, ..., n -1) of 1 or more and less than n divided by n are the same, so they are different from each other. The number is (n -1) / 2." Actually, the statement seems to be: For a prime n ≡ 3 mod 4, the set of quadratic residues modulo n (excluding 0) has size (n-1)/2, and they are all distinct. Then from that set, we take two different numbers a and b and compute a "difference" as defined: compute a-b (or b-a?) Actually the example: for n=11, they compute the set of quadratic residues mod 11 from numbers 1..10: they get {1,3,4,5,9}. Then they pick two different a,b from this set. Then they compute "difference": if a - b (or a-b?) yields negative, add n; then if result > (n-1)/2, subtract from n. So essentially, they map the absolute cyclic distance to a number between 1 and (n-1)/2 (the minimal distance on the circle of numbers 0..n-1). The example: difference between 1 and 9: 1-9 = -8 -> +11 = 3; (3 is ≤5, so keep). Also difference between 9 and 1: 9-1=8 -> >5 so compute 11-8=3. So indeed they take the minimal positive distance on the circle. So the "difference" is the smaller of (|a-b|, n - |a-b|), but since they consider ordered pairs? They say choose two different a and b and calculate the difference. It seems they consider unordered pairs? But in counting occurrences, they compute all differences for all unordered pairs? Actually they say: "From the resulting set of numbers, choose two different a and b and calculate the difference." Then "The 'difference' in the numbers thus obtained is either 1,2,..., (n-1)/2, and appears the same number of times." So the claim is that for n prime ≡ 3 mod 4, if we compute the minimal cyclic difference for all unordered pairs of distinct quadratic residues modulo n, each possible distance 1,2,...,(n-1)/2 appears equally often. The example for n=11: there are 5 residues. Number of unordered pairs = C(5,2)=10. They compute differences for each unordered pair, and count occurrences of each distance 1..5. The output shows 5 lines (for distances 1 to 5) each with number of occurrences = 4? Wait, they said 4 each? Actually they said: "From these calculation results, we can see that 1, 2, 3, 4, and 5 appear four times." For 5 distances, each appears 4 times, total 54=20, but there are only 10 unordered pairs. Something's off: 10 pairs can't produce 20 counts. Unless they count ordered pairs (a,b) with a≠b, then number of ordered pairs = 54=20. Indeed in the example: they say "choose two different a and b and calculate the difference." It doesn't specify ordered or unordered, but the calculations they show: they compute 1-9 and 9-1 separately. They got both yielding 3. So they are counting ordered pairs (a,b) with a ≠ b. So total number of ordered pairs = m*(m-1) where m = (n-1)/2. For n=11, m=5, ordered pairs =20. They claim each distance appears 4 times, so 5 distances * 4 = 20, consistent. So they consider ordered pairs of distinct residues.
Thus the property: For prime n ≡ 3 mod 4, for the set of quadratic residues modulo n (non-zero), the distribution of minimal cyclic distances for ordered pairs is uniform over {1,..., (n-1)/2}. The problem asks: "To confirm this, a program that takes an odd number n of 10000 or less as an input, executes the calculation as shown in the example (finds the frequency of the difference of the square that is too much divided by n), and outputs the number of occurrences. Please create." Wait: "finds the frequency of the difference of the square that is too much divided by n" maybe translation: "finds the frequency of the difference of the squares of the remainder" Actually they phrase: "executes the calculation as shown in the example (finds the frequency of the difference of the square that is too much divided by n), and outputs the number of occurrences." So they want us to, for each dataset (odd n ≤ 10000), compute the frequencies as described, and output them each on a new line from difference 1 to (n-1)/2.
But then they also mention: "This property is peculiar to a prime number that is 3 when divided by 4, and this is not the case with a prime number that is 1 when divided by 4." So for non-prime or for primes ≡ 1 mod 4, the frequencies might not be equal. However, the problem seems to ask: for any odd number n ≤ 10000 (maybe not necessarily prime?), we have to compute the frequencies as described. Because they say: "a program that takes an odd number n of 10000 or less as an input, executes the calculation as shown in the example... outputs the number of occurrences." They don't say n is prime; they just say odd number n ≤ 10000. So we must implement the algorithm for any odd n? The property might only hold for primes ≡ 3 mod 4, but we are to compute frequencies for any given odd n. So we just follow the steps:
Compute the set R of residues of squares modulo n for numbers 1 to n-1. Actually they said: "The results of calculating the remainder of the square of a natural number (1, 2, ..., n -1) of 1 or more and less than n divided by n are the same, so they are different from each other." That's a claim about primes ≡ 3 mod 4 that the squares produce distinct residues? Wait, they said "are the same, so they are different from each other." Probably translation: "The results ... are all distinct, so the number of distinct residues is (n-1)/2." Actually that property holds for any odd prime (or any prime? Actually for any prime p, the quadratic residues (non-zero) are exactly (p-1)/2 distinct numbers). For composite n, squares modulo n can have repeats; the set of residues from 1..n-1 may be smaller than (n-1)/2. So the problem does not restrict to primes. So we must compute the set R = { (i*i) mod n | i=1,..., n-1 }. Then the set might have duplicates; but they say "the results ... are the same, so they are different from each other" meaning maybe they consider the distinct residues? Actually the example: n=11 (prime) yields set of residues: {1,4,9,5,3} exactly 5 distinct numbers. But if there are duplicates, do we treat the set as a multiset or as a set of distinct numbers? The problem statement: "The results of calculating the remainder of the square of a natural number (1, 2, ..., n -1) of 1 or more and less than n divided by n are the same, so they are different from each other. The number is (n -1) / 2." That's confusing. Probably they mean: For prime n ≡ 3 mod 4, the residues are all distinct and the count is (n-1)/2. But then later they say: "From the resulting set of numbers, choose two different a and b and calculate the difference." So the set is the set of residues obtained (presumably distinct). But if n is not prime, the squares mod n may have repeats; do we consider only distinct residues? Or do we consider the list with multiplicities? The example uses distinct residues. The problem says "set of numbers" implying distinct. Also in step 1 they show computing remainders, but they list duplicates (like 3 appears twice for 5^2 and 6^2), but then they say "obtained in 1." Actually they say: "1. Calculate the remainder of the square of the numbers from 1 to n-1 divided by n." Then they list: 1,4,9,5,3,3,5,9,4,1. That's 10 numbers with duplicates. Then they say: "Calculation of 'difference' between a and b: 1. Calculate the difference between different numbers for 1, 3, 4, 5, 9 obtained in 1." Wait reading: "1. Calculate the difference between different numbers for 1, 3, 4, 5, 9 obtained in 1." Actually the original: "2. Calculation of "difference" between a and b
Calculate the difference between different numbers for 1, 3, 4, 5, 9 obtained in 1.
If the calculation result is negative, add n = 11.
In addition, if the result of the calculation is greater than (n-1) / 2 = 5, subtract from n = 11.
Find the number of appearances"
Interpretation: From step 1, they got numbers: 1,4,9,5,3,3,5,9,4,1. But then they say "the numbers obtained in 1" which might be the set of distinct residues? But they list "1,3,4,5,9" explicitly: that's the distinct set sorted? In step 2, they say "Calculate the difference between different numbers for 1, 3, 4, 5, 9 obtained in 1." Possibly they first take the distinct residues. Because the text says "the results ... are the same, so they are different from each other" which is not clear. But looking at the example: they list all squares, then for difference they use only the distinct ones? They say "for 1, 3, 4, 5, 9 obtained in 1." That's the distinct set. So likely the set is the set of distinct quadratic residues modulo n (non-zero). But then why compute the full list with duplicates? They just showed it for illustration, but the distinct set is what matters. Also the property they claim (equal frequencies) is for primes ≡ 3 mod 4 and would not hold if duplicates are included? Let's test: For n=11, if we consider the multiset of 10 residues (with duplicates), and take ordered pairs of distinct elements (but duplicates could be from same residue value? If we treat each occurrence as distinct (e.g., positions), then we could have pairs where the numbers are equal? But they say choose two different a and b, likely meaning distinct positions? But if we have duplicate residues, then there could be pairs where a and b are the same residue but from different i? Would they be considered different numbers? The text says "choose two different a and b" without specifying if they are from the set (maybe they mean distinct elements of the set, not necessarily distinct i). In set theory, a set has unique elements, so duplicates are not considered. So it's natural to interpret the "set of numbers thus obtained" as the set of distinct residues.
Thus, for any odd n, we first compute the set S = { (i*i) mod n : 1 ≤ i ≤ n-1 } (distinct values). Then S may have size m (which is ≤ (n-1)/2 if n prime, but can be less for composite). Then we consider all ordered pairs (a,b) with a,b ∈ S, a ≠ b. For each ordered pair, we compute d = a - b (as integer difference), then if d < 0, add n to make it positive? Actually they do: if negative, add n; then if result > (n-1)/2, subtract from n. So we compute: d1 = (a - b) mod n? Actually they don't take mod, they do: if negative add n, then if > (n-1)/2, compute n - that value. This yields a number between 1 and (n-1)/2 inclusive? The output examples show distances from 1 to (n-1)/2. In case a-b = 0? Not allowed because a≠b, but could be 0 mod n? Since a and b are between 0 and n-1, a≠b so difference not 0. But could a-b = n? No because a,b in [0,n-1], max diff n-1, min - (n-1). So after adjusting negative, we get positive in 1..n-1. Then if > (n-1)/2, we take n - that, which yields a number in 1..(n-1)/2. So essentially we compute the minimal positive distance: min( (a-b) mod n, n - ((a-b) mod n) )? But careful: (a-b) mod n yields a number in [0,n-1). Since a≠b, mod is not 0. Actually mod gives a number in 1..n-1. Then min(d_mod, n-d_mod). That yields a number from 1..floor((n-1)/2). Since n is odd, (n-1)/2 is integer. So we compute d = min( (a-b) mod n, n - ((a-b) mod n) ). That is the "cyclic difference". That is what they compute.
Then we count frequencies for each possible d from 1 to (n-1)/2. The total number of ordered pairs = m*(m-1). Then we output the frequency for each d (as an integer) on separate lines, for each dataset. The example input has 11 and 15. Let's test with n=15 (odd composite). Compute squares modulo 15: i from 1 to 14:
Set of distinct residues: {1,4,9,10,6}? Wait also maybe? Let's collect: 1,4,9,1,10,6,4,4,6,10,1,9,4,1 => distinct: {1,4,6,9,10}. That's 5 distinct residues. (n-1)/2 = 7. So output expects lines for d=1..7? In example output after the 5 lines for n=11, there are more lines for n=15. Let's check the provided example input and output:
Input:
11
15
0
Output:
4
4
4
4
4
2
2
4
2
4
4
2
Wait that's a lot of numbers. Let's count: after the five 4's for n=11, there should be outputs for n=15. For n=15, (n-1)/2=7, so we need 7 numbers. The total output given: There are 5 lines of '4'? Actually they list:
4
4
4
4
4
2
2
4
2
4
4
2
That's 12 numbers. For n=11, need 5 numbers. So after 5 numbers, the remaining 7 numbers would correspond to n=15. The provided output: first five are all 4 (matches n=11). Then next seven? Let's separate: after 5 "4"s, we have: 2,2,4,2,4,4,2? That's 7 numbers: positions: 6:2, 7:2, 8:4, 9:2, 10:4, 11:4, 12:2. So that is the output for n=15: frequencies for d=1..7 are [2,2,4,2,4,4,2]? But let's verify if that matches our computation.
b=9: (1-9)=-8 mod15=7, min(7,8)=7? Actually 7 ≤ 7? (n-1)/2=7, so 7 is allowed. But if result >7, we would subtract from n: 7 is not >7, so keep 7. So distance=7? Wait min(7,8)=7. But careful: the described procedure: if difference (after making positive by adding n if negative) > (n-1)/2, then subtract from n. So with a=1, b=9: 1-9=-8 +15=7. That's exactly (n-1)/2=7, so not >7, so result =7. So distance 7.
b=10: 1-10=-9 +15=6, distance=6 (since 6 ≤7).
a=4:
b=1: 4-1=3, ≤7 =>3.
b=6: 4-6=-2+15=13 >7 => 15-13=2? Actually procedure: if result > (n-1)/2, subtract from n. So 13 >7, so n - 13 = 2. So distance=2.
b=9: 4-9=-5+15=10 >7 =>15-10=5.
b=10: 4-10=-6+15=9 >7 =>15-9=6.
a=6:
b=1: 6-1=5, ≤7 =>5.
b=4: 6-4=2, ≤7 =>2.
b=9: 6-9=-3+15=12 >7 =>15-12=3.
b=10: 6-10=-4+15=11 >7 =>15-11=4.
a=9:
b=1: 9-1=8 >7 =>15-8=7? Actually 8 >7, so n-8=7. So distance=7.
b=4: 9-4=5, ≤7 =>5.
b=6: 9-6=3, ≤7 =>3.
b=10: 9-10=-1+15=14 >7 =>15-14=1.
a=10:
b=1: 10-1=9 >7 =>15-9=6.
b=4: 10-4=6, ≤7 =>6.
b=6: 10-6=4, ≤7 =>4.
b=9: 10-9=1, ≤7 =>1.
Now count frequencies for distances 1..7:
d=1: occurrences: from (9,10) and (10,9)? Actually from a=9,b=10 gave 1; a=10,b=9 gave 1. Any others? Also maybe? Let's list all with d=1: (9,10)=1; (10,9)=1. That's 2.
d=2: from (4,6)=2; (6,4)=2; any others? (Maybe also? (???) ) So count=2.
d=3: from (1,4)=3; (4,1)=3; (6,9)=3; (9,6)=3. That's 4.
d=4: from (6,10)=4; (10,6)=4; maybe (???) Also (???) Actually check: (10,6)=4; (6,10)=4; any others? (???) That's 2? Wait also (???) from (???) Let's see if any other gave 4: (???) (9,?) none. (1,?) no. (4,?) no. (10,6)=4 and (6,10)=4. So only 2? But we had (???) Wait (4,?) (???) Possibly (???) (?) Actually check (???) (???) (???) Also (???) from (???) I may have missed (???) (???) Actually from a=10,b=4 gave 6; a=4,b=10 gave 6. So not 4. a=6,b=? gave 4 only with 10. a=10,b=6 gave 4. So two. But output for n=15 shows d=4 =? The output order: d=1:2; d=2:2; d=3:4; d=4:2; d=5:4; d=6:4; d=7:2. Let's check our d=5: occurrences: from (1,6)=5; (6,1)=5; (4,9)=5; (9,4)=5 => 4. d=6: from (1,10)=6; (10,1)=6; (4,10)=6; (10,4)=6 => 4. d=7: from (1,9)=7; (9,1)=7; maybe (9,1) we already have? (1,9)=7; (9,1)=7; also (???) (???) (???) (???) Actually also (9,1)=7; (1,9)=7; also (???) maybe (???) (???) (???) That's 2. So our counts: d1=2, d2=2, d3=4, d4=2, d5=4, d6=4, d7=2. That matches the provided output: after the 5 fours, we have: 2,2,4,2,4,4,2. Perfect. So indeed they used distinct residues.
Thus the problem is: For an odd integer n (≤10000), compute the set S of distinct quadratic residues modulo n from the squares of numbers 1..n-1. Then consider all ordered pairs (a,b) with a,b ∈ S, a≠b. For each ordered pair, compute the minimal cyclic distance d on the circle of numbers 0..n-1 (i.e., the smaller of (a-b) mod n and n - ((a-b) mod n) which yields a number from 1 to (n-1)/2). Count the frequency of each d (d = 1,2,..., (n-1)/2). Output each frequency on a separate line, in order.
Edge cases: n might be 1? But they said odd number n of 10000 or less, and input ends with 0. n might be 1? Possibly but 1 is odd ≤10000. However, the process: numbers from 1 to n-1 = 0, so set S would be empty? Then (n-1)/2 = 0, so maybe no output? But n likely > 1. We can assume n is odd and >=3? Possibly. They gave example 11 and 15, both >=3. I'll assume n>=3.
Also note: For composite n, the set S may be smaller than (n-1)/2. Then some distances might have zero occurrences. We should output zeros for those distances? The problem doesn't explicitly mention zero occurrences, but likely we output the count (which may be 0). In the example, n=15 had distances 1..7, all had some positive counts, but not all distances necessarily will be non-zero; we should output the integer (maybe zero) for each. The problem says "output the number of occurrences" for each difference.
Thus, algorithm per dataset:
Read n until 0.
Compute residues = set()
For i in 1..n-1: residues.add((i*i) % n)
Let residues_list = sorted? Not needed. But we need to iterate over all ordered pairs. Size m = len(residues). m can be up to (n-1)/2 at most (since for prime n, exactly (n-1)/2). But n up to 10000, m up to about 5000. Ordered pairs count would be m*(m-1) up to about 25 million for n=10000? Actually (5000*4999) ≈ 25 million. That's manageable if we are careful? 25 million operations per dataset might be borderline but probably okay in C++ but in Python might be slow if we have many datasets. But we have no constraint on number of datasets; but typical programming contest problems have maybe up to dozens of datasets. 25 million per dataset in Python might be okay if optimized (maybe 0.1 sec per 10 million operations? Actually Python loops are slower; 25 million simple arithmetic operations might take ~1 second or more; multiple datasets could exceed time. But we can attempt to optimize.
However, note that n ≤ 10000, m ≤ 5000, ordered pairs ~25e6. That's probably okay for a single dataset, but if there are many, could be a problem. But we can improve: we can compute the distribution without explicitly enumerating all ordered pairs using combinatorial reasoning? The property of uniform distribution is only for primes 3 mod 4. But for arbitrary n, we need to compute directly. However, there may be a more efficient method: The set S is the set of squares mod n. We can compute the distance counts by iterating over all unordered pairs? But still O(m^2). Could we use an array of size n to mark residues, then compute contributions using convolution? Possibly, but for n up to 10000, O(m^2) might be acceptable if we code efficiently in Python, using numpy? Probably not allowed. But let's evaluate worst-case: n=9973 (prime ~10000) gives m=4986. Ordered pairs = about 24.86 million. In Python, a double nested loop over m elements (list) doing a few operations: say we iterate i over m, j over m, j≠i, compute diff. That's m*(m-1) ~ 24.86e6 iterations. In CPython, that might take around 1-2 seconds if the loop is tight. But then we also have to handle other datasets. Probably okay within typical time limits (maybe 2 sec). However, there may be multiple such large n; worst-case sum of m^2 over datasets could be large. But the problem statement may be from an old contest where time limits are generous. We'll aim for O(m^2). But we can try to improve constant factors: Precompute residues list; precompute (n - r) maybe? Actually we compute for each ordered pair: d = (a - b) % n; if d > half: d = n - d. Since n is fixed.
We can also note that we need counts for each distance 1..half. We can initialize an array freq of size half+1 (1-indexed). Then iterate i over m, j over m, if i != j: compute diff = (res[i] - res[j]) % n; if diff > half: diff = n - diff; freq[diff] += 1. This is straightforward.
But we need to be careful: For large m, double loop with Python's list indexing and mod may be okay. 25 million loops with simple arithmetic might be about 0.2-0.3 sec? Actually Python loops are slower: typically 50 million simple ops per second? Let's test mental: Python can do about 50 million simple integer additions per second on a fast machine? Actually typical CPython can do around 30-50 million operations per second if very simple? But with modulus and conditionals, slower. 25 million loops might take ~0.5-1 sec. For multiple datasets, maybe fine.
But we can consider optimizing by using array of residues and using precomputed differences? Another approach: Since we need distances on circle, we can sort residues and then use two-pointer? But not straightforward because the circle wraps. However, we could treat residues as points on a circle and count the number of ordered pairs at each distance. For a set of points on a circle, the distances between all ordered pairs can be computed via convolution using FFT? That would be O(n log n) but n is only up to 10000, so O(n^2) is acceptable.
Given the problem likely from a simple programming contest, they expect straightforward O(m^2) solution.
But wait: The set S might be smaller than half. For n=9999 (odd composite), m could be less, maybe much less. In worst case, n is prime, m is maximal (n-1)/2. So that is worst-case.
Thus we can implement as described.
Now we must also consider input: multiple lines, each an integer n, ending with 0. Output for each dataset: print frequencies for distances 1 to (n-1)/2 each on separate line. So each dataset's output is (n-1)/2 lines. There is no blank line between datasets.
Now, the example output: For n=11, they output 5 lines: all 4. Then for n=15, they output 7 lines: 2,2,4,2,4,4,2. And they are all on separate lines with no extra text.
Thus, our program should produce exactly that.
Now, double-check: Are we supposed to consider the set of residues including 0? The problem says "remainder of the square of a natural number (1, 2, ..., n -1) of 1 or more and less than n divided by n" So squares of numbers from 1 to n-1, not including 0, so 0 is not included. So S does not contain 0. Good.
Now, what about n that are prime but ≡ 1 mod 4? According to the property, the frequencies are not uniform. But we still compute as per algorithm. So that's fine.
One more nuance: The problem says "The number is (n -1) / 2." That might refer to the size of the set. So for prime n, the number of distinct residues is (n-1)/2. For composite, it may be less. So in algorithm we use the set of distinct residues, not multiset.
Now, confirm the description of difference: They say "If the result is greater than (n -1) / 2, subtract the difference from n." Actually earlier they said "If the result is greater than (n -1) / 2, subtract the difference from n." But in the example, they compute a-b (positive after possible addition), then if >5, subtract from n. For a=4,b=6: 4-6 = -2 -> +11 = 9? Wait they didn't show that example. They showed 9-1: 8 -> n - 8 = 3. So they compute absolute difference? Actually 9-1=8 >5 so they did 11-8=3. That is indeed min(|9-1|, n - |9-1|) = min(8,3) = 3. So algorithm: d = (a - b) (maybe signed); if d < 0: d += n; then if d > half: d = n - d. That yields the minimal positive distance. Equivalent to d = (a - b) % n; if d > half: d = n - d.
Thus implemented.
Now, let's ensure we compute mod correctly: In Python, (a - b) % n gives a number in 0..n-1. Since a != b, result is 1..n-1. Then if d == 0? That would only happen if a==b modulo n, but a,b are both in [0,n-1] and distinct, so not equal. So d in 1..n-1. Then if d > half: d = n - d. Since n is odd, half = (n-1)//2, and n-d will be in range 1..half. So final d ∈ [1, half].
Now, we need to count frequencies.
Implementation steps:
For each n:
If n == 0: break.
half = (n-1)//2
Compute set S: set()
for i in range(1, n): S.add((i*i) % n)
Convert to list res = list(S) (order doesn't matter)
m = len(res)
Initialize freq array of length half+1 (index 0 unused) all zeros.
For i in range(m):
a = res[i]
for j in range(m):
if i == j: continue
b = res[j]
d = (a - b) % n
if d > half: d = n - d
freq[d] += 1
After loops, for d in range(1, half+1): print(freq[d])
Optimization: To avoid the inner if i==j, we can loop j over all indices and then subtract something? But simpler is to keep the if. Could also iterate j != i by using for j in range(m): if i != j. That's fine.
Potential micro-optimizations: Precompute d for each pair? Not necessary.
But we can further optimize by noting that freq[d] will be symmetric: each ordered pair (a,b) contributes to some d; we might compute only for i<j and double the count for d when a,b produce same d both ways? But careful: For ordered pairs, (a,b) and (b,a) may produce the same d? Actually minimal distance is symmetric: the distance from a to b is same as from b to a because min(|a-b|, n-|a-b|) is symmetric. So for unordered pair {a,b}, both ordered pairs produce the same d. So if we compute only unordered pairs, we can multiply by 2. But note: What if the distance d equals half? For odd n, half = (n-1)/2, and if |a-b| = half? Actually since n is odd, n is odd, half = (n-1)/2 integer. Could it happen that the distance d = half? Yes, for example n=11, distance 5? Actually half=5, and there is pair (1,6?) but 6 is not in S. But in n=11, half=5, pairs like (1,6?) not applicable. But in n=15, half=7, we had distance 7 from (1,9) etc. So symmetric. So we can compute unordered pairs and then double count. That reduces operations by factor ~2. But still O(m^2/2). Still okay.
But note: Could there be cases where the distance is computed as exactly half? half = (n-1)/2. Then d > half? Since half is integer, condition d > half; if d == half, we do not subtract. So d = half is possible. That's fine.
Now, we should also consider that the set S may be small. For n=1, half=0, no output? But n=1 not given likely.
Now, edge: What if n=3? Then half=1. Squares: 1^2=1 mod3=1, 2^2=4 mod3=1. Set S = {1}. m=1. Then there are no ordered pairs (since only one element). So freq[1] should be 0. Output "0". That seems reasonable.
Now, we must output for each d from 1 to half. For n=3, half=1, output one line: 0.
Now, test n=5 (prime, but 5 ≡ 1 mod4). half=2. Squares: 1^2=1,2^2=4,3^2=9 mod5=4,4^2=16 mod5=1 => S={1,4}. m=2. Ordered pairs: (1,4): d = (1-4)%5 = 2? (1-4=-3+5=2, half=2, not >2, d=2) ; (4,1): (4-1)=3 >2? 3>2 so n-3=2 => d=2. So both give d=2. So frequencies: d=1:0, d=2:2. Output: 0\n2. That would be the answer.
Now, if we implement, it should work.
Potential pitfalls: The set S might include 0? If n is not prime, squares of numbers that are multiples of sqrt? For i multiple of factors? For example, n=9 (odd composite). Squares: 1^2=1,2^2=4,3^2=0,4^2=7,5^2=7,6^2=0,7^2=4,8^2=1. So S = {0,1,4,7}? But 0 is included? Since we compute squares of numbers from 1 to n-1 (i=3 gives 9 mod9=0). So 0 appears. But the problem says "remainder of the square... of 1 or more and less than n divided by n", yes remainder can be 0. Are we supposed to include 0 in the set? The original statement: "The results of calculating the remainder of the square of a natural number (1, 2, ..., n -1) of 1 or more and less than n divided by n are the same, so they are different from each other. The number is (n -1) / 2." This seems to be about prime n: For prime n, remainder 0 occurs only for multiples of n, but i from 1 to n-1, squares mod prime are never 0 because p does not divide i. So for prime, 0 is not in the set. For composite, 0 can appear. Does the problem intend to include 0? The example for n=15 did not include 0, because no i gives 0 mod 15? Actually 15 composite, squares: 15 divisible by 3 and 5. Could any square be divisible by 15? i would need to be multiple of 15, but i<15, so no. So 0 not present. For n=9, 0 would be present. Should we include 0? The problem description is ambiguous. The phrase "set of numbers thus obtained" refers to the results of calculating the remainder. That would include 0 if it occurs. But later they say "The number is (n-1)/2." That is only true for primes. So maybe they are only interested in primes? But the program says: "a program that takes an odd number n of 10000 or less as an input, executes the calculation as shown in the example (finds the frequency of the difference of the square that is too much divided by n), and outputs the number of occurrences." They don't restrict to primes. So for composite n, we should follow the exact steps: compute all squares of numbers 1..n-1, take the set of remainders (which may include 0). But in the example, they didn't include 0 because n=11 (prime) and n=15 (no 0). For n=9, 0 would be included. So we need to decide.
Let's examine the original Japanese problem maybe? The description: "A prime number n (11, 19, 23, etc.) that divides by 4 and has 3 has an interesting property." So they start talking about primes ≡3 mod4. Then they describe the property. Then they say: "This property is peculiar to a prime number that is 3 when divided by 4, and this is not the case with a prime number that is 1 when divided by 4. To confirm this, a program that takes an odd number n of 10000 or less as an input, executes the calculation as shown in the example (finds the frequency of the difference of the square that is too much divided by n), and outputs the number of occurrences." So it appears the program is to verify the property: For a given odd n, compute these frequencies, and see that for primes ≡3 mod4 they are equal, while for others they are not. So we must compute exactly as described, which should be the same as for primes: compute squares from 1..n-1, take the set of residues (including 0? Actually for primes, 0 not included, so they'd just get quadratic residues). For composite, they'd get some set including possibly 0. So we should include 0 if it appears.
But the difference computation: "choose two different a and b and calculate the difference." If 0 is in the set, then we include pairs involving 0. The circle includes 0 as a point. That is fine. The algorithm for difference still works. So we should include 0.
But we must check: In the problem's step 1, they list the squares: for n=11, they got residues: 1,4,9,5,3,3,5,9,4,1. That list includes duplicates but not 0. For composite n like 9, the squares list would include 0. So the set would include 0. Then they'd choose two different numbers from that set, which may include 0. So we should include it.
But is there any special handling? The "difference" as defined: it's the cyclic distance on the circle of numbers 0,...,n-1. So 0 is just another number. So it's fine.
Thus our algorithm: compute residues set = { (i*i) % n for i in range(1,n) }.
But careful: The set includes distinct residues; if 0 appears, include it.
Now, the number of distances we output is (n-1)/2. That includes all possible distances from 1 to half. For pairs involving 0, distances computed similarly.
Now, is there any scenario where the distance might be 0? Not if a≠b, because if a and b are distinct residues, the minimal distance is at least 1. So fine.
Now, is there any scenario where the distance computed might be something else? For example, if n is even? But n is odd, so half is integer.
Now, we need to output frequencies for each d in 1..half. If half is 0 (n=1), no output? But we can safely skip if n==1 (maybe not in input). If n=1, we could just output nothing? But I'll assume n>=3.
Now, let's test with a small composite including 0: n=9.
n=9, half=4. Compute squares mod9 from 1..8:
1->1,2->4,3->0,4->7,5->7,6->0,7->4,8->1. Set S = {0,1,4,7}. m=4. Ordered pairs: 4*3=12.
Compute distances: n=9, half=4.
List points: 0,1,4,7.
We'll compute for each ordered pair (a,b), a≠b.
We can compute d = (a - b) % 9; if d>4 then d=9-d.
Now count d=1: from (0,1) and (1,0) => 2.
d=2: from (0,7) and (7,0) => 2.
d=3: from (1,4),(1,7),(4,1),(4,7),(7,1),(7,4) => that's 6? Actually list: (1,4)=3, (1,7)=3, (4,1)=3, (4,7)=3, (7,1)=3, (7,4)=3 => 6.
d=4: from (0,4) and (4,0) => 2.
So frequencies: d=1:2, d=2:2, d=3:6, d=4:2.
Now total ordered pairs = 12; sum = 2+2+6+2=12. Good.
Now output would be 4 lines: 2,2,6,2.
That seems plausible.
Now, what about n that are prime ≡ 1 mod 4? n=13, half=6. Let's compute quickly to see if uniform? Likely not.
So algorithm stands.
Now, consider performance. For n up to 10000, m ≤ n/2 approx 5000. Loops 25 million. That's okay in Python if optimized and maybe using PyPy.
But we must also consider that n can be composite with m possibly larger? Actually maximum distinct residues from squares modulo n: For a prime p, there are (p-1)/2 residues. For composite, it's at most φ(n)/2^k? Actually the number of squares modulo n is related to the number of quadratic residues modulo n. For n composite, the number of squares modulo n can be less than (n-1)/2, but could it be larger? No, because if n is prime, the residues are exactly half of non-zero. For composite, the number of squares (including 0) might be more? Let's consider n=prime power. For n=9, we had 4 residues (0,1,4,7). (n-1)/2 =4. So m=4 = half+? Actually half=4, so m=4. So same size? Wait half= (9-1)/2 =4. m=|S|=4. That's exactly half? Actually (n-1)/2 =4, but we also include 0? Did we include 0? Yes, S includes 0. So size is 4, which equals half. For n=9, half=4, so size m=4. For n=15, half=7, m=5 <7. So m can be at most half+1? Because 0 could be included, making size possibly up to half+1? Let's find max. The total number of quadratic residues modulo n (including 0) is something like: For n = product of distinct odd primes, the number of squares is (∏ ( (p_i+1)/2 )) I think. For n=3*5=15, ( (3+1)/2 * (5+1)/2 ) = (2 * 3) =6? That would be 6 including 0? Actually including 0 gives 6. But we got 5 distinct residues (excluding 0 because 0 didn't appear). Wait 0 appears only if i is multiple of all prime factors? For n=15, to have i^2 ≡ 0 mod 15, i must be divisible by 15, but i<15, so no. So 0 not included. So size = number of nonzero squares = 5, which is less than 6. So maximum size might be less than or equal to (n-1)/2 + 1? For n prime, size = (n-1)/2 (0 not included). For n=9, size=4 (including 0) which is equal to (9-1)/2 =4. For n=27 maybe? Not sure. But worst-case m might be close to n/2. So O(m^2) worst-case still about (n/2)^2 = n^2/4 ≈ 25 million for n=10000. That's okay.
But we need to be careful about Python's speed. Let's try to estimate: A loop with 25 million iterations performing a few operations: mod, comparison, addition, indexing. Python can handle maybe 50-100 million simple ops per second? Actually typical CPython can do about 30 million integer additions per second on a modern CPU? But modulus and branching are slower. It might take around 1-2 seconds. In an online judge, 2 seconds might be okay if only one dataset. But there could be multiple large datasets. The problem might only have a few datasets. Usually they give up to maybe 10 datasets. That would be 10*2=20 seconds, too much. But maybe the input will not have many large n. However, to be safe, we might want to optimize.
We could use C++ but we're to write in Python. We can adopt some micro-optimizations:
Use list of residues as Python list.
Precompute half.
Use local variables for n, half, freq (list), and maybe use Python's built-in functions.
Avoid modulus by using if statements? Actually we can compute diff = a - b; if diff < 0: diff += n; then if diff > half: diff = n - diff. That might be slightly faster than modulus? Possibly. Let's test: In Python, modulus operator % is not super slow but might be similar. We'll implement with ifs.
Use for i in range(m): a = res[i]; for j in range(m): if i == j: continue; b = res[j]; diff = a - b; if diff < 0: diff += n; if diff > half: diff = n - diff; freq[diff] += 1
That's fine.
Could use for i in range(m): a = res[i]; for b in res: if a == b: continue; ... This is similar.
Alternatively, we can precompute a distance matrix? No.
Another optimization: Because we need ordered pairs, we could compute unordered pairs and multiply by 2, but careful: For unordered pairs, we need to count both directions. Since distance is symmetric, we can do:
for i in range(m):
for j in range(i+1, m):
a = res[i]; b = res[j];
diff = a - b; if diff < 0: diff = -diff? Actually not, because we need the cyclic distance, not absolute difference. But since we are symmetric, we can compute d = min( (a - b) mod n, (b - a) mod n )? Actually the minimal cyclic distance is symmetric. So we can compute d once and then add 2 to freq[d] (since both (a,b) and (b,a) contribute d). That cuts loops by factor ~2. However, careful: what about the case where the two directed distances are different? They are always the same because distance is the minimal cyclic distance, which is symmetric. So yes, we can just double. So we can do:
for i in range(m):
a = res[i]
for j in range(i+1, m):
b = res[j]
d = (a - b) % n
if d > half:
d = n - d
freq[d] += 2
But we must ensure that for unordered pairs, the directed distances are indeed identical. Since distance is defined as min(d_mod, n-d_mod), which is symmetric. So (a,b) gives same as (b,a). So we can safely add 2.
Edge case: If d == half? That's symmetric. So fine.
Thus the number of iterations becomes C(m,2) = m*(m-1)/2, which for m=5000 is about 12.5 million, half of 25 million. That's better.
We could further speed up by using arrays from array module or numpy, but not necessary.
Now, we need to sort? Not necessary for distance computation; but if we want to exploit properties? Not needed.
Thus algorithm:
for each n:
if n == 0: break
half = (n-1)//2
residues = set()
for i in range(1, n):
residues.add((ii) % n)
res = list(residues) # maybe convert to list for indexing
m = len(res)
freq = [0](half+1) # index from 0 to half, we use 1..half
for i in range(m):
a = res[i]
for j in range(i+1, m):
b = res[j]
d = a - b
if d < 0:
d = -d? Actually careful: we need cyclic distance, not absolute difference. The expression d = (a - b) % n yields the forward distance from b to a? Let's define properly.
We need to compute the minimal cyclic distance between a and b. That's min( (a - b) mod n, (b - a) mod n ). Since (b - a) mod n = n - ((a - b) mod n) (if not zero). So we can compute:
diff = (a - b) % n
if diff > half:
diff = n - diff
That yields the minimal distance.
But using mod is fine. However, we could also compute:
diff = a - b
if diff < 0:
diff = -diff
if diff > half:
diff = n - diff
But is that correct? Not always. Because the cyclic distance is not the absolute difference unless that is ≤ half. Actually the minimal cyclic distance is min(diff_abs, n - diff_abs) where diff_abs = absolute difference |a-b|. Since a,b in [0,n-1], the absolute difference is one of the two distances along the circle (the shorter arc). Indeed, the distance along the circle can be either |a-b| or n - |a-b|, whichever is smaller. Since n is odd, the smaller is always ≤ half. So we can compute diff_abs = abs(a-b). Then if diff_abs > half: diff = n - diff_abs else diff = diff_abs. That is simpler and likely faster than mod. Let's test: For a=1,b=9 in n=11, abs(1-9)=8 >5, so n - 8 =3, correct. For a=9,b=1, abs=8 >5 =>3. For a=4,b=6 in n=15, abs=2 ≤7 =>2 correct. For a=6,b=4, abs=2 =>2. For a=0,b=4 in n=9, abs=4 <=4? half=4, so diff=4, correct. For a=4,b=0, abs=4 =>4, correct. For a=0,b=7 in n=9, abs=7 >4 =>9-7=2, correct. So indeed we can just use absolute difference and compare with half.
Thus we can compute:
abs_diff = abs(a - b)
if abs_diff > half:
d = n - abs_diff
else:
d = abs_diff
This avoids modulus and handles both directions symmetrically. Since we are iterating only unordered pairs, this is fine.
Thus the inner loop becomes:
d = abs(a - b)
if d > half: d = n - d
freq[d] += 2
But careful: Is there any case where abs(a-b) = 0? No because a≠b.
Now, is it always that the minimal cyclic distance equals min(abs_diff, n - abs_diff)? Yes because the circle distance between two points is the length of the shorter arc, which is the minimum of the clockwise and counterclockwise distances. Since numbers are placed in order around the circle, the clockwise distance from a to b is either (b - a) mod n, and counterclockwise is (a - b) mod n. The minimal is min( (a-b) mod n, (b-a) mod n ). But (a-b) mod n = (a - b + n) % n. This is not necessarily equal to abs_diff? Actually if a ≥ b, then abs_diff = a - b. The clockwise distance from b to a is a - b (if a≥b) which is abs_diff. The counterclockwise distance from b to a is n - (a - b) = n - abs_diff. So minimal is min(abs_diff, n - abs_diff). If a < b, then abs_diff = b - a. The clockwise distance from a to b is b - a = abs_diff, the counterclockwise is n - (b - a) = n - abs_diff. So same. So indeed the minimal cyclic distance is min(abs_diff, n - abs_diff). So we can compute using abs_diff.
Thus we don't need mod.
So algorithm is:
freq = [0]*(half+1)
for i in range(m):
for j in range(i+1, m):
d = abs(res[i] - res[j])
if d > half:
d = n - d
freq[d] += 2
Now, ensure that d is at least 1. Since res[i] != res[j], abs_diff >= 1. If abs_diff == half? That's <= half, so d = half. If abs_diff > half, d = n - abs_diff which will be less than half? Actually if abs_diff > half, then n - abs_diff < half because n = 2half+1, so n - abs_diff = 2half+1 - abs_diff. Since abs_diff > half, we have n - abs_diff < half+1? Let's test: half=5, n=11, abs_diff=8 >5, n - 8 =3 <5. So d is in [1, half]. Good.
Now, note that we are adding 2 for each unordered pair, assuming both ordered pairs contribute the same d. That's correct because the distance is symmetric. So total sum of freq = 2C(m,2) = m(m-1), as required.
Now, we should also consider if m=0? For n=1, m=0. Then no loops, freq all zeros. But we output nothing? Probably n≥3.
Now, we need to output each frequency on its own line. For n=3, half=1, m? Compute residues: 1^2=1 mod3=1, 2^2=4 mod3=1 => S={1}. m=1, freq[1]=0. Output "0". Good.
Now, we should ensure we use fast I/O if many outputs. Use sys.stdin.read to read all input, and sys.stdout.write to output.
Now, we need to consider memory: freq size up to half ≤ 5000, fine.
Now, we should test on given examples.
Let's implement.
But there is one more nuance: The problem's description mentions that for prime n ≡ 3 mod 4, the frequencies are all equal. In that case, m = (n-1)/2, and each freq[d] = ? For n=11, m=5, total ordered pairs = 20, half=5, each freq=4. So each = (m*(m-1)) / half? Actually 20/5=4. So for n=11, yes.
Now, for n=15, we got non-uniform.
Now, we have to ensure we don't include 0 in the set if 0 is not in the squares. In composite n where some squares are 0, we include 0. That's fine.
Now, about the phrase: "The results of calculating the remainder of the square of a natural number (1, 2, ..., n -1) of 1 or more and less than n divided by n are the same, so they are different from each other." Actually translation might mean: For a prime n ≡ 3 mod 4, the residues are all distinct and their number is (n-1)/2. That's not needed.
Thus solution seems clear.
Now, maybe we can optimize further by noticing that the set of residues is symmetric about n/2? Not necessary.
Now, implementation details:
Pseudo:
import sys
def process(n):
half = (n-1)//2
residues = set()
for i in range(1, n):
residues.add((ii) % n)
res = list(residues)
m = len(res)
freq = [0](half+1)
for i in range(m):
a = res[i]
for j in range(i+1, m):
d = abs(a - res[j])
if d > half:
d = n - d
freq[d] += 2
out_lines = [str(freq[d]) for d in range(1, half+1)]
return "\n".join(out_lines)
def main():
data = sys.stdin.read().strip().split()
out = []
for token in data:
n = int(token)
if n == 0:
break
out.append(process(n))
sys.stdout.write("\n".join(out))
if name == "main":
main()
But careful: For multiple datasets, the output should have each dataset's frequencies in order, and presumably no blank line between. So joining all lines together with newline is fine.
But we need to ensure that if there are multiple datasets, the outputs are concatenated directly. In the example, there is no extra blank line. So join with newline across all lines.
Now, what about the case where half=0? If n=1, we would have no lines, but then joining might produce empty string? That's okay, but maybe we should skip.
Now sum: let's compute freq:
d=1: from (3,4) and (4,5) => each +2 -> total 4? Actually (3,4): +2, (4,5): +2 => 4.
d=2: from (1,3): +2, (3,5): +2 => 4.
d=3: from (1,4): +2, (1,9): +2 => 4.
d=4: from (1,5): +2, (5,9): +2 => 4.
d=5: from (3,9): +2, (4,9): +2 => 4.
So indeed all 4. Good.
Now for n=15, as we computed earlier, unordered pairs count = C(5,2)=10, each contributes +2, total 20.
Our computed frequencies: d=1: from (9,10)? Actually we earlier computed ordered pairs. For unordered, we need to compute d for each unordered pair (a,b). Let's compute from our earlier ordered data, but using unordered:
Set = {1,4,6,9,10}. Unordered pairs:
(1,4): d = min(3,12?) Actually abs=3 => d=3 (since 3 ≤7) -> d=3 -> add 2 to freq[3]
(1,6): abs=5 -> d=5 -> +2 to d5
(1,9): abs=8 >7 -> d=7 -> +2 to d7
(1,10): abs=9 >7 -> d=6 -> +2 to d6? n=15, half=7, 9>7 so d=15-9=6, yes.
(4,6): abs=2 -> d=2 -> +2 to d2
(4,9): abs=5 -> d=5 -> +2 to d5
(4,10): abs=6 -> d=6 -> +2 to d6
(6,9): abs=3 -> d=3 -> +2 to d3
(6,10): abs=4 -> d=4 -> +2 to d4
(9,10): abs=1 -> d=1 -> +2 to d1
Now counts: d1:2, d2:2, d3: from (1,4) and (6,9) => 2+2=4? Actually (1,4) gave d3, (6,9) gave d3 => each +2 => total 4. d4: from (6,10) => +2 => total 2? Wait (6,10) only? That's one unordered pair, so +2 -> 2. d5: from (1,6) and (4,9) => 2+2=4. d6: from (1,10) and (4,10) => 2+2=4. d7: from (1,9) => +2 -> 2. So frequencies: 2,2,4,2,4,4,2. That's what we got earlier. So algorithm matches.
Thus implementation is correct.
Now, we need to watch out: For large n, constructing set of residues using loop from 1 to n-1 is O(n). n up to 10000, so 10000 steps, trivial.
Thus overall complexity O(n + m^2). Worst-case m ~ 5000, so m^2 ~ 25 million, fine.
Now, we should consider if there is any possibility that m can be larger than half+1? Actually the maximum number of distinct residues from squares of 1..n-1 cannot exceed n (obviously) but since we exclude 0? Actually we include 0, so at most n distinct residues (0..n-1). But can we have all n residues? For n prime, only (n-1)/2 + 1? Actually 0 plus (p-1)/2 gives (p+1)/2. For composite, maybe more. For n=35=15, max squares? We have 5 distinct non-zero squares, plus 0 if present. Could we have more? The number of squares modulo n (including 0) is the product over primes p^e? Actually for modulus n, the number of quadratic residues mod n is something like: For n = ∏ p_i^{e_i}, the number of squares is ∏ f(p_i^{e_i}), where f(p^e) = (p^{e-1} (p+1) ) / 2 for odd p, plus something for powers of 2. For odd n, we can ignore 2. So for p^e, the number of squares including 0 is (p^{e-1}(p+1))/2. For p=3, e=1: (3^{0}(3+1))/2 = 4/2=2? That's including 0? Actually that yields 2? But for p=3, squares mod3 are 0,1 only. That's 2, correct. For p=3, e=2: p^2=9, (3^{1}(3+1))/2 = 34/2=6. So there are 6 squares modulo 9 including 0? But we earlier found only 4 (0,1,4,7). That seems less. Did we miss some? Let's check squares modulo 9: i from 0..8: 0^2=0,1^2=1,2^2=4,3^2=0,4^2=7,5^2=7,6^2=0,7^2=4,8^2=1. So set = {0,1,4,7} -> 4. So theory gave 6, but that's for all residues modulo 9, not only from numbers 0..8? Actually the number of quadratic residues modulo 9 (including 0) is 6? Let's check: According to known result: The number of squares modulo p^k for odd prime p is (p^{k-1}(p+1))/2. For p=3, k=2: (3^{1}(3+1))/2 = 12/2=6. So there should be 6 residues. Which ones? Possibly: 0,1,4,7,? also? Maybe 9? Actually mod9, numbers 0..8. We have 0,1,4,7. That's 4. So something's off. Wait the formula might count numbers that are squares mod 9, but not all numbers in 0..8 may be squares? Indeed, only 0,1,4,7 are squares. That's 4. So why does formula give 6? Perhaps I misremember: For p odd prime, the number of quadratic residues modulo p^k (including 0) is (p^{k} + p^{k-1} + 2)/4? Not sure. Let's derive: In the group of units modulo p^k, the squares are a subgroup of index 2? Actually the group of units modulo p^k for odd p is cyclic of order φ(p^k)=p^{k-1}(p-1). The squares form the unique subgroup of index 2 if -1 is a square? Wait: In a cyclic group, the squares are the subgroup of order φ/2 if φ is even. Since φ(p^k)=p^{k-1}(p-1) which is even for odd p>2. So squares among units have size φ/2 = p^{k-1}(p-1)/2. Including 0, total squares = 1 + φ/2 = 1 + p^{k-1}(p-1)/2. For p=3, k=2: φ=3^1(3-1)=32=6; φ/2=3; plus 0 gives 4. So that's 4. So formula: number of squares mod p^k = 1 + p^{k-1}(p-1)/2. For p=3,k=2 => 1+32/2=1+3=4. Good. So our set size m is at most 1 + φ(n)/2? For composite n, it's product over primes? Actually for n odd, Chinese remainder theorem: squares mod n correspond to tuples of squares mod each prime power. So number of squares (including 0) = ∏ (1 + p_i^{e_i-1}(p_i-1)/2). For n=15=35: product = (1 + (3-1)/2) * (1 + (5-1)/2) = (1+1) * (1+2) = 23=6. Including 0? This counts including 0? The formula 1 + p^{e-1}(p-1)/2 includes 0. So for 3: 1+1=2, for 5: 1+2=3, product=6. So there are 6 squares modulo 15, but we only got 5. Because the set of squares from numbers 0..14 includes all squares modulo 15? Actually we need to check: Are there numbers that are squares modulo 15 that are not represented by a square of an integer 0..14? Possibly some residues are squares but the square root may be >14? But modulo 15, any square residue has a representative in 0..14 that is a square of some integer modulo 15. Because if x^2 ≡ a (mod 15), then we can take x mod 15, which is between 0 and 14, and its square gives a. So the set of squares modulo 15 is exactly the set of residues a for which there exists an integer x such that x^2 ≡ a mod 15. That set includes all squares of x in 0..14. So there should be 6 distinct residues. Which ones are missing? Let's compute squares for x=0..14:
Set: {0,1,4,6,9,10}. That's 6! I earlier missed 0 and 6? Actually earlier I computed set S = {1,4,6,9,10} and omitted 0 because we only considered i from 1 to 14? But we included 0? For n=15, i from 1 to 14 (n-1). That's i=1..14, not including 0. So 0 is not included because 0^2 mod15 =0 but i=0 is not in the range (1..n-1). The problem specifies natural numbers 1,2,..., n-1. So 0 is not included. So we do NOT include 0 unless some i in 1..n-1 yields 0. For n=15, no i gives 0, so 0 not in set. So for n=15, our set S has size 5, not 6. So our set is not the full set of quadratic residues mod n; it's the set of squares of numbers 1..n-1. That excludes 0 if 0 is not a square of a number in that range. But if n is composite and there exists i such that i^2 ≡ 0 mod n, i.e., n divides i^2, then 0 is included. So the set S is the set of non-zero squares that appear as squares of numbers 1..n-1, plus possibly 0 if it appears. That's exactly the set of quadratic residues modulo n that have a square root in the range 1..n-1. For prime n, all non-zero residues have two square roots, so they appear, so S = set of all non-zero residues (size (n-1)/2). For composite, some residues may not be represented because the square root might be a number that shares a factor with n? Actually any quadratic residue a modulo n (non-zero) has some integer x such that x^2 ≡ a mod n. However, x might be 0 mod n? No, a is non-zero so x not multiple of n. But x may not be in 1..n-1? But we can reduce x modulo n to an integer in 0..n-1. If x mod n = 0, then a ≡ 0, contradiction. So x mod n is in 1..n-1. So there is always a representative in 1..n-1. So actually every non-zero quadratic residue modulo n appears as square of some number in 1..n-1. Wait, is that always true? For n=15, the residue 6 appears: we found i=6 gives 6. Residue 10 appears: i=5 gives 10. Residue 1 appears, 4 appears, 9 appears. So all non-zero residues appear. But earlier I thought there were 6 residues including 0, and we got 5 non-zero residues. That matches: the set of non-zero squares mod15 is size 5. Because total squares = 6 including 0, so non-zero =5. So our set S (non-zero squares) includes all non-zero quadratic residues. For prime, non-zero quadratic residues count = (p-1)/2. So indeed S is the set of all non-zero quadratic residues modulo n. That is consistent: for any odd n, the map i -> i^2 mod n for i=1..n-1 yields all quadratic residues modulo n that are non-zero (i.e., all numbers a such that there exists an integer coprime to n? Actually not necessarily coprime: For composite n, a quadratic residue could be not coprime to n, e.g., for n=9, a=0 is zero, but non-zero a=3? Is 3 a quadratic residue mod9? Is there x such that x^2 ≡ 3 mod9? x^2 mod9 can be 0,1,4,7. So 3 is not a quadratic residue. So the set of quadratic residues modulo n is exactly the set of squares of numbers modulo n (including 0). The non-zero ones are exactly those squares of numbers in 1..n-1 that are not 0. And indeed every non-zero quadratic residue has a square root in 1..n-1. So our set S is the set of non-zero quadratic residues. That matches: For n=9, non-zero quadratic residues are {1,4,7}? Actually squares mod9: 1^2=1, 2^2=4, 3^2=0, 4^2=7, 5^2=7, 6^2=0, 7^2=4, 8^2=1. So non-zero distinct residues: {1,4,7}. That's 3. (n-1)/2 =4. So m = 3, not 4. So m can be less than (n-1)/2. In our earlier calculation for n=9, we included 0 erroneously? We computed S = {0,1,4,7} from i=1..8? But from i=1..8, we got residues: 1,4,0,7,7,0,4,1 => set = {0,1,4,7}. So 0 is included because i=3 gave 0, i=6 gave 0. So 0 appears. So for n=9, 0 is a square of i=3 and 6, which are in 1..8. So S includes 0. So S = {0,1,4,7} includes 0. That is the full set of quadratic residues modulo 9 (including 0). That size is 4. So for n=9, S includes 0. So the set S is the set of all quadratic residues modulo n (including 0 if it appears). But if 0 does not appear (i.e., no i in 1..n-1 squares to 0 mod n), then S excludes 0. When does 0 appear? If n has a divisor d such that there exists i with i^2 ≡ 0 mod n, i.e., n | i^2. Since i < n, i cannot be a multiple of n, but could be a multiple of a square factor? For n to divide i^2, n must have all prime factors with exponents, but i could be a multiple of the product of each prime to ceil(e/2). For example, n=9=3^2, i must be multiple of 3 to have i^2 divisible by 9. i=3 gives 9, i=6 gives 36 divisible by 9? 36/9=4, yes. So 0 appears. For n=15=3*5, to have i^2 divisible by 15, i must be divisible by 15, impossible for i<15, so 0 does not appear. So S includes 0 exactly when n is not squarefree? Actually if n has at least one prime factor with exponent ≥2, then there exists i that is a multiple of that prime to at least ceil(e/2). For n= p^2, i=p works. For n= p^3, i = p^2 works? Because (p^2)^2 = p^4 divisible by p^3. So yes. So S may contain 0 for many composite n.
Thus the problem's description: "The results of calculating the remainder of the square of a natural number (1, 2, ..., n -1) of 1 or more and less than n divided by n are the same, so they are different from each other." That's only true for primes (no 0 and all non-zero residues are distinct and appear exactly twice except? Actually for prime, each non-zero residue appears exactly twice? In the list, they appear twice each, but set is distinct. So they say "are the same, so they are different from each other." That's ambiguous but we can ignore.
So we should compute S exactly as described: all residues from squares of i=1..n-1, take distinct set. That includes 0 if it occurs.
Now, confirm for n=9, the property about uniform frequencies? Probably not, we computed frequencies: 2,2,6,2. Not uniform.
Thus the algorithm is correct.
Now, one more nuance: The problem says "The number is (n -1) / 2." This refers to the size of the set for primes ≡3 mod4. But for composite, we ignore that.
Now, final step: We'll implement and test with some cases.
Let's also test n=3: i=1,2. squares: 1,1 mod3? Actually 1^2=1,2^2=4 mod3=1. Set S = {1}. m=1, half=1, freq[1]=0. Output "0". That seems correct but the problem might not test n=3.
n=5: half=2, S from i=1..4: squares: 1,4,4,1 => set = {1,4}. m=2, freq: compute unordered pair: (1,4): d=abs(1-4)=3 >2? Actually half=2, n=5, abs=3 >2 so d=5-3=2. freq[2]+=2. So freq[1]=0, freq[2]=2. Output "0\n2". Good.
n=13 (prime ≡1 mod4): half=6. Squares mod13: non-zero residues? For prime 13, squares: 1,4,9,3,12,10,10,12,3,9,4,1 => set = {1,3,4,9,10,12}? That's 6 = (13-1)/2. m=6. Let's compute frequencies manually maybe to see non-uniform. We'll test later.
Now, we should consider the case where m=0 (possible only if n=1? Or n=2? but n is odd, so n=1 yields i from 1 to 0? Actually for n=1, there are no numbers 1..0, so S empty. So m=0. Then half = (1-1)//2 =0. So we would output nothing (zero lines). That's okay but maybe not needed.
Now, complexity wise, for n=9973 (prime), m = 4986, C(m,2) ≈ 12.43 million. Loop 12.4 million iterations, each with abs, comparison, maybe addition. That's fine.
But we can further micro-opt: store residues in a list and access via index. Use local variables for n, half, freq, res. Use for i in range(m): a = res[i]; for j in range(i+1,m): b = res[j]; d = a - b; if d < 0: d = -d; if d > half: d = n - d; freq[d] += 2. That uses subtraction and then absolute by negation if negative, which may be faster than abs? In Python, abs() is a built-in function and may be slightly slower than manual branch? But we can test. But we can do:
diff = a - b
if diff < 0: diff = -diff
if diff > half: diff = n - diff
freq[diff] += 2
This avoids function call. It's likely fine.
Now, we must be careful: The distance d must be at least 1. Since a!=b, diff != 0. So fine.
Now, one more thing: Because we are using set, the order of elements in res is arbitrary but fine.
Now, we need to output for each dataset the frequencies for d=1..half each on a separate line. That can be a lot of lines if half is large (up to 5000). For each dataset, output up to 5000 lines. That's fine.
Now, potential issues: The problem might expect to output the frequencies for each dataset, maybe with each dataset's output separated by a blank line? But example shows no blank line. So we follow that.
Now, edge cases: n is odd and possibly 1? If n=1, half=0, we output nothing. But would that be considered a dataset? Possibly not.
Now, one more nuance: They say "a program that takes an odd number n of 10000 or less as an input". So n is odd. Good.
Now, I'll implement as described.
But before finalizing, think about whether there is any property that can simplify for large n to avoid O(m^2). For prime n ≡ 3 mod4, there is uniform distribution, but we still need to compute frequencies for any n. Since we don't know n's nature, we must compute directly. But maybe the problem only expects to test primes? However, they gave n=15 which is composite, so they want composite as well. So direct enumeration is required.
Now, could there be a more efficient way using the fact that residues are quadratic residues and maybe we can compute distances by sorting residues and using two pointers? Let's think: The set S is a subset of [0, n-1]. We need to count, for each d, number of ordered pairs (a,b) with a,b∈S, a≠b, such that min(|a-b|, n-|a-b|) = d. This is similar to counting distances on a circle. If we sort the residues, we can compute for each a, the other points at distances. But still O(m^2) in worst-case if we do for each a, scan others? However, we could use a boolean array of size n marking which residues are present, and then for each possible a, we could consider distances? But we still need to count pairs, which could be done by convolution: Let's have an indicator array f of length n, where f[x] = 1 if x∈S else 0. Then we want for each d, count of ordered pairs (a,b) with a,b∈S, a≠b, such that either a-b ≡ d (mod n) or a-b ≡ -d (mod n), but we want to count each ordered pair exactly once with the minimal distance d. However, if we consider the directed differences mod n, each unordered pair contributes two directed differences that are complements (unless d=0). The minimal distance d appears twice for each unordered pair? Actually each unordered pair {a,b} corresponds to two directed differences: one with value t (mod n) and the other with n-t. The minimal distance d = min(t, n-t). So if we count directed differences, we could count both t and n-t. But we want to count the minimal d for each ordered pair. That's exactly half of the sum of counts for t and n-t? Not exactly because for each unordered pair, the two directed differences are t and n-t, and both have minimal distance d = min(t,n-t). So if we simply count directed differences where the directed difference (not necessarily minimal) equals some value s, then each unordered pair contributes two counts: one to s=t and one to s=n-t. The minimal distance for that unordered pair is d, and both s=t and s=n-t are ≥ d, with one being d and the other being n-d (which > d unless d = n/2 but n odd). So the minimal distance d occurs exactly once per ordered pair? Wait, for each unordered pair, we have two ordered pairs: (a,b) and (b,a). The distance for (a,b) is min(t, n-t) = d. The distance for (b,a) is also min(t, n-t) = d. So both ordered pairs have distance d. So each unordered pair contributes 2 to freq[d]. So that's consistent.
If we instead count directed differences without taking min, each unordered pair contributes one to count[t] and one to count[n-t] (where t = (a-b) mod n, assuming we take positive mod). So the frequency of directed difference t is something. Then the minimal distance frequency can be derived: for each t=1..n-1, let d = min(t, n-t). Then the total count for d from ordered pairs is count[t] + count[n-t]? But careful: For a given unordered pair, if t is the directed difference from a to b, then the directed difference from b to a is n-t. So the unordered pair contributes exactly one to count[t] and one to count[n-t]. So the sum count[t] + count[n-t] counts each unordered pair twice? Actually count[t] and count[n-t] each count ordered pairs. For a fixed unordered pair, it contributes 1 to count[t] and 1 to count[n-t]. So if we sum count[t] + count[n-t], we get 2 for that unordered pair. But the minimal distance d = min(t, n-t). So if we define for each d, we want the sum over all ordered pairs where the minimal distance is d. For a given unordered pair with t and n-t, the minimal distance d = min(t, n-t). The two ordered pairs both have distance d. So the contribution to freq[d] is 2. Meanwhile, count[t] + count[n-t] = 2. So freq[d] = count[t] + count[n-t]? But that would assign the same unordered pair's contribution to both t and n-t summed, which equals 2, which equals freq[d]. However, note that for different unordered pairs, t may be different but d might be the same. So if we aggregate over all unordered pairs, we could compute freq[d] = sum over all t such that min(t,n-t)=d of count[t]. But count[t] includes all ordered pairs with directed difference t. Since each unordered pair contributes to exactly one t (and its complement) with t possibly equal to d or n-d. For a given unordered pair, if t = d (i.e., the directed difference equals the minimal distance), then the other directed difference is n-d (which > d). So this unordered pair contributes to count[d] and count[n-d]. The minimal distance for that unordered pair is d, and we want to add 2 to freq[d]. If we take count[d] + count[n-d], that includes both contributions from that unordered pair, which sum to 2. But count[d] also includes contributions from other unordered pairs where the directed difference is d (but those could correspond to unordered pairs where the minimal distance is d as well? Actually if directed difference is d, then the minimal distance is min(d, n-d). Since d <= half, min(d, n-d)=d. So any ordered pair with directed difference d has minimal distance d. Similarly, any ordered pair with directed difference n-d (which > half) has minimal distance d as well. So count[d] counts ordered pairs with minimal distance d, and count[n-d] also counts ordered pairs with minimal distance d (since n-d < half? Actually n-d is less than half because n-d = (2half+1)-d = 2half+1-d. If d ≤ half, then n-d = half + (half+1-d) which is > half? Let's check: half = (n-1)/2. n-d = n - d = (2half+1) - d. If d ≤ half, then n-d ≥ half+1, so > half. So ordered pairs with directed difference n-d have minimal distance d. So both count[d] and count[n-d] correspond to ordered pairs with minimal distance d. So freq[d] = count[d] + count[n-d] because every ordered pair with minimal distance d contributes to either count[d] (if its directed difference is d) or count[n-d] (if its directed difference is n-d). And there is no overlap because an ordered pair has a unique directed difference. So indeed we can compute the directed difference counts first, then combine.
Thus we could compute count[t] for t=1..n-1 (directed differences) from the set S. Then for each d=1..half, freq[d] = count[d] + count[n-d].
Now, how to compute count[t] efficiently? count[t] = number of ordered pairs (a,b) in S×S, a≠b, such that (a - b) mod n = t. This is like convolution of the indicator function with its reversed version. Specifically, if we define an array f of length n with f[x]=1 if x∈S else 0. Then the number of ordered pairs (a,b) with a,b ∈ S and a-b ≡ t (mod n) is sum_{x} f[x] * f[(x - t) mod n]. But this includes pairs where a=b if t=0. For t≠0, it automatically excludes a=b? Actually if a=b then (a-b) mod n =0, so for t≠0, the sum over x f[x]*f[x-t] (mod n) will not count a=b because that would require t=0. So it counts ordered pairs (a,b) with a∈S, b∈S, and b ≡ a - t mod n. That includes all ordered pairs with that directed difference. So count[t] = sum_{x} f[x] * f[(x - t) mod n].
Thus we can compute count[t] for all t via convolution using FFT? But n ≤ 10000, we can compute O(n^2) by iterating over a∈S and for each t compute? Actually we can compute count[t] by: for each a in S, for each b in S, compute t = (a-b) mod n, and increment count[t]. That's O(m^2) already, same as before. But we could compute via convolution using FFT to get O(n log n). Since n is only up to 10000, O(n^2) might be okay if m ~ n/2, but O(n^2) = 100 million, which is heavier than O(m^2)=25 million. Actually O(n^2) would be 100 million, which is larger. But we could compute count[t] by iterating over a in S and for each b in S, compute t and increment count[t]. That's exactly the same O(m^2) we already have; but if we then compute freq[d] = count[d] + count[n-d], we still have O(m^2). However, we could compute count[t] using the fact that S is a subset, but no better than O(m^2) in worst case.
But maybe we can compute count[t] via convolution using FFT with size next power of two > 2n. Since n ≤ 10000, the FFT size ~ 16384, which is small. We can compute convolution of f with reversed f? Actually we want cyclic convolution modulo n. But we can compute linear convolution and then fold mod n? Specifically, define array f of length n with f[i]=1 if i∈S else 0. Then the cyclic autocorrelation: count[t] = sum_{i} f[i] * f[(i - t) mod n]. That's a cyclic convolution. We can compute using FFT by creating arrays of length 2n, compute convolution, then for t=0..n-1, count[t] = conv[t] + conv[t+n]? Actually standard technique: For cyclic convolution of length n, we can compute linear convolution of f and g where g[i] = f[(-i) mod n]. But we can do: duplicate f to length 2n, compute convolution of f and reversed f (or f and f shifted). Let's derive: We want C[t] = ∑{i=0}^{n-1} f[i] * f[(i - t) mod n]. Define g[i] = f[(-i) mod n] = f[(n-i) mod n]. Then C[t] = ∑{i} f[i] * g[t-i] (with indices mod n). That's a cyclic convolution. To compute cyclic convolution via linear convolution, we can extend f and g to length 2n by zero-padding, compute linear convolution (length 2n-1), then C[t] = linear_conv[t] + linear_conv[t+n] for t=0..n-1. Since n is small, we could implement this with FFT or simply with O(n^2) but using Python's numpy? Not allowed typically. But we could implement O(n^2) by iterating over i and t? That would be O(n * size of S) maybe? Actually we could compute count[t] by: for each a in S, for each shift t, we check if (a - t) mod n is in S. That's still O(m * n) which could be up to 5k * 10k = 50 million, similar to O(m^2) but maybe larger. O(m^2) is better if m is about half of n.
So the O(m^2) approach with unordered pairs is fine.
Thus final solution: O(n + m^2) per dataset.
Now, let's implement.
But before final code, I'd like to consider potential speed improvements: Use Python's list and for loops, but we can use PyPy's JIT. Usually online judges accept Python, and they may have time limit such that O(12 million) is okay. Let's assume.
We can also micro-opt by using array of ints for res, and use local variables.
Implementation:
def solve_one(n):
half = (n-1)//2
seen = [False]n
for i in range(1, n):
seen[(ii) % n] = True
# Build list of residues
res = [i for i, v in enumerate(seen) if v]
m = len(res)
freq = [0]*(half+1)
# Use local variables for speed
n_local = n
half_local = half
freq_local = freq
for i in range(m):
a = res[i]
for j in range(i+1, m):
d = a - res[j]
if d < 0:
d = -d
if d > half_local:
d = n_local - d
freq_local[d] += 2
# Output
return "\n".join(str(freq_local[d]) for d in range(1, half+1))
But building list via enumerate(seen) is fine.
Alternatively, we could generate residues set using set and then list, but the boolean array method might be faster? Let's compare: Using set will involve hashing and may be faster for building distinct list? Actually n is only 10000, so either is fine. But using boolean array ensures O(n) time to mark, then O(n) to collect, total O(n). Set operations also O(n) but with constant factor maybe higher. I'll use boolean array.
But careful: The squares ii may be large; but i up to 10000, ii up to 100 million, fits in int. Compute (ii) % n: we can compute (i%n)(i%n) % n, but Python's big ints are fine. But to be efficient, we can compute (i*i) % n using multiplication and modulo; that's okay.
Alternatively, we can compute residues by incremental method: (i+1)^2 mod n = (i^2 + 2i + 1) mod n, but not necessary.
Now, note: For n=10000, we need to iterate i=1..9999, that's 9999 iterations, trivial.
Now, we need to read input until 0.
Now, we should output each dataset's frequencies. But note: If we output each dataset's frequencies as a block, we need to separate them? In the example, there is no blank line. So we just join all lines together with newline. But we need to ensure that if we have multiple datasets, the last line of previous dataset and first line of next are directly consecutive with newline, no extra blank line. That's fine.
Now, a subtlety: The problem statement says: "output the frequency of occurrence in the following format.
Number of occurrences (integer) of (a, b) where the difference between the squares of the remainder is 1
The number of occurrences (integer) of (a, b) where the difference between the squares of the remainder is 2
::
::
The number of occurrences (integer) of (a, b) where the difference between the squares of the remainder is (n-1) / 2"
Thus each frequency on its own line.
Now, we might also consider that for n=1, there is no output? But not needed.
Now, final code:
import sys
def main():
data = sys.stdin.read().strip().split()
out_lines = []
for token in data:
n = int(token)
if n == 0:
break
half = (n - 1) // 2
# boolean array for residues
present = [False] * n
for i in range(1, n):
present[(i * i) % n] = True
# collect residues into list
residues = [i for i in range(n) if present[i]]
m = len(residues)
freq = [0] * (half + 1)
# compute unordered pairs
for i in range(m):
a = residues[i]
for j in range(i + 1, m):
d = a - residues[j]
if d < 0:
d = -d
if d > half:
d = n - d
freq[d] += 2
# append frequencies for d=1..half
for d in range(1, half + 1):
out_lines.append(str(freq[d]))
sys.stdout.write("\n".join(out_lines))
Now, we should consider the case where half=0 (n=1). If we process n=1, half=0, then residues: from i=1..0 no loop, so present all false, residues = [] empty. Then freq size 1? Actually freq = [0] * (0+1) = [0]. Then loop for d in range(1, half+1) -> range(1,1) empty, so no output. So nothing printed for that dataset. That's okay. But the problem likely doesn't include n=1.
Now, potential micro-optimizations: We might want to avoid constructing present array for each dataset? It's fine.
Now, note: The problem statement says "odd number n of 10000 or less". So n is odd. We can rely on that.
Now, what about n=0 to terminate. Good.
Now, I think solution is ready.
Now, but we need to provide the final answer: The program as requested. We'll output code accordingly.
But before final, we might test with a larger prime to ensure performance.
I could also test with n=1999 (prime, 1999 mod4? 1999%4=3? 1999=4*499+3, so ≡3 mod4. Should have uniform frequencies. m=999. C(m,2) ≈ 498,501. Loop 0.5 million, trivial.
For n=9973, m=4986, C(m,2) ≈ 12.43 million, loop will be okay.
Now, we must consider Python's recursion? Not used.
Now, maybe we need to worry about memory for out_lines if many datasets and many lines (like if many datasets each with half~5000, total lines could be huge, but still maybe manageable. But we can output incrementally instead of storing all lines. However, storing all lines as strings then joining is also fine. Suppose 10 datasets each with 5000 lines, that's 50000 lines, each line short, memory trivial.
But we can also output as we go, but need to avoid extra newline at end? Not critical. We'll stick to collecting.
Now, we should also note: The input may contain whitespace and line breaks. Using sys.stdin.read().split() handles.
Now, final answer: Provide the Python code.
But is there any trick? The problem statement might be from a Japanese contest where they want to verify property, so output frequencies for each distance. Our solution does that.
Now, we can finalize.
But let's double-check the problem statement: "The number is (n -1) / 2." Actually they said "The number is (n -1) / 2." That might refer to the count of numbers in the set for primes. Then later: "The 'difference' in the numbers thus obtained is either 1, 2, ..., (n -1) / 2, and appears the same number of times." That's the property for primes ≡3 mod4. So the frequencies are equal for those primes. So for prime 11, we get 4 each. For prime 19, what would be frequency? m=9, ordered pairs=9*8=72, half=9, each frequency = 72/9 = 8. So each would be 8.
Now, for composite like 15, we got non-uniform.
Thus code works.
Now, I'll produce final answer with code and explanation. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
out_lines = []
code
for token in data:
n = int(token)
if n == 0:
break
half = (n - 1) // 2
# ---- step 1: collect distinct residues of i^2 (mod n) for i = 1 .. n-1 ----
present = [False] * n
for i in range(1, n):
present[(i * i) % n] = True
residues = [x for x in range(n) if present[x]]
m = len(residues)
# ---- step 2: count frequencies of the minimal cyclic distances ----
freq = [0] * (half + 1) # freq[d] for d = 1 .. half
for i in range(m):
a = residues[i]
for j in range(i + 1, m):
b = residues[j]
d = a - b
if d < 0:
d = -d # |a - b|
if d > half:
d = n - d # minimal distance on the circle
freq[d] += 2 # both ordered pairs (a,b) and (b,a)
# ---- output frequencies for distances 1 .. half ----
for d in range(1, half + 1):
out_lines.append(str(freq[d]))
sys.stdout.write("\n".join(out_lines))