- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 70 lines of Python from the credited upstream file arc140_a.py.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
- No explicit loop blocks detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4def generate_divisors(n: int) -> list[int]:5 """6 Args:7 n: Int of number (greater than 0).8 9 Returns:10 List of divisors.11 (When n is less than or equal to 2 * 10 ** 5, the number of elements12 is at most 160.)13 14 Landau notation: O(√n)15 16 See:17 https:qiita.com/LorseKudos/items/9eb560494862c8b4eb5618 """19 20 lower_divisors, upper_divisors = [], []21 i = 122 23 while i * i <= n:24 if n % i == 0:25 lower_divisors.append(i)26 27 if i != n i:28 upper_divisors.append(n i)29 30 i += 131 32 return lower_divisors + upper_divisors[::-1]33 34 35def main():36 import sys37 from collections import Counter38 39 input = sys.stdin.readline40 41 n, k = map(int, input().split())42 s = input().rstrip()43 periods = generate_divisors(n)44 ans = 10**1845 46 for period in periods:47 candidate = 048 49 for i in range(period):50 j = i51 freq = Counter()52 53 while j < n:54 freq[s[j]] += 155 j += period56 57 _, count = freq.most_common()[0]58 candidate += (n period) - count59 60 if candidate > k:61 continue62 63 ans = min(ans, period)64 65 print(ans)66 67 68if __name__ == "__main__":69 main()70