We need to find the set of possible fun values for given n. The process: people numbered 1..n in a circle. Starting from person 1, we have a fixed step size k (positive integer ≤ n). We move around the circle, each time passing the ball to the current person's k-th neighbor in the direction of increasing ids. That means if we are at person i, the next person is i + k (mod n), but since ids are 1..n, we need to handle wrap-around. Typically we consider arithmetic modulo n, with residues 1..n. Starting at 1, the sequence is: 1, 1+k mod n (if 0 -> n), then plus k again, etc. So essentially we are walking on the additive group Z_n (with elements 1..n representing residues mod n; but careful: mapping: integer x mod n with 0 representing n? Let's define positions as residues mod n: 0 corresponds to n, and 1 corresponds to 1. Starting at 1 (mod n representation? Actually it's easier: use 0-index: let positions be 0..n-1, where 0 corresponds to person 1, 1 to person 2, ..., n-1 to person n. Then moving by +k (mod n) each step. Starting at 0. Then we iterate until we return to 0. The set of visited positions is the orbit of 0 under addition of k modulo n. That's the subgroup generated by k modulo n, i.e., the multiples of d = gcd(k, n) (since the additive group is cyclic). Actually the orbit of 0 is {0, k mod n, 2k mod n, ...} which is the cyclic subgroup of Z_n generated by k. The size of the orbit is n / d where d = gcd(k, n). Since we stop when we return to 0 (and not counting the second arrival of 0 as a touch? In example: n=6, k=4, d = gcd(4,6)=2, orbit size = 3. They list the order as [1,5,3,1] => visited persons: 1,5,3, then back to 1 (they count touching only once? In fun sum they summed 1+5+3 = 9, i.e., they did not count the final 1 again because it's already touched? Or they count the initial 1 and the subsequent touches excluding final return that is not a new touch? The statement: "Consider the set of all people that touched the ball. The fun value of the game is the sum of the ids of people that touched it." So it's the sum of distinct people who ever held the ball. In the example, they touched 1,5,3 (the final return to 1 doesn't add a new person). So the set is the distinct visited positions.
Thus the set of distinct positions visited is the set {0, k, 2k, ..., (L-1)k} mod n where L = n/d (the order). Those are exactly the multiples of d modulo n? Actually the set is {0, k, 2k, ..., (n/d -1)k} = {0, d(k/d), ...} but since k = d * k', with gcd(k', n/d)=1, the set is the subgroup d * Z_n? Wait: In Z_n, the subgroup generated by k is exactly the set of multiples of d, where d = gcd(k,n). Because the subgroup consists of all multiples of k, but multiples of k give residues that are multiples of d, and indeed the subgroup has size n/d, which equals number of multiples of d. And indeed the set {0, d, 2d, ..., n-d} is the unique subgroup of order n/d. But is that equal to the set generated by k? Yes, because d divides k, so multiples of d are in the subgroup generated by k? Actually k = d * k', and since gcd(k', n/d) = 1, k' is invertible mod n/d. The set generated by k is {ak mod n | a integer} = {d * (ak') mod n}. Since k' is invertible mod n/d, as a runs over 0..n/d-1, ak' mod (n/d) runs over all residues mod n/d. Then d times that yields all multiples of d (0, d, 2d, ..., n-d). So indeed the distinct visited positions are exactly the positions with indices that are multiples of d (mod n). Under 0-indexed, these correspond to residues 0, d, 2d, ..., n-d. In 1-indexed, these correspond to ids: 1 (for 0), 1+d, 1+2d, ..., 1 + (n/d -1)*d. But careful: The mapping: 0-index position p corresponds to person id = p+1. So visited ids: 1, 1+d, 1+2d, ..., 1+ (n/d -1)d? That would give ids: 1, 1+d, 1+2d,... up to 1+(n-d). But 1+(n-d) = n+1-d, which is ≤ n. Since n is divisible by d? Actually d divides n, yes. So the set of visited ids is {1 + jd | j=0,1,..., n/d -1}. That is exactly all persons whose id ≡ 1 (mod d) when using mod n? Wait id mod d? For d dividing n, the residues modulo d: ids 1,1+d,... have remainder 1 mod d. But also id = 1 corresponds to remainder 1 mod d. So indeed all ids that are congruent to 1 modulo d, but note that the set of numbers from 1 to n that are ≡ 1 (mod d) has size n/d, and indeed they are exactly those. However, is that always the set? Check example n=6, k=4, d=2 => visited ids: 1, 1+2=3, 1+4=5. That's {1,3,5}. But they gave example [1,5,3,1] -> set {1,5,3} which is indeed {1,3,5}. Good.
For k=2, d=2 as well, same set. For k=3, d=gcd(3,6)=3, visited ids: 1, 1+3=4? Actually 1+3=4, so {1,4}? But they said for k=3 fun value is 5. 1+4=5. That matches. For k=1, d=1 => visited ids: all 1..6, sum=21. For k=5, d=gcd(5,6)=1 also all visited, sum=21. For k=6, d=gcd(6,6)=6? Actually gcd(6,6)=6, d=6, visited ids: only 1 because n/d =1, so set {1} sum=1. So the fun value depends only on d = gcd(k,n). Actually for each d dividing n, there exist k such that gcd(k,n)=d (any k multiple of d and coprime to n/d). So the fun value for a given d is the sum of all ids that are visited when the step size has gcd d with n. And since visited set is exactly the set of persons whose index (1-indexed) is congruent to 1 modulo d? But careful: In 0-indexed, visited set is multiples of d (including 0). In 1-indexed, that translates to ids: 1 + jd, for j=0..(n/d -1). So sum = sum_{j=0}^{n/d -1} (1 + jd) = n/d * 1 + d * ( (n/d -1) * (n/d) / 2 ) = n/d + d * (n/d)(n/d -1)/2 = n/d + (d * (n/d)(n/d -1))/2.
Simplify: Let m = n/d. Then m is integer, number of visited persons. Then sum S(d) = sum_{j=0}^{m-1} (1 + j d) = m + d * m(m-1)/2 = m + (d * m (m-1))/2.
But note that d = n/m, so substitute: d = n/m. Then:
S = m + (n/m) * m (m-1)/2 = m + n (m-1)/2.
So S = m + n*(m-1)/2, where m = n / d, i.e., m divides n (since d divides n). m is a positive divisor of n? Actually d divides n, so m = n/d is also a divisor? Wait if d is a divisor, then m = n/d is integer, and also m divides n (since n = d * m). But m is not necessarily a divisor? Actually if d is any positive divisor of n, then m = n/d is also a positive divisor of n. So m runs over all positive divisors of n as well. So we can express fun value in terms of m = n / gcd(k,n). Since m is the size of the visited set? Wait earlier we said size = n/d = m. Yes, visited distinct count = m. So m is the orbit length (excluding the final return). So m is a divisor of n, and m can be any divisor of n (except possibly m = n? That would correspond to d=1, visited all n persons; m = n, ok). And also m = 1 corresponds to d = n, visited just 1. So the possible fun values are S(m) = m + n*(m-1)/2 for each divisor m of n? But careful: For a given m dividing n, is there always a k such that gcd(k,n) = n/m? Yes, any k such that gcd(k,n)=d with d = n/m, e.g., k = d (if gcd(d,n)=d? gcd(d,n)=d obviously, but need to ensure k ≤ n, and k positive. d = n/m is ≤ n, and if we take k = d, then gcd(d,n)=d, because d divides n. So indeed for each divisor d of n, k = d yields gcd = d, so m = n/d. So every divisor m of n corresponds to a possible fun value S = m + n*(m-1)/2. However, is that the complete set? Could there be different d giving same m? m = n/d, so each d gives a unique m. So fun values correspond one-to-one with divisors m of n. But note that m can also be expressed as the number of distinct people visited. So the possible fun values are exactly those sums.
But check examples: n=6. Divisors of n: 1,2,3,6. For m=1: S = 1 + 6*(0)/2 = 1. For m=2: S = 2 + 6*(1)/2 = 2+3=5. For m=3: S = 3 + 6*(2)/2 = 3+6=9. For m=6: S = 6 + 6*(5)/2 = 6+15=21. That matches output: 1 5 9 21 (sorted: 1,5,9,21). Good.
Second sample: n=16. Divisors: 1,2,4,8,16. Compute: m=1: S=1 + 160/2 =1. m=2: S=2 + 161/2 =2+8=10. m=4: S=4 + 163/2 =4+24=28. m=8: S=8 + 167/2 =8+56=64. m=16: S=16 + 16*15/2 =16+120=136. Output: 1 10 28 64 136. Yes.
So the answer is: for each divisor m of n (or equivalently for each divisor d of n, but it's simpler to use m), compute S(m) = m + n*(m-1)/2, and output in increasing order. But careful: is m always the number of visited distinct people? Yes, orbit length L = n / gcd(k,n) = m. So indeed m runs over all positive divisors of n. So the set of possible fun values is { n/d + n*(n/d -1)/2? Wait using d: S(d) = n/d + d * (n/d)(n/d -1)/2? but simpler: for each divisor m of n, S = m + n(m-1)/2.
But need to confirm that for any k between 1 and n inclusive, m = n / gcd(k,n) is indeed a divisor of n, and all divisors appear? For each divisor m of n, we can choose k = n/m, then gcd(k,n) = n/m (since n/m divides n), so m = n / (n/m) = m. So yes.
Thus answer: compute all divisors of n, for each divisor m compute f = m + n*(m-1)/2, sort them ascending. Since n up to 1e9, number of divisors is at most around 134k (for highly composite numbers, but typically less, and problem says at most 1e5 possible fun values). So we need to list all divisors and compute f.
But is there any nuance? For example, what about k=0? Not allowed (positive integer). So fine.
But wait: The ball passing is described as "the person picks a positive integer k at most n, and passes the ball to his k-th neighbour in the direction of increasing ids". Interpretation: "k-th neighbour" meaning count k persons ahead, including the neighbours? Typically, if you have neighbors, the 1st neighbor is the next person? In the example n=6, k=4: from person 1, his k-th neighbor in direction of increasing ids: neighbours are persons adjacent? But they said "k-th neighbour" might be ambiguous: In the example, from 1, with k=4, they passed to 5. Person 1's neighbours are 2 and 6 (adjacent). The 4th neighbor? Possibly they consider counting starting from the next person: 2 is first neighbor, 3 second, 4 third, 5 fourth? That yields 5. So indeed it's just moving k steps in clockwise direction (increasing ids). So it's equivalent to adding k mod n, with wrap-around (if sum exceeds n, subtract n). So our interpretation is correct.
One more nuance: They said "the person with id 1 initially has a ball. He picks a positive integer k at most n, and passes the ball to his k-th neighbour in the direction of increasing ids, that person passes the ball to his k-th neighbour in the same direction, and so on until the person with the id 1 gets the ball back. When he gets it back, people do not pass the ball any more." So the ball may return to 1 after some steps. We have to consider that the ball may return to 1 before covering all multiples of d? Actually it returns when the accumulated steps sum to a multiple of n. Since each step adds k mod n, the ball returns to 1 when the number of steps L such that L*k ≡ 0 mod n. The smallest positive L is n/gcd(k,n) = m. So after m passes (i.e., m moves), the ball arrives back at 1. The sequence of distinct holders includes the starting 1, then the m-1 intermediate persons, then the return to 1 which is not a new holder. So number of distinct holders = m. So that matches.
Thus the fun values are indeed determined by m.
Now the problem is straightforward: given n (2 ≤ n ≤ 1e9), find all divisors m of n, compute f(m) = m + n*(m-1)/2, and output them sorted.
But wait: Could there be any other fun value not captured by this? Possibly if the ball is passed until person 1 gets it back, but they might count the final return as a touch? But the example shows they did not count the final 1 again. So set is distinct people.
Now is there any possibility that the ball touches 1 again earlier? It returns exactly after L steps, where L = n/gcd(k,n). That's the first time it returns (unless k=0 which not allowed). So distinct set is exactly that.
Thus solution: compute all divisors of n. Since n up to 1e9, its sqrt is about 31623. So we can iterate i from 1 to sqrt(n) and if i divides n, add i and n/i (if distinct). That yields list of divisors. Then for each divisor m, compute f = m + n*(m-1)/2. However careful: m is a divisor, but we could also compute for d as divisor? Both same.
But note: f(m) = m + n*(m-1)/2 = (2m + n(m-1))/2 = (n(m-1) + 2m)/2. This is integer because n(m-1) is even? Not necessarily. For n even or m odd? Let's check parity: n(m-1) + 2m is always even? n(m-1) may be odd times odd etc. But f is sum of integers so it's integer. Actually m + n*(m-1)/2: we need to ensure n*(m-1) is even to be integer division. But since f is sum of integers, it must be integer. So maybe we need to use integer arithmetic with careful division: compute as (2m + n(m-1)) / 2. This numerator is always even? Let's test n=3, m=2 (since n=3 divisors:1,3? Actually n=3, divisors:1,3. m=2 is not a divisor of 3. So m only divisors of n, so m divides n. If n is odd, m can be odd or 1? All divisors of odd n are odd. So n odd, m odd => (m-1) even, so n*(m-1) even. If n is even, m can be even or odd. If m odd, then (m-1) even => n*(m-1) even. If m even, then (m-1) odd, but n is even => product even. So numerator always even. So integer.
We'll compute using 64-bit (since n up to 1e9, m up to 1e9, n*(m-1) up to about 1e9*1e9 = 1e18, which fits in 64-bit signed). So use long long.
Implementation steps:
- Read n.
- Generate all divisors of n. Use for i=1; i*i <= n; ++i. If n % i == 0, add i as divisor, and if i != n/i add n/i.
We'll store in a vector divs.
- For each divisor m in divs, compute f = m + n*(m-1)/2. (Use integer division after verifying evenness but direct multiplication might overflow 32-bit, so use long long.)
Actually compute as: f = m + (n * (m-1)) / 2; Since n*(m-1) even, integer division yields correct.
Or compute f = (2m + n(m-1)) / 2.
- Collect f values into a vector, sort.
- Output.
But check if there is any other nuance: Could k be n? Then step is to the same person? Actually if k=n, then from person 1, his n-th neighbor? In a circle of n, moving n steps brings you back to the same person (since each step moves to next neighbor, after n steps you return). But if you pass to your k-th neighbor with k=n, then you'd pass to the person n steps ahead, which is yourself? Usually, the "k-th neighbour" might be defined as the k-th person away, so if k = n, you would count n persons ahead, which would land on the original person? But the example says for n=6, k=6 yields fun value 1. So they treat k=n as passing to oneself? Actually if you pass to your 6th neighbor, and there are only 6 people including yourself? Wait, if we have persons 1..6 in a circle, and you are person 1, your first neighbor in increasing direction is 2, second neighbor is 3, third neighbor 4, fourth neighbor 5, fifth neighbor 6, sixth neighbor? After 6, the next would be back to 1? Actually if you count neighbors as distinct persons different from yourself, the sixth neighbor would be 1? Let's see: starting from 1, the persons encountered as you move clockwise: 2 (first neighbor), 3 (second), 4 (third), 5 (fourth), 6 (fifth), then back to 1 (sixth). So yes, k=n lands on yourself. So the step size effectively 0 mod n. But we defined k as positive integer ≤ n. If k = n, then adding n modulo n is 0, so the next position is the same. Then the process: person 1 passes to himself? But the problem says passes to his k-th neighbour, and then that person passes... If he passes to himself, does that count as him getting the ball back immediately? The example says k=6 results in fun value 1, meaning only person 1 touched the ball. So they consider that the ball is immediately back to 1? Actually if you pass to yourself, then the ball returns to 1 without anyone else touching. So that corresponds to m=1 (since gcd(n,n)=n, d=n, m=1). That matches our formula. So it's fine.
Now confirm that for any divisor m, we can indeed get the corresponding k? For m=1, k = n works. For m = n, k = 1 works (gcd(1,n)=1). So all are achievable.
Thus answer is simple.
But wait: The problem says: "It can be shown that under the constraints of the problem, the ball always gets back to the 1-st player after finitely many steps, and there are no more than 10^5 possible fun values for given n." That aligns with number of divisors.
So solution: find divisors, compute f, sort, output.
But is there any possibility that two different m produce same f? In examples, they are distinct. Could there be collisions? Let's examine: f(m) = m + n*(m-1)/2. This is linear in m? Actually it's linear: f = (n/2) * m + (1 - n/2). Wait: f = m + n(m-1)/2 = m + (n m)/2 - n/2 = m*(1 + n/2) - n/2. But n/2 might not be integer. But f is strictly increasing with m if coefficient (1 + n/2) >0, which it is. But is it monotonic? derivative positive: 1 + n/2 > 0. So for integer m, as m increases, f increases. Since m are distinct divisors, f are distinct and increasing with m. But note that m are not necessarily sorted? But if m1 < m2 then f(m1) < f(m2). So the f values will be in the same order as the m values. So we could just sort the divisors m ascending and compute f accordingly, they will be ascending. But careful: Since f(m) = m + n*(m-1)/2 = (n/2 + 1)*m - n/2. This is linear with positive slope (n/2+1 > 0 for n≥2). So indeed it's strictly increasing. So we can compute f for each divisor m and sort by m (or by f, same order). So we can sort the divisors m ascending, then compute f in that order and output.
But note: divisors may not be in order; we need output in increasing order of f, which matches increasing order of m. So we can simply sort divisors.
Edge Cases: n=2. Divisors: 1,2. f(1)=1+20/2=1. f(2)=2+21/2=2+1=3. Output: 1 3. Let's test: n=2, k=1: passes to neighbor (2), then 2 passes to neighbor (1). Touched: 1 and 2 => sum=3. k=2: passes to self? => sum=1. So correct.
n=3. Divisors: 1,3. f(1)=1, f(3)=3+3*2/2=3+3=6. So output 1 6. Let's test: n=3, k=1: path:1->2->3->1 distinct sum=1+2+3=6. k=2: gcd(2,3)=1 also sum=6? Actually gcd(2,3)=1 => m=3, sum=6. k=3: m=1 sum=1. So correct.
n=4. Divisors: 1,2,4. f(1)=1, f(2)=2+41/2=2+2=4, f(4)=4+43/2=4+6=10. Output 1 4 10. Check: n=4, k=2 (d=2,m=2) visited: 1,3 sum=4. k=1 or3 or? k=1 (d=1,m=4) sum=1+2+3+4=10. k=4 sum=1. k=3: gcd(3,4)=1 => sum=10. So correct.
n=5. Divisors:1,5. f(1)=1, f(5)=5+5*4/2=5+10=15. Output 1 15.
n=12. Divisors: many. f will be increasing with m.
Thus algorithm works.
Now complexity: generating divisors up to sqrt(1e9) ~31623, which is trivial.
Implementation details: Use int for i, but ii might overflow if i is int up to 31623, ii about 1e9, fits in 32-bit signed (max ~2e9). But safe to use long long for i or cast i*i to long. But using int is fine.
Now check if any pitfalls: The output order: space-separated in increasing order. Use vector funs, compute, sort, then print.
Potential integer overflow: n*(m-1) might be up to 1e9 * (1e9-1) ≈ 1e18, which fits in signed 64-bit (max 9.22e18). So use long long.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
vector<ll> divs;
for (ll i = 1; i*i <= n; ++i) {
if (n % i == 0) {
divs.push_back(i);
if (i != n/i) divs.push_back(n/i);
}
}
vector<ll> fun;
for (ll m : divs) {
ll f = m + (n * (m-1)) / 2;
fun.push_back(f);
}
sort(fun.begin(), fun.end());
for (size_t i = 0; i < fun.size(); ++i) {
if (i) cout << ' ';
cout << fun[i];
}
cout << endl;
return 0;
}
But careful: The expression (n * (m-1)) / 2 is integer division; we know it's even, but in C++ integer division truncates. Since it's even, it's exact. But to avoid any possibility of rounding error if n*(m-1) is odd (though we argued it's even), we could compute f = (2m + n(m-1)) / 2. But both same.
We could also compute f = m + n*(m-1)/2 using integer division, but ensure that n*(m-1) is computed as ll.
Alternatively, we could compute as (m * (n+2) - n) / 2? Let's derive: f = m + n*(m-1)/2 = (2m + n m - n)/2 = (m(n+2) - n)/2. That may be simpler: f = (m*(n+2) - n)/2. But careful: m*(n+2) may overflow? n up to 1e9, n+2 up to 1e9+2, m up to 1e9, product up to ~1e18+2e9 ~ 1e18, still safe. And since numerator is even, division yields integer. Indeed m*(n+2) - n = 2m + n(m-1), same. So you can compute f = (m*(n+2) - n) / 2.
Will it always be integer? As argued, yes.
But to be safe, we can compute using long long.
Now think about the statement: "Find and report the set of possible fun values for all choices of positive integer k." So we need to output each fun value exactly once. Since each divisor yields one fun value, it's fine.
But note: For n up to 1e9, number of divisors is at most about 1344? Actually the maximum number of divisors for numbers up to 1e9 is something like 1536? I recall that the highly composite numbers around 1e9 have around 1344 divisors (maybe more). For 1e9 exactly, its prime factorization is 2^9 * 5^9? Actually 1e9 = 2^9 * 5^9? 2^9=512, 5^9=1953125, product exceeds 1e9. Actually 1e9 = 10^9 = 2^9 * 5^9? 2^9=512, 5^9=1953125, product=5121953125= 1,000,000,000 exactly, so yes 1e9 = 2^9 * 5^9, number of divisors = (9+1)(9+1)=100. So not large. The maximum number of divisors for n up to 1e9 is achieved by numbers like 73513440 (maybe). Actually I recall that the maximal number of divisors for n ≤ 10^9 is 1344 (maybe 1536). But it's within 10^5 as problem states. So fine.
Thus solution is simple.
But wait: Could there be any other fun values derived from k that are not captured by the divisor approach? Let's double-check the process: Starting at 1, we pass to i+k mod n (with wrap). But the visited set is the orbit of 1 under adding k repeatedly. In a cyclic group of order n, the orbit of an element under addition of k is the coset of the subgroup generated by k. Starting from 1 (which corresponds to residue 1 mod n or 0-index 0). So it's exactly the subgroup generated by k, as we used. That subgroup is multiples of d. So it's correct.
Thus answer is as described.
One more nuance: The problem statement says: "He picks a positive integer k at most n, and passes the ball to his k-th neighbour in the direction of increasing ids". Could it be that "k-th neighbour" means the k-th person from him, not counting himself? That's what we used. So moving k steps. Yes.
Thus solution is straightforward.
But wait: Could k be greater than n? No, at most n.
Therefore we are done.
But I see a potential twist: The ball is passed until person 1 gets the ball back. But what if the ball returns to 1 after some steps, but not necessarily after exactly m steps? That is the first time. So it's fine.
Now verify with more examples: Suppose n=9. Divisors: 1,3,9. f(1)=1, f(3)=3+92/2=3+9=12, f(9)=9+98/2=9+36=45. Output 1 12 45. Let's test manually: For k=3, gcd(3,9)=3 => m=3, visited ids: 1, 1+3=4, 1+6=7 => sum=12. For k=6, gcd(6,9)=3 => same. For k=9 => m=1 sum=1. For others (k=1,2,4,5,7,8) gcd=1 => m=9 sum=45. So correct.
Thus answer is consistent.
Now implement.
But wait: The problem says "the ball always gets back to the 1-st player after finitely many steps, and there are no more than 10^5 possible fun values for given n." So indeed number of divisors can't exceed 1e5. For n up to 1e9, it's fine.
Edge Cases: n is prime? Then divisors: 1, n. f: 1, n + n*(n-1)/2 = n + n(n-1)/2 = n*(n+1)/2? Actually compute: f(n) = n + n*(n-1)/2 = (2n + n^2 - n)/2 = (n^2 + n)/2 = n(n+1)/2. So output: 1, n(n+1)/2. That matches all people sum when k not multiple of n. Good.
Now finalize.
But check the note in second example: "the values 1, 10, 28, 64 and 136 are achieved for instance for k = 16, 8, 4, 10 and 11, respectively." Let's test: n=16, k=10: gcd(10,16)=2 => m=8 => f=8+16*7/2=8+56=64, matches. k=11: gcd(11,16)=1 => m=16 => f=136. So correct.
Now confirm that k=8 gives gcd=8 => m=2 => f=2+161/2=10, matches. k=4 gives gcd=4 => m=4 => f=4+163/2=4+24=28, matches. k=16 gives m=1 => f=1.
Thus all good.
Thus solution is just find divisors and compute formula.
Now answer.
But wait: Could there be a scenario where the set of visited people is not exactly the multiples of d? Since we start at 1, which is not at 0 in the 0-index representation? In 0-index, starting at 0. Multiples of d are {0, d, 2d,...}. In 1-index, that corresponds to {1, 1+d, 1+2d,...}. But are those all numbers congruent to 1 mod d? Yes, for d dividing n, the set {1, 1+d, ..., 1+(n-d)} are exactly numbers with remainder 1 mod d. That's correct.
Thus answer.
But is there any requirement to handle large n with many divisors? Sorting O(d log d) where d <= ~1e5 is fine.
Now I will produce final answer.
But wait: Could the fun value be computed as sum of arithmetic progression: first term a1 = 1, last term an = 1 + (m-1)d, number m, sum = m(first+last)/2 = m*(2 + (m-1)d)/2. That yields m + dm(m-1)/2. And d = n/m. So f = m + n(m-1)/2. Yes.
Now finalize.
One more check: For n=1? But constraint says n>=2.
Now I'll implement.
But there's a nuance: The problem's output expects space-separated integers in increasing order. Ensure no trailing space.
Now provide code.
But we need to ensure we compute divisors efficiently. Since n up to 1e9, sqrt loop up to 31623, okay.
Now let's test with n=12: divisors: 1,2,3,4,6,12. f: m=1 ->1; m=2 ->2+121/2=2+6=8; m=3 ->3+122/2=3+12=15; m=4 ->4+123/2=4+18=22; m=6 ->6+125/2=6+30=36; m=12 ->12+12*11/2=12+66=78. Sorted: 1,8,15,22,36,78. Let's simulate: k=12 => m=1 ->1; k=6 => gcd(6,12)=6 -> m=2 -> visited {1,7}? Wait 1+6=7 sum=1+7=8; k=4 => gcd=4 -> m=3 -> visited {1,5,9} sum=1+5+9=15; k=3 => gcd=3 -> m=4 -> visited {1,4,7,10} sum=1+4+7+10=22; k=2 => gcd=2 -> m=6 -> visited {1,3,5,7,9,11} sum=1+3+5+7+9+11=36; k=1 or 5 or 7 or 11 => gcd=1 -> m=12 -> sum=78. Works.
Now done.
Potential pitfalls: When computing n*(m-1)/2, if using integer division, ensure the product is computed as long long. So cast n to long long, or declare variables as long long.
Implementation:
int main() {
long long n;
cin >> n;
vector<long long> divs;
for (long long i = 1; i*i <= n; i++) {
if (n % i == 0) {
divs.push_back(i);
if (i != n / i) divs.push_back(n / i);
}
}
vector<long long> ans;
for (auto m : divs) {
ans.push_back(m + (n * (m - 1)) / 2);
}
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i++) {
if (i) cout << " ";
cout << ans[i];
}
cout << endl;
return 0;
}
That's it.
But wait: In the formula, we used m + n*(m-1)/2. But we need to guarantee that the division is integer and that we are not losing precision. Since n and m-1 are integers, product may be odd, but we argued it's always even. Let's prove: n*(m-1) is even. n and m are divisors. If n is even, done. If n is odd, then all divisors m are odd. Then m-1 is even. So product even. So safe.
Thus final.
But the problem statement says: "the set of possible fun values for all choices of positive integer k." It might be that k can be any positive integer, not necessarily ≤ n? Actually it says "at most n". So limited. But anyway.
Now note: Could there be any scenario where k is such that the ball returns to 1 after some steps but the visited set is not exactly the multiples of d? Suppose k is not relatively prime to n, but the start is 1. In additive group, the orbit of 0 under addition of k is the subgroup generated by k, which is multiples of d. So it's correct.
Thus solution is trivial.
But maybe there is a twist: The ball is passed to the k-th neighbour in the direction of increasing ids, but what about the fact that the neighbor is counted from the current person? For person i, the k-th neighbor in increasing direction would be the person with id (i + k) mod n, but careful: If i + k > n, we wrap and get (i + k) - n. That's exactly addition modulo n, with result in 1..n. So it's correct.
Thus answer.
Now final output.
However, before finalizing, consider possibility that the set of visited people could be something else if the ball returns earlier? But the first return is after L steps. But could the ball touch 1 earlier? For it to touch 1 before L steps, we would need some t < L such that t*k ≡ 0 mod n. But L is the smallest positive t. So no earlier.
Thus all good.
Now for completeness, let's produce the reasoning in the solution explanation: The visited set is the arithmetic progression with difference d = gcd(k,n) and includes all numbers congruent to 1 modulo d. Then sum derived as function of m = n/d. So the fun values correspond to divisors m.
Thus done.
Now answer.
Potential improvements: Instead of generating divisors, we could generate all m = n / d where d divides n. But it's same.
I'll write final solution.
Now the code: include iostream, vector, algorithm, cmath? Not needed.
We can also use set to avoid duplicates but not needed.
Now final. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
// Collect all divisors of n
vector<ll> divisors;
for (ll i = 1; i * i <= n; ++i) {
if (n % i == 0) {
divisors.push_back(i);
if (i != n / i) {
divisors.push_back(n / i);
}
}
}
// Compute fun value for each divisor m = number of distinct participants
vector<ll> fun;
for (ll m : divisors) {
// f = m + n*(m-1)/2
fun.push_back(m + (n * (m - 1)) / 2);
}
// Sort increasingly (the function is monotone in m, but we sort anyway)
sort(fun.begin(), fun.end());
// Output
for (size_t i = 0; i < fun.size(); ++i) {
if (i) cout << ' ';
cout << fun[i];
}
cout << endl;
return 0;
}