We need to compute the probability that Vasya can uniquely determine k given optimal strategy.
We have string s of length n. Kolya randomly chooses k uniformly from {0,..., n-1}. Then he constructs t = rotate left by k: t[i] = s[(i+k) mod n]? Let's define: s indices 1..n. t = s_{k+1}s_{k+2}...s_n s_1...s_k. So t[1] = s[k+1] (with index modulo n, 1-indexed). Vasya is told string s. He then asks Kolya to reveal the first letter of t, i.e., t[1] = s[k+1]. He sees this letter. Then, after seeing it, he can choose one more position to ask Kolya to reveal: he chooses an index i (presumably from 2..n? He can choose any position? The problem says "open one more letter on some position, which Vasya can choose." It doesn't forbid the first letter (but he already knows it) but likely chooses a distinct position. Usually it's another letter. So he can pick any index j (maybe 2 <= j <= n). After seeing that letter, he then must guess k uniquely. If there are multiple possible k consistent with s and the revealed letters, he loses.
Vasya wants to maximize probability of winning. He can adapt his choice of the second position based on the first letter he saw. He may also have a strategy to guess k without asking second letter? But he must ask one more letter after seeing the first? The problem says: "he asks Kolya to tell him the first letter of the new string, and then, after he sees it, open one more letter on some position, which Vasya can choose." So he always opens exactly one more letter. He must then guess k after that. He cannot skip asking the second; he must use that information. However, he could possibly determine k uniquely from just the first letter if that already identifies a unique shift? Actually, after seeing the first letter, there could be multiple possible k that produce that letter as first. He will then ask a second letter to disambiguate. If after seeing the second letter, only one k remains, he wins. So his strategy is: after seeing first letter = a, choose a position j (2..n) to reveal. Based on both letters, he will be able to compute k if uniquely determined. He wants to maximize the chance that for the random k, the chosen j leads to unique identification.
But note: He can adapt j based on a. So for each possible first letter value (or the actual observed a), he can choose a j (maybe a function of a) that gives the best chance of uniquely identifying k among those shifts that have first letter a. Since k is uniformly random, the probability of winning is sum over a of Pr(first letter = a) * (max_{j} probability (given first letter a, picking j yields unique k). Or more precisely, for each a, we consider the set K_a = {k: s[k+1]=a}. Since k is uniform, conditioned on first letter a, k is uniformly distributed over K_a (because each k gives a particular first letter; the distribution of k conditional on a is uniform over those k with that property). Then Vasya chooses a position j (from 2..n) to query. After seeing the letter at t[j], he can eliminate some k. The condition for winning is that among k in K_a, there is exactly one that matches the observed letter at position j (the letter at t[j] = s[(k+j) mod n]? careful: t[j] = s[(k + j) mod n] with 1-index but need modulo correctly: t[j] = s[ ((k+j-1) mod n) + 1 ]? Actually, if s indices 0..n-1 easier: s[0..n-1]. Choose k in [0,n-1]. t = rotate left by k: t[i] = s[(i+k) % n] for i=0..n-1? If we index from 0: t[0] = s[k], t[1] = s[(k+1)%n], ..., t[j] = s[(k+j)%n]. But problem uses 1-index: first letter is t[1] = s[k+1] if 1-index, but careful: if s indices 1..n, k in 0..n-1, then t[1] = s[k+1] (with k+1 modulo n? but k in [0,n-1]; k+1 can be n, then s[n] so it's fine; if k = n-1, then t[1] = s[n]; t[2] = s[1] etc.) So using 0-index: let s[0..n-1]. Choose k uniformly from 0..n-1. t[i] = s[(i+k) % n] for i=0..n-1? But if i=0 corresponds to first letter, then t[0] = s[k]; if i=1 is second, t[1] = s[(k+1)%n]. In problem, first letter is s[k+1] under 1-index; under 0-index it would be s[(k) % n]? Let's convert: 1-index: s[1..n], k from 0 to n-1. Then t[1] = s[k+1]; if k+1 > n, then it's s[k+1 - n]? Actually k+1 in [1, n]; when k = n-1, k+1 = n, so t[1]=s[n]; when k = 0, t[1]=s[1]. So indeed t[1] = s[(k)?? Not exactly: With 0-index, let s[0..n-1]. To match: if we define k' = k? Let s_0 be original first letter? Let's map: s_1 (1-index) corresponds to s[0] (0-index). Then t_1 = s_{k+1}: s index k+1 (1-index) -> s[k] (0-index) because if s_{i} (1-index) => s[i-1] (0-index). So t_1 corresponds to s[k] (since k+1-1 = k). So with 0-index: t[0] = s[k]. Then t[2]? t_2 = s_{k+2} if k+2 <= n else s_{k+2-n}. In 0-index, t[1] = s[(k+1)%n]? Because t_2 corresponds to s index (k+2-1) = k+1 mod n? Actually if k+1 < n, then s[k+1]; if k+1 = n, then s[0]? Since k+1 = n corresponds to index n (1-index) which is s[0] in 0-index (since s[n] -> s[0]? Wait careful: s[1..n]; if k+2 > n, then wrap: t_2 = s_{k+2-n}. In 0-index, s_{i} => s[i-1]. So if k+2-n = j, then t_2 => s[j-1]. Alternatively, we can define: t[i] (0-index) = s[(k + i) % n]. Then t[0] = s[k]; t[1] = s[(k+1)%n]; ... This matches: For i=0, t[0]=s[k]; for i=1, t[1]=s[(k+1)%n] which equals s[k+1] if k+1 < n, else s[0]. But s[0] corresponds to s_1. In the 1-index description, t_2 = s_{k+2} if k+2 <= n else s_{k+2-n}. Note k+2-n = (k+2) - n. If k=n-1, then k+2 = n+1, so k+2-n=1, so t_2 = s_1. In 0-index, t[1] = s[(n-1+1)%n] = s[0] -> s_1. So correct. So we can work with 0-index: s length n, k uniform in [0, n-1]. t[i] = s[(k + i) % n] for i = 0..n-1. Vasya sees t[0] (first letter). Then he chooses some index j from 1..n-1 (since first already known) to see t[j]. After seeing both, he must deduce k uniquely.
Thus: given s, we need to compute the maximum probability that after seeing t[0] and t[j] (for chosen j depending on t[0]), k is uniquely determined.
Equivalently: For each possible first letter a, consider the set K_a = { k in [0,n-1] : s[k] = a } (since t[0] = s[k] because i=0). Then conditional on a, k is uniform over K_a. Vasya picks a j in {1,...,n-1} based on a. After picking j, he sees t[j] = s[(k+j) % n] = s[ (k + j) mod n ]. For a given a and j, the observed pair (a, b) where b = t[j] will occur for all k in K_a such that s[(k+j)%n] = b. Among those k, if exactly one such k yields that observed b (i.e., the set of k in K_a with that property has size 1), then Vasya wins for that particular (k, j). If more than one, he loses because he can't distinguish which k it is. (If none? impossible because the true k will yield some b; but when he sees b, that means exactly those k that give that b are possible; if there were zero, that can't happen because the observed b is from the actual k, so at least one exists.)
Thus, for a fixed a and j, the probability of winning conditioned on a (i.e., fraction of k in K_a for which, given the observed b from that k, the set of k' in K_a with s[(k'+j)%n] = b has size exactly 1) is:
For each k in K_a, define S(k, j) = { k' in K_a : s[(k'+j)%n] = s[(k+j)%n] }. Then after seeing b = s[(k+j)%n], the possible k' are exactly S(k, j). Vasya wins if |S(k, j)| = 1. So probability = (number of k in K_a with |S(k, j)| = 1) / |K_a|.
But note that Vasya chooses j after seeing a but before seeing b. So he wants to maximize that fraction for that a. So for each a, the best j yields winning prob p_a = max_{j in 1..n-1} (count_{k in K_a} [ |S(k, j)| = 1 ] / |K_a| ). Then overall probability = sum_{a} P(a) * p_a, where P(a) = |K_a| / n (since k uniform). So overall probability = (1/n) * sum_{a} |K_a| * p_a = (1/n) * sum_{a} max_{j} (count_{k in K_a} [|S(k,j)| = 1]).
But careful: Since Vasya chooses j after seeing a, we need to make sure that j can depend on a. Yes.
Thus the problem reduces to: For each character a that appears in s, we have set of positions K_a = {i : s[i] = a} (0-index). For each offset j (1 <= j <= n-1), for each k in K_a, define the group of k' in K_a that have the same character at position (k'+j) mod n as (k+j) mod n. Actually we need to consider the multiset? But groups are equivalence classes: For a fixed j, we partition K_a based on the value b = s[(k+j)%n]. For each b, let group G_{a,j,b} = { k in K_a : s[(k+j)%n] = b }. Then for any k in such group, the size of S(k,j) is |G_{a,j,b}|. So Vasya wins for that k iff the group size is 1. So count of winning k for (a,j) = number of singleton groups = sum_{b} [ |G_{a,j,b}| == 1 ] * 1? Actually each singleton group contributes exactly 1 k. So count_{k in K_a} [|S(k,j)| = 1] = number of k in K_a that are alone in their group = number of groups of size 1.
Thus for each (a,j), we compute groups based on mapping f_{j}: k -> s[(k+j)%n]. For k in K_a, we look at f_j(k). For each distinct f_j(k) value, count how many k map to it. Then count of singleton groups = number of values where count = 1.
Then p_a = max_j ( (number of singleton groups) / |K_a| ).
Then answer = (1/n) * sum_{a} |K_a| * p_a = (1/n) * sum_{a} max_j ( number of singleton groups for (a,j) ).
Thus we need to compute, for each character a, the maximum over j of the number of k in K_a that have unique f_j(k) values (i.e., no other k in K_a maps to the same f_j(k)). And then sum these maxes, divide by n.
We need to do this efficiently for n up to 5000. Brute force over all a and j: number of a is at most 26? Actually letters are lowercase English, so at most 26 distinct characters. But also K_a could be many positions. n is 5000, so O(26 * n^2) could be 26*25e6 = 650e6, borderline but maybe okay with optimizations? 5000^2 = 25e6, times 26 = 650 million, too high in Python, might be too slow. We need smarter approach.
But note that characters are only 26, so we can treat each character separately. For each character a, we have set of positions pos = [i0,i1,...,im-1] where s[i] = a. For each shift j (1..n-1), we need to compute how many of these positions have unique s[(i+j)%n] among this set. i.e., we consider the array of "output letters" when we shift each i by j mod n and take s at that index. We need to count values that appear exactly once.
Given that m = |K_a| can be up to n (if all letters same). But if all letters same, then K_a is all positions, then for any j, f_j(k) will all be same letter (since s[(k+j)%n] = that same letter), so all groups have size m (unless m=1 but n>=3 so m>=3). So number of singleton groups = 0. So max_j = 0. So contribution zero. So for characters with many occurrences, we still need to compute max_j; but maybe we can compute efficiently using convolution or string matching? Alternatively, we can compute for each j, the counts of pairs (i, j) such that s[(i+j)%n] = something. Since n=5000, we could possibly do O(n^2) total across all characters if we are clever. But O(n^2 * 26) might be high but maybe feasible in Py with optimizations? Let's compute: n=5000, n^2=25e6. 25e6 operations per character? That's 650 million if we loop over 26. However, we don't need to loop over all 26 characters for each j? We could compute for each character a, we have its positions. For each j, we compute the counts of each output letter among those positions. That is, for each a, for each j, we need to compute a histogram of s[(i+j)%n] for i in pos. Then the number of unique values is count of letters where histogram count =1. But we don't need to know which letters; just count of positions that have unique output. Could be computed by counting collisions.
A naive way: For each a, we have m positions. For each j (1..n-1), we compute for each i in pos: b = s[(i+j)%n], and then we increment a count. But doing m * (n-1) per a leads to O(m * n) per a, and sum over a of m = n, so total O(n^2). Because each position i belongs to exactly one character a. For each a, we would process its m positions across all j? That would be m * (n-1) operations for that a. Summed over all a gives total n * (n-1) = O(n^2). Wait, careful: For each a, we have to evaluate all j (1..n-1). For each j, we need to compute the mapping from i in pos to b. That's m operations per j, so total per a = m * (n-1). Sum over a of m = n, so total = n * (n-1) = O(n^2). That's only 25e6 for n=5000, which is fine! Indeed we can afford O(n^2) total, not O(26 * n^2). Because each position i is processed only for its own character a, and for each j we process all i in that character's set. The total number of (i, j) pairs across all characters is n * (n-1) = ~25e6. That's definitely manageable in Python if we are careful. However, we need to compute for each a and j the number of singleton groups efficiently. Doing a direct per-j loop with counting via a dictionary might be okay if we do it cleverly.
But we need to compute for each a, for each j, the count of i in pos such that s[(i+j)%n] is unique among the pos for that j. How to compute that efficiently?
One approach: For fixed a, we have positions pos[0..m-1]. For each j, we consider array v_j[i] = s[(pos[i] + j) % n]. We need to count elements in v_j that appear exactly once. This is like for each j, compute frequencies of letters (or more precisely, we need to consider collisions; but since s is a string of 26 letters, the output alphabet is only 26. So we can use an array of size 26 for counts. For each j, we can compute counts for each output letter by iterating over i in pos and incrementing count[letter index]. Then after building counts, we can compute number of i for which count[letter_of_i] == 1. But careful: we need count[letter_of_i] == 1 for that specific i. So we could first fill counts, then iterate again over i to count those where counts[letter] == 1. That would be O(m) per j. So total O(n^2) as argued (since sum m over a = n, and j runs n-1 times, so total operations ~ n * (n-1) * constant. That's 25e6 times maybe a few operations, which is acceptable in Python if implemented efficiently (use list of ints, precomputed indices). 25e6 ~ 25 million; Python can handle maybe 0.1 sec per 10 million? Actually Python can do ~50 million simple operations per second? On typical contest machines, 100 million may be borderline. But 25 million might be okay, especially if we optimize loops using built-in functions, but we also need to do it for each character separately. But since total i,j pairs is n*(n-1)=25e6, that's the total work across all characters. So we can just loop over all characters, for each compute m and then for each j from 1 to n-1 compute counts. That's O(n^2) total. With n=5000, n^2=25e6, that's definitely okay. We can even do a bit more.
But we must be careful: For each a, we need to compute max_j over (n-1) j of the count of unique positions. So we can compute for each j the count and keep the maximum for that a. Then sum max over a and divide by n.
Implementation details:
- Input string s of length n.
- Preprocess s to list of integer letter indices: ord(c)-97 => 0..25.
- For each character ch from 0 to 25:
- Gather positions pos = [i for i, c in enumerate(s) if c == ch] (0-index). m = len(pos).
- If m == 0: skip (max = 0).
- For this a, we need to evaluate for each shift j from 1 to n-1 the number of i in pos such that the letter at (i+j)%n is unique among pos for that j.
- Approach: For each j, compute count array cnt[26] initialized to 0.
For each i in pos:
b = s[(i+j) % n] # integer 0..25
cnt[b] += 1
Then after building cnt, count uniq = sum(1 for i in pos if cnt[ s[(i+j)%n] ] == 1)
Keep max_uniq = max(max_uniq, uniq)
- After loop over j, contribution = max_uniq. (Note that if m>0, p_a = max_uniq/m, but we sum |K_a| * p_a = max_uniq? Wait, earlier we had overall probability = (1/n) * sum_{a} max_j (number of singleton groups). The number of singleton groups is exactly the count of i in K_a that have unique f_j(i). That's exactly uniq count. So for each a, we compute max_uniq (max over j) of uniq, and sum them, divide by n. So contribution = max_uniq, not divided by m.
- However careful: In the earlier derivation, probability = (1/n) * sum_a max_j ( count of k in K_a where |S(k,j)| = 1). That's exactly sum over a of max_j uniq_count. So yes.
But we must double-check: For each a, max over j of the count of k that would lead to win. When we condition on a, the probability of winning is max_j ( uniq_count / |K_a| ). So overall probability = sum_a (|K_a|/n) * (max_j uniq_count/|K_a|) = sum_a (max_j uniq_count)/n. So sum of max_uniq divided by n. So correct.
Now the above algorithm O(n^2) works for n up to 5000. However, we must be careful: For each a, we have m positions, and we do n-1 loops, each loop does m operations for building cnt plus m operations for counting uniq. That would be 2m(n-1) per a. Summed over a gives 2n(n-1) = O(2n^2) = 50 million operations, still okay. But we can combine the counting step: while building cnt we could also store the list of which letters are hit by which i? But we need to know per i whether its cnt becomes 1 exactly. However we can compute uniq as: After building cnt, we can compute uniq by iterating over i again and checking cnt[letter] == 1. That's fine.
But we could potentially reduce work: For each a, we could precompute for all shifts j the distribution more efficiently using convolution? But O(n^2) is okay.
Edge Cases: n up to 5000, but we need double precision. Output probability with high precision.
Now test on examples:
Example 1: s = "technocup". Let's compute manually? According to output 1.0. That means for each character a, max_uniq = |K_a|, i.e., for every a, there exists a shift j such that all positions with that first letter become unique after seeing second letter. Or overall sum of max_uniq = n, so probability = 1. We'll verify later.
Example 2: "tictictactac". Output 0.333333333333333. n = length? Let's count: "tictictactac" -> 13? Actually length: t i c t i c t a c t a c? Let's count: t(1) i(2) c(3) t(4) i(5) c(6) t(7) a(8) c(9) t(10) a(11) c(12) maybe? Actually "tictictactac" should be 12? Let's not worry, the probability given is 1/3.
Example 3: "bbaabaabbb", n=10, output 0.1.
We'll test algorithm after implementation.
But wait: Is there any nuance? Vasya can choose the second position after seeing the first letter. That means for each a, he can choose the best j. So our calculation per a is max over j. That matches.
Now double-check the logic: After seeing first letter a, he knows k is in set K_a. He then chooses j. Then Kolya reveals t[j] = b. Then he knows that k must be among those k' in K_a for which s[(k'+j)%n] = b. If that set has size 1, he wins. So his chance of winning given a is the probability over random k in K_a that the chosen j leads to a singleton group. However, he chooses j before seeing b, so the probability is (number of k for which the group size = 1) / |K_a| only if he picks a j. He wants to maximize that fraction. So yes.
Thus algorithm seems correct.
Complexity: O(n^2) time, O(n) memory (plus per a we need to loop j). But we need to be careful about modulo operation for each (i,j) access: (i+j)%n. That's cheap.
Now implementation steps:
- Read s as string.
- n = len(s)
- Convert s to list of ints: arr = [ord(c)-97 for c in s]
- For each ch in range(26):
pos = [i for i, c in enumerate(arr) if c == ch]
m = len(pos)
if m == 0: continue
max_uniq = 0
For each shift j from 1 to n-1:
for j in range(1, n):
cnt = [0]*26
# Compute counts of letters at positions (i+j)%n for i in pos
for i in pos:
b = arr[(i+j) % n]
cnt[b] += 1
# Now count how many i have cnt[ arr[(i+j)%n] ] == 1
uniq = 0
for i in pos:
if cnt[ arr[(i+j) % n] ] == 1:
uniq += 1
if uniq > max_uniq:
max_uniq = uniq
# optional: early exit if max_uniq == m, we can break
if max_uniq == m:
break
contribution += max_uniq
But note: For a given a, if max_uniq reaches m, we can stop early.
Edge: j goes from 1 to n-1 inclusive. However, j = 0? Not allowed because he already saw first letter, so he can't ask for the same position? He could ask position 1 again? Problem says "open one more letter on some position". Typically it's a different position? It doesn't explicitly forbid opening the first letter again, but that would give no new information. So it's never optimal, so we can ignore j=0. So we only consider j in 1..n-1.
Now, the above loops for each ch: For each ch, we compute for each j. But total operations across all ch is sum_{ch} m_ch * (n-1) * 2. Since sum m_ch = n, total ~ 2n(n-1) ~ 50 million. For n=5000, that's 50 million inner operations. Each inner operation does indexing, modulo, increments, etc. In Python, 50 million might be okay if optimized (maybe 1-2 seconds). But we can further reduce constant factor.
We can also try to combine loops: Instead of iterating over all j per character separately, we could precompute something? But given constraints, we can attempt.
But note: For each ch, we are recomputing arr[(i+j)%n] for each i and j. This is O(n^2) modulo operations. Modulo operation is slower; we could precompute shifted indices? Since n is only 5000, we can precompute for each offset j the array shifted positions: maybe we can precompute for each i a list of letters? Actually, we could precompute for each i, the letter at (i + j) mod n for all j? That's n*n matrix, 25 million entries, too large memory but could compute on the fly.
We could precompute a matrix of size n x n? That's 25 million, each byte, maybe 25 MB, but could be okay? However, 25 million integers might be more. But we could store as list of arrays of bytes? But Python overhead is high, not advisable.
Alternatively, we can avoid modulo by using two slices: For each j, we can think of rotating the string by j and then mapping positions. But still need to compute indices.
We can also note that for each ch, we only need positions where s[i] = ch. So for each such i, we can precompute the list of letters for all shifts? Actually, for fixed i, as j varies, arr[(i+j)%n] cycles through the string starting from i+1 mod n. So it's essentially the string s shifted. But we could precompute for each i the array of letters for all j: that is the rotated string viewed from i? That would be n x n again.
Maybe we can optimize the inner loops using numpy? Not allowed.
But 50 million operations in Python is likely okay if we code efficiently. Let's estimate: 50 million simple integer operations can take around 1-2 seconds in CPython, maybe more like 2-3 sec. But the time limit is unknown, but typical Codeforces problems with n=5000 and O(n^2) often require optimization in Python. However, we have 26 characters, but we loop over j for each ch. That is basically n * (n-1) iterations across all ch, but we need to do two inner loops per j (first to fill cnt, second to count uniq). That's 2 * n * (n-1) = 2*~25e6 = 50e6. Plus some overhead. Possibly borderline but could pass if well-coded.
We can reduce to one loop: Instead of two loops, we can compute uniq on the fly? If we increment cnt as we go, we don't know final counts until we finish. But we could first compute list of b values for each i, store them, then compute counts and then check. That still two loops but maybe we can combine by using a mapping from b to list of indices? But we can compute uniq as: after we have counts, we need to know for each i if its count ==1. So we need both the b values and the counts. We could store the b values in an array of length m for that j, then compute counts using a histogram, then iterate over stored b values to count ones. That's essentially the same.
Alternatively, we can compute uniq directly without second pass by using two-pass: first count, then second pass check. That's fine.
We can also prune: For each ch, if m=1, then for any j, uniq = 1 (since only one element, its group size is 1). So max_uniq = 1. So we can handle m=1 quickly. But that's trivial.
Now, is there any subtlety: The second position j can be any from 1 to n-1 inclusive. But note that if we pick j that is a multiple of n? No, j<n.
Another nuance: Could Vasya also choose j after seeing the first letter, and he also can decide to not ask anything? No, he must ask.
Now, we need to compute probability exactly as described.
Let's test on the examples manually with code later.
But wait, there might be a more efficient way: For each character a, we need to find the shift j that maximizes the number of positions i in K_a such that no other i' in K_a yields the same s[(i+j)%n]. That is, we need to find j that minimizes collisions.
We can treat for each pair of positions i1, i2 in K_a, for which j do they collide? They collide if s[(i1+j)%n] == s[(i2+j)%n]. That means that the letters at positions (i1+j) and (i2+j) are equal. For a given j, collisions happen when the shifted letters match. For each j, we can compute how many positions have a unique image. Equivalent to: For each j, the number of positions i whose image is unique equals |K_a| minus the number of positions that share their image with at least one other.
Alternatively, we could compute for each j the number of collisions: For each b, if count[b] >=2, then all count[b] positions are non-unique. So uniq = number of b with count=1. So we need to compute count per b.
Instead of O(m*n) per character, we can perhaps use a frequency array for each j but using the fact that we have only 26 letters. For each character a, we can precompute for each offset j the count per output letter using convolution? Since alphabet small, we can compute for each output letter b, a bitmask of positions i in K_a that map to b at offset j. But that's still similar.
Given n=5000, O(n^2) should be fine.
But let's double-check total operations: For each ch, we loop j from 1 to n-1. For each j, we compute for each i in pos: b = arr[(i+j)%n]. For each i we do: idx = (i+j) % n (which might be computed by addition and if >= n subtract n) and then access arr[idx]. Then increment cnt[b]. That's constant. Then second loop same. So 2*m operations per j. sum over ch: 2 * n * (n-1) = 2n(n-1) ~ 50e6. For each operation, Python overhead includes loop overhead, function calls? We'll be in nested loops: for ch in range(26): if pos: for j in range(1,n): for i in pos: ... This could be heavy because the innermost loops are Python-level loops. However, we can try to micro-opt: use local variables, prebind arr, n, etc. Use list of positions, and for each j, we could precompute an array of shifted indices? Not necessary.
But note: For a given ch, m could be large (like n for all same letter). In that case, we loop over j (n-1) and for each j, we loop over all i (n) twice. That's about 2n^2 operations for that ch, which is ~50 million for n=5000. That's okay. But the total across all ch would sum to 2n^2, not 2n^2 per ch. Since sum m = n, the worst-case is when all letters are the same: then there is only one ch with m=n, and we do 2n^2 operations. So actually total operations are O(n^2) total, not O(26n^2). Because each position belongs to exactly one character, so the sum over characters of m = n. In worst-case, one character has all positions, so total operations ~ 2n*(n-1) = O(2n^2). That's 2*25e6 = 50 million. That's absolutely fine.
Thus algorithm is efficient enough.
Now we need to compute arr[(i+j)%n] efficiently. The modulo operation can be replaced by: idx = i+j; if idx >= n: idx -= n. Since i in [0,n-1], j in [1,n-1], i+j can be up to 2n-2. So we can do: idx = i + j; if idx >= n: idx -= n; maybe one subtraction only. That's faster than modulo.
We can precompute arr as a list of ints.
Now we code:
-
n = len(s)
-
arr = [ord(c)-97 for c in s]
-
total_uniq_sum = 0
-
For each ch in range(26):
pos = [i for i, c in enumerate(arr) if c == ch]
m = len(pos)
if m == 0: continue
if m == 1:
total_uniq_sum += 1 # any j works, uniq=1
continue
max_uniq = 0
Precompute maybe the list of pos for quick iteration.
For each j:
for j in range(1, n):
cnt = [0]*26
# First pass: build counts
for i in pos:
idx = i + j
if idx >= n:
idx -= n
b = arr[idx]
cnt[b] += 1
# Second pass: count uniq
uniq = 0
for i in pos:
idx = i + j
if idx >= n:
idx -= n
b = arr[idx]
if cnt[b] == 1:
uniq += 1
if uniq > max_uniq:
max_uniq = uniq
if max_uniq == m: # can't improve
break
total_uniq_sum += max_uniq
-
answer = total_uniq_sum / n
-
print with high precision, e.g., print("{:.15f}".format(answer))
Test on examples.
But let's do mental checks.
Example 1: "technocup". Let's compute manually? We'll trust algorithm.
Example 2: "tictictactac". We'll test with code after writing.
But there might be subtlety: j can be any from 1 to n-1, inclusive. However, what about j such that (i+j)%n wraps around? That's fine.
Now consider if for a given a, we have m positions. For each j, we compute uniq. We need the maximum. That's okay.
But there's an alternative interpretation: Vasya, after seeing first letter a, can choose a position j based on a. But j must be an integer between 1 and n-1 (maybe he could also choose j=0? Not useful). So our max over j is correct.
Now check if we need to consider that Vasya might also use the knowledge of the string s to deduce possible k after seeing both letters? He will be able to identify k uniquely if the pair (first letter, second letter) and the known s uniquely identifies the shift. That is exactly our condition.
Now, is it possible that after seeing two letters, Vasya still cannot uniquely determine k, but maybe he can deduce something else? He must guess exactly k. So if multiple shifts consistent, he loses.
Thus algorithm is correct.
But wait: The second letter is chosen after seeing the first letter. He can choose j based on a. So our max over j per a is fine.
Now, we must ensure we consider all possible j from 1 to n-1. However, note that the shift j is an index in the rotated string t. There is also possibility that Vasya could ask for a position that is not necessarily the second letter? He can choose any position (maybe including the first again)? But that would not provide new info. So we ignore.
Now, is there any scenario where picking j such that the letter observed is the same as the first letter? That's fine.
Now, complexity: For each ch, we allocate cnt[26] each j. That's fine.
We could also precompute for each ch a list of pos and also precompute for each shift j a list of b for each i? But not needed.
But we need to be careful: In the loops, for each j we recompute idx = i+j and check condition. We could precompute for each i an array of the letters at all offsets? That would be a list of length n for each i, total n^2 memory and time to fill, but would eliminate the addition and conditional inside the innermost loop? However, building that table would also be O(n^2) and would likely be slower due to memory overhead. The current approach is fine.
We can also attempt to reduce the number of j's we need to test. Since j from 1 to n-1, but we may only need to consider j up to something? However, we need to consider all j to find max. But we can early break if we hit max_uniq = m.
Also note that for a given a, the number of distinct j that can give different results? All may be needed.
Now test with small strings to verify.
Write a quick mental example: s = "aba", n=3. Let's compute probability.
s = a b a (indices 0:a,1:b,2:a). letters: a appears at positions 0,2; b at 1.
Case a (first letter = a): K_a = {0,2}. For each j (1,2):
- j=1: compute (i+j)%n: i=0 ->1 -> b; i=2 ->0 -> a. So b values: b, a. Counts: b:1, a:1 => both unique => uniq=2. So for j=1, uniq=2.
- j=2: i=0->2->a; i=2->1->b. So same: a,b => uniq=2. So max_uniq=2.
Thus for a, max_uniq=2.
Case b: K_b = {1}, m=1 => max_uniq=1.
Total sum = 2+1=3, n=3, probability=1. So Vasya always wins. Is that correct? Let's think: s="aba". All cyclic shifts: original: aba -> t? Actually shifts: k=0: t=aba; k=1: t=baa; k=2: t=aab. First letters: a for k=0,2; b for k=1. If first letter is b (unique), then k=1, Vasya knows k immediately? But he still must ask a second letter? The problem says he asks the first letter, then after he sees it, open one more letter on some position, and then guess k. He could ask a second letter but if the first letter already uniquely identifies k, he can still ask some second letter; but after seeing second letter, he still knows k uniquely because only one k consistent with first letter? But careful: After seeing first letter b, only k=1 is possible. So regardless of second letter, he knows k uniquely. However, the condition for winning is that after seeing both letters, he can determine k. If there is exactly one k consistent, he wins. So even if the second letter doesn't provide new info, there is still exactly one k. So he wins. In our approach for b, m=1, then any j yields uniq=1 because the only k yields a group of size 1. So our method gives max_uniq=1, correct.
If first letter is a (two possibilities k=0 or 2). Vasya chooses j=1. Then t[1]? For k=0, t=aba => second letter = b; for k=2, t=aab => second letter = a? Wait: For k=2, t = rotate left by 2: s[2] s[0] s[1] = a a b? Actually s indices: 0:a,1:b,2:a. k=2 => t[0]=s[2]=a, t[1]=s[0]=a, t[2]=s[1]=b. So second letter = a. So for j=1, observed second letters: if k=0 => b; if k=2 => a. So they differ, so after seeing second letter, Vasya can tell which k. So uniq=2. So j=1 works. So probability = 1. Good.
Now test example "aaa". n=3, all letters same. For each a, K_a = {0,1,2}, m=3. For any j, all s[(i+j)%n] = a (since all letters a), so counts: a:3, uniq=0. So max_uniq=0. So total sum = 0, probability = 0. So Vasya never can win. Is that correct? If s is all same, then all cyclic shifts are identical, so t is same string regardless of k. The first letter always a. Then second letter also a. So Vasya cannot distinguish k. So probability 0. Good.
Now test example 2: "tictictactac". We'll write small script to verify later.
Now ensure that we considered that Vasya can choose different j for different first letters. Our per-character max_uniq accounts for that.
Now, but is there any scenario where Vasya might want to use a probabilistic strategy? For instance, after seeing first letter, he might randomize his choice of j to increase chance? But we assume he chooses j deterministically to maximize probability. However, could a mixed strategy yield higher probability than a deterministic one? Since the probability is averaged over k (which is random) and Vasya's strategy can depend on observed a. For a fixed a, the probability of winning for a deterministic choice of j is (number of k with unique group)/|K_a|. Since the distribution of k is uniform given a, any randomized choice over j yields a convex combination of these fractions, which cannot exceed the maximum. So deterministic optimal.
Thus we are good.
Now we just need to implement efficiently.
Potential optimization: For each character a, we have list pos. For each j, we can compute the mapping from i to b. Since we need to compute cnt for 26 letters, we can use array of ints. But we need to reset cnt for each j. Instead of creating new list each time, we can reuse an array and zero it. However, zeroing 26 ints each j is trivial.
But note: In Python, creating a new list of 26 zeros each j might be fine. But we can allocate cnt = [0]*26 and then assign inside the j loop: we need to reset to zeros each iteration. We could do cnt = [0]*26 and then after using, we need to set back to zero for next j. But we can just create a new list each j; overhead is small.
But we can also precompute for each a, for each i, the entire cyclic sequence of letters? Not needed.
One more nuance: The sum of max_uniq across characters can be computed. But we need to ensure that we treat each character correctly. However, there might be interplay: What if for a given first letter a, Vasya could choose j differently based on something else? No, only based on a. So per-character optimization is correct.
Now test with the examples using code.
Let's quickly simulate with Python in mind.
We'll implement:
def solve():
import sys
s = sys.stdin.readline().strip()
n = len(s)
arr = [ord(c)-97 for c in s]
total = 0
for ch in range(26):
pos = [i for i in range(n) if arr[i] == ch]
m = len(pos)
if m == 0:
continue
if m == 1:
total += 1
continue
best = 0
# We can attempt to speed up by precomputing for each i the string of letters? Not needed.
# For j in 1..n-1
for j in range(1, n):
cnt = [0]*26
# First pass
for i in pos:
idx = i + j
if idx >= n:
idx -= n
b = arr[idx]
cnt[b] += 1
# Second pass
uniq = 0
for i in pos:
idx = i + j
if idx >= n:
idx -= n
b = arr[idx]
if cnt[b] == 1:
uniq += 1
if uniq > best:
best = uniq
if best == m:
break
total += best
ans = total / n
print("{:.15f}".format(ans))
We should test.
But careful: The modulo operation: idx = i + j; if idx >= n: idx -= n; This works because i+j < 2n, so subtract once is enough. For idx >= n, subtract n. That's correct.
Now, we can also precompute arr as a list of ints.
Now let's test with given examples manually using Python? Since I'm not executing, let's do mental or small checks.
Example 1: "technocup". Let's compute quickly? I suspect that each character appears at most once? "technocup" letters: t,e,c,h,n,o,u,p? Actually "technocup": t(1), e(1), c(2? t e c h n o c u p? spelling: t e c h n o c u p - c appears twice? Wait: "technocup": t, e, c, h, n, o, c, u, p. Yes c appears twice, others unique. So for characters that appear once, m=1 => uniq contribution 1 each. For c which appears twice, we need to see if there exists j such that both positions become unique. Let's compute: s = t e c h n o c u p. indices 0..8. c at positions 2 and 6. For j from 1 to 8, we need to check if s[(2+j)%9] and s[(6+j)%9] are distinct. If they are distinct, then both become unique? Actually we also need to consider collisions with other letters? Wait, we only consider the set K_a (c). There are only two positions. For them to both be unique, we need that the two output letters are different (so each has count 1). That would give uniq=2. If they are same, then each count=2, uniq=0. So we need to find j such that letters at (2+j) and (6+j) mod 9 are different. Since the string has many letters, likely there exists such j. For j=1: positions 3 and 7: s[3]=h? Actually s[0]=t,1=e,2=c,3=h,4=n,5=o,6=c,7=u,8=p. For j=1: (2+1)=3 -> h; (6+1)=7 -> u; different => uniq=2. So best=2. So total sum: For each of 8 distinct letters (t,e,c? Actually let's list all letters: t,e,c,h,n,o,c,u,p. Distinct: t,e,c,h,n,o,u,p -> 8 distinct? But c appears twice, so total distinct characters: 7 unique + c = 8 distinct? Actually t, e, c, h, n, o, u, p are 8 distinct letters, but c appears twice, so total positions = 9. So characters: t(1), e(1), c(2), h(1), n(1), o(1), u(1), p(1). So sum of max_uniq: for each single:1 => 7 singles contribute 7. For c:2 => total 9. Sum=9. n=9 => probability=1. Good.
Example 2: "tictictactac". Let's compute roughly: We'll trust algorithm gives 1/3.
Example 3: "bbaabaabbb". n=10. Let's list: b b a a b a a b b b? Actually string: b b a a b a a b b b? Let's write indices 0..9: b(0), b(1), a(2), a(3), b(4), a(5), a(6), b(7), b(8), b(9). So counts: a appears at indices 2,3,5,6 => m_a=4; b appears at indices 0,1,4,7,8,9 => m_b=6. So total sum of max_uniq across characters: we need to compute best j for a and b.
We need to see probability = 0.1 => total sum = 0.1*10 = 1. So sum of max_uniq =1. So either only one character contributes 1, the other 0, or combinations sum to 1. Since m_b=6 and m_a=4, the sum of max_uniq can't exceed m_a+m_b=10, but we get 1. So only one position overall can be uniquely identified? That seems improbable. Let's compute manually perhaps.
We can try to compute for a: positions: 2,3,5,6. For each j from 1..9, we need to compute number of positions where the letter at (i+j)%10 is unique among these four.
Let's compute s: index:0:b,1:b,2:a,3:a,4:b,5:a,6:a,7:b,8:b,9:b.
We need for each j, the values at positions (2+j)%10, (3+j)%10, (5+j)%10, (6+j)%10.
Let's compute table:
j=1: indices: 3,4,6,7 -> s[3]=a, s[4]=b, s[6]=a, s[7]=b => letters: a,b,a,b. So counts: a:2, b:2 => uniq=0 (since each count>1). So 0.
j=2: indices:4,5,7,8 -> s[4]=b, s[5]=a, s[7]=b, s[8]=b => letters: b,a,b,b => a:1, b:3 => uniq=1 (a appears once). So uniq=1.
j=3: indices:5,6,8,9 -> s[5]=a, s[6]=a, s[8]=b, s[9]=b => a,a,b,b => uniq=0.
j=4: indices:6,7,9,0 -> s[6]=a, s[7]=b, s[9]=b, s[0]=b => a,b,b,b => a:1, b:3 => uniq=1.
j=5: indices:7,8,0,1 -> s[7]=b, s[8]=b, s[0]=b, s[1]=b => all b => uniq=0.
j=6: indices:8,9,1,2 -> s[8]=b, s[9]=b, s[1]=b, s[2]=a => b,b,b,a => a:1, b:3 => uniq=1.
j=7: indices:9,0,2,3 -> s[9]=b, s[0]=b, s[2]=a, s[3]=a => b,b,a,a => uniq=0.
j=8: indices:0,1,3,4 -> s[0]=b, s[1]=b, s[3]=a, s[4]=b => b,b,a,b => a:1, b:3 => uniq=1.
j=9: indices:1,2,4,5 -> s[1]=b, s[2]=a, s[4]=b, s[5]=a => b,a,b,a => a:2,b:2 => uniq=0.
So for a, max_uniq = 1 (achieved at several j). So contribution from a is 1.
Now for b: positions: 0,1,4,7,8,9. Compute for each j:
We need to compute letters at (i+j)%10 for i in {0,1,4,7,8,9}.
Let's compute s array: 0:b,1:b,2:a,3:a,4:b,5:a,6:a,7:b,8:b,9:b.
We'll compute for each j:
j=1: indices:1,2,5,8,9,0 -> s[1]=b, s[2]=a, s[5]=a, s[8]=b, s[9]=b, s[0]=b => letters: b,a,a,b,b,b => a:2, b:4 => uniq=0 (since all counts >1? Actually a count 2, b count 4 => no singletons)
j=2: indices:2,3,6,9,0,1 -> s[2]=a, s[3]=a, s[6]=a, s[9]=b, s[0]=b, s[1]=b => a,a,a,b,b,b => a:3,b:3 => uniq=0
j=3: indices:3,4,7,0,1,2 -> s[3]=a, s[4]=b, s[7]=b, s[0]=b, s[1]=b, s[2]=a => a,a,b,b,b,b? Actually list: a (3), b (4), b (7), b (0), b (1), a (2) => a:2, b:4 => uniq=0
j=4: indices:4,5,8,1,2,3 -> s[4]=b, s[5]=a, s[8]=b, s[1]=b, s[2]=a, s[3]=a => b,a,b,b,a,a => a:3,b:3 => uniq=0
j=5: indices:5,6,9,2,3,4 -> s[5]=a, s[6]=a, s[9]=b, s[2]=a, s[3]=a, s[4]=b => a,a,b,a,a,b => a:4,b:2 => uniq=0
j=6: indices:6,7,0,3,4,5 -> s[6]=a, s[7]=b, s[0]=b, s[3]=a, s[4]=b, s[5]=a => a,b,b,a,b,a => a:3,b:3 => 0
j=7: indices:7,8,1,4,5,6 -> s[7]=b, s[8]=b, s[1]=b, s[4]=b, s[5]=a, s[6]=a => b,b,b,b,a,a => b:4,a:2 =>0
j=8: indices:8,9,2,5,6,7 -> s[8]=b, s[9]=b, s[2]=a, s[5]=a, s[6]=a, s[7]=b => b,b,a,a,a,b => a:3,b:3 =>0
j=9: indices:9,0,3,6,7,8 -> s[9]=b, s[0]=b, s[3]=a, s[6]=a, s[7]=b, s[8]=b => b,b,a,a,b,b => a:2,b:4 =>0
So for b, max_uniq seems 0 for all j. So contribution from b = 0. So total sum = 1, probability = 0.1. That matches example. So algorithm would produce that.
Thus algorithm works.
Now ensure we handle m=0: skip.
But wait: There is nuance: For a character a with m>1, we need to compute best j. For each j, we computed uniq count. But could there be a case where the best j yields uniq < m, but maybe there is a different j that yields a better chance? We are taking max, that's fine.
Now complexity: For each ch with m large, we loop over all j. Could there be many ch? At most 26, but sum of m across all ch is n. So the total inner loops across all ch is O(n^2). That's fine.
But we need to be careful about the case where m is large but we can early break if we achieve max_uniq = m. That's helpful.
Now we need to output with absolute or relative error <= 1e-6. So printing with 12-15 decimal digits is fine.
Now we should also consider possible pitfalls: The first letter Vasya sees is t[0] = s[k]. But in our mapping, we used positions i where s[i] = a. That matches: K_a = {k: s[k] = a}. So yes.
Now verify: The second letter t[j] = s[(k+j)%n]. So we used that.
Now edge: n=3, all distinct: s = "abc". Then each character appears once, so each max_uniq=1, total sum = 3, probability = 1. Let's test: s="abc". k=0 => t="abc", first a; k=1 => "bca", first b; k=2 => "cab", first c. Each first letter unique, so Vasya knows k immediately. But he still asks a second letter, but it doesn't matter. So probability = 1. Good.
Now consider s = "abca". n=4, letters: a at 0,3; b at 1; c at 2. For a: positions 0,3. Compute best j? For j=1: (0+1)=1 -> b, (3+1)=0 -> a => b and a distinct => uniq=2. So max_uniq=2. For b: m=1 =>1. For c: m=1 =>1. total sum = 2+1+1=4, probability=1. So always win. Check: k possibilities: if first letter a (k=0 or 3), j=1 gives second letters: for k=0, t[1]=b; for k=3, t[1]=a; distinct => win. So yes.
Now consider s = "aab". n=3, a at 0,1; b at 2. For a: positions 0,1. Compute j=1: (0+1)=1->a, (1+1)=2->b => letters a,b distinct => uniq=2 => max_uniq=2. b: m=1 =>1. Sum=3, probability=1. Works.
Now consider s = "abab". n=4, a at 0,2; b at 1,3. For a: positions 0,2. j=1: (0+1)=1->b, (2+1)=3->b => both b => uniq=0. j=2: (0+2)=2->a, (2+2)=0->a => both a => 0. j=3: (0+3)=3->b, (2+3)=1->b => both b => 0. So for a, max_uniq=0. For b: positions 1,3. j=1: (1+1)=2->a, (3+1)=0->a => both a =>0; j=2: (1+2)=3->b, (3+2)=1->b =>0; j=3: (1+3)=0->a, (3+3)=2->a =>0. So max_uniq=0 for both. Total sum=0, probability=0. Indeed, string "abab" has period 2. Cyclic shifts: "abab", "baba", "abab", "baba". So two distinct strings. First letters: a for k=0,2; b for k=1,3. If first letter a, possible k=0,2. Then any second letter? For k=0, t="abab": second letter b; for k=2, t="abab": also second letter b. So they are same; can't distinguish. So probability 0. So correct.
Now, is there any scenario where j = n/2 etc? fine.
Now, is there any scenario where the optimal j for a character is not the one that maximizes the count of unique positions, because Vasya could also use the knowledge of the second letter to decide his guess? But we already considered that: after seeing b, he will know the set of possible k; if that set has size 1, he wins. So maximizing the number of k for which the set size becomes 1 is indeed maximizing probability.
Thus algorithm is correct.
Now we should think about potential pitfalls with modulo indexing: We use i+j and if >= n subtract n. This works because i+j < 2n, so subtracting once suffices.
But note: For j from 1 to n-1, i+j can be exactly n? If i=0 and j=n, not possible because j max n-1; i+j maximum = (n-1)+(n-1) = 2n-2. For i+j >= n and < 2n, we subtract n. That gives index in [0, n-1]. Correct.
Now, we also need to ensure that we only consider j from 1 to n-1 inclusive. j = 0 not allowed because that would be the first letter again. Could j be equal to n? Not, since positions are 1..len(s)-1? Actually string length l, positions are from 1 to l? In problem, Vasya can open "one more letter on some position". It might be any position, maybe including the first again? Typically it's another letter, but even if allowed, it wouldn't help. So ignore.
Now, we must compute the probability. Implementation details: Since n up to 5000, loops are fine.
But we can potentially optimize further by precomputing for each offset j the mapping of indices? Actually, for each j, we could compute an array shift_j where shift_j[i] = s[(i+j)%n]. That would be an n-length array for each j, total n^2 memory. That might be faster because we avoid repeated modulo calculation? But memory ~ 25 million integers, each Python int overhead huge, not feasible.
Better to compute on the fly.
We can also try to reduce work by noting that for each character, the positions list is small for many characters; only the large ones matter. But worst-case is all same character: m=n, then we do about 2n*(n-1) operations = about 50 million. Python can handle 50 million simple operations in about 2 seconds maybe? Let's approximate: 50 million integer additions, if statements, list accesses. Might be okay with PyPy or CPython? Usually Codeforces time limit 2 seconds. Python might be borderline but maybe okay with PyPy. We can further micro-optimize.
We can precompute for each i the string of letters for all j? Not needed.
We can also note that for m=n (all same character), all cnt values will be n for that letter (since all positions map to same letter). So uniq will be 0 always, and we can skip computing? But we still need to confirm max_uniq=0. However, we can quickly detect that if all letters are same, then for any a, the whole string is that a. For that single character (the only one), K_a = all positions. For any j, all s[(i+j)%n] = a (since all letters a), so cnt[a] = n, uniq=0. So max_uniq=0. So we can early exit: If len(set(s))==1, answer=0.0. But that's a special case.
For strings with many repeated characters but not all same, we still need to compute. But m can be up to 5000 for a single character. So worst-case runtime is when there is one character with m close to n, and maybe a few others with small m. So total operations ~2 * n * (n-1) ~ 50 million for n=5000. That's okay.
We need to be careful with Python loops overhead: The innermost loops are over pos (which could be 5000) for each j (4999). That's about 25 million iterations per pass (first and second pass). But we have two passes, so 50 million iterations. Each iteration does a few operations. In Python, 50 million loops may be around 1-2 seconds in PyPy? Actually PyPy can handle about 100 million simple operations per second? Not exactly; typically 100 million might be ~2-3 seconds. I'd like to optimize.
We can combine the two passes into one by storing the b values in a temporary list, then after counting we can count uniq. That's still two loops but we could compute uniq in