C - チーム分けくじ引き / Team Assignment Lottery 解説 by admin
GPT 5.2 HighOverview
This problem asks us to find, as an irreducible fraction, the probability that a randomly drawn opponent has a different rank from Takahashi (Member 1).
Analysis
The lottery selects one person from members \(2 \sim N\), each with equal probability \(\frac{1}{N-1}\).
Therefore, the probability that “a pair is formed (= ranks differ)” is the ratio of:
- Numerator: the number of people with a different rank from Takahashi among members \(2 \sim N\)
- Denominator: the number of candidates, \(N-1\)
In other words, the answer is [ \frac{#{i \in [2,N] : S_i \neq S_1}}{N-1} ]
If we naively do something like S = input().split() to store all members’ strings in a list, the input can be large with \(N \le 10^6\), and in Python this tends to increase memory usage and slow things down (risk of TLE/memory issues).
Instead, we read the input as a byte string and only count A/B characters, avoiding unnecessary string creation for a fast, memory-efficient solution.
Also, since the output must be an irreducible fraction, we need to reduce using \(\gcd\).
When the probability is \(0\) or \(1\), it must be output as 0/1 or 1/1 respectively, so we handle those cases with branching.
Algorithm
- Read \(N\).
- Read the remaining input as a byte string and extract only the characters
AorBin order.- Record the first rank encountered as \(S_1\) (Takahashi’s rank).
- For the 2nd onward (members 2 and beyond), count the number of people with the same rank as \(S_1\) as
same.
- The number of people with a different rank is
diff = (N-1) - same. - The probability is \(\frac{\text{diff}}{N-1}\).
- If
diff == 0, output0/1. - If
diff == N-1, output1/1. - Otherwise, compute \(g=\gcd(\text{diff}, N-1)\) and output
diff/gand(N-1)/g.
- If
Example: When \(N=5\) and the ranks are A B A B B
Takahashi is A, and members 2–5 are B A B B.
There are 3 people with a different rank (B), so the probability is \(\frac{3}{4}\).
Complexity
- Time complexity: \(O(N)\) (just counting \(N\) occurrences of
A/B) - Space complexity: \(O(1)\) (only a constant number of variables such as person counts)
Implementation Notes
Since the input size is large, instead of creating a huge list with
split(), we read everything at once withsys.stdin.buffer.read()and count onlyA/Bfrom the byte string for speed.We use
math.gcdto reduce to an irreducible fraction.For the cases
diff=0(never forms a pair) anddiff=N-1(always forms a pair), we output0/1and1/1respectively as specified.Source Code
import sys
import math
def main():
buf = sys.stdin.buffer
N_line = buf.readline()
while N_line.strip() == b"":
N_line = buf.readline()
N = int(N_line)
rest = buf.read()
s1 = None
same = 0
cnt = 0 # number of ranks read
for b in rest:
if b == 65 or b == 66: # 'A' or 'B'
cnt += 1
if cnt == 1:
s1 = b
else:
if b == s1:
same += 1
if cnt == N:
break
diff = (N - 1) - same
if diff == 0:
print("0/1")
return
if diff == N - 1:
print("1/1")
return
g = math.gcd(diff, N - 1)
print(f"{diff // g}/{(N - 1) // g}")
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.