/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 39ms 18.246 MiB
#2 Accepted 201ms 19.742 MiB
#3 Accepted 232ms 19.328 MiB
#4 Accepted 79ms 19.738 MiB
#5 Accepted 105ms 19.406 MiB
#6 Accepted 207ms 18.898 MiB
#7 Accepted 206ms 18.992 MiB
#8 Accepted 237ms 19.488 MiB
#9 Accepted 372ms 20.812 MiB

Code

from itertools import permutations
import math

MOD = 998244353
M = 2e5 + 10
MAX_N = 2000

# List of all permutations of 'abc'
all_permutations = []

# Prime numbers up to 2000
primes = []
# Prime presence for numbers up to 2000
is_prime = [0] * (MAX_N + 1)

def precal():
    # Generate all permutations of "abc"
    s = "abc"
    all_permutations.append(s)
    for perm in permutations(s):
        all_permutations.append(''.join(perm))
    
    # Sieve of Eratosthenes to find primes up to MAX_N
    is_prime[0] = 0
    is_prime[1] = 1
    primes.append(0);
    for i in range(2, MAX_N + 1):
        if is_prime[i] == 0:
            primes.append(i)
            for j in range(i * 2, MAX_N + 1, i):
                is_prime[j] = 1

def main():
    # Read number of test cases
    t = int(input())
    
    # Precompute necessary data
    precal()

    # Process each test case
    for _ in range(t):
        # Read n and the string s
        n = int(input())
        s = input().strip()

        # Frequency count of 'a', 'b', 'c' in the string
        fre = [0] * 3
        for char in s:
            fre[ord(char) - ord('a')] += 1

        # Initialize the result with a large value
        mx = M

        # Iterate over all permutations of "abc"
        for perm in all_permutations:
            # For each prime pair (start, end) where start + end <= n
            for start in primes:
                if start > n:
                    break
                for end in primes:
                    if start + end > n:
                        break
                    baki = n - (start + end)
                    if  is_prime[baki]==1:
                        continue
                    
                    # Frequencies of characters in the current permutation
                    a = fre[ord(perm[0]) - ord('a')]
                    b = fre[ord(perm[1]) - ord('a')]
                    c = fre[ord(perm[2]) - ord('a')]

                    pos = 0
                    neg = 0

                    # Calculate how much we need to add or remove to match the prime requirements
                    if a > start:
                        pos += a - start
                    if a < start:
                        neg += start - a
                    if b > end:
                        pos += b - end
                    if b < end:
                        neg += end - b
                    if c > baki:
                        pos += c - baki
                    if c < baki:
                        neg += baki - c
                    
                    # Minimize the result
                    mx = min(mx, min(pos, neg))

        # If no valid answer found, return -1
        if mx == M:
            mx = -1
        
        # Output the result for the current test case
        print(mx)

# Execute the main function
if __name__ == "__main__":
    main()

Information

Submit By
Type
Submission
Problem
P1158 Yet another Beautiful String
Language
PyPy 3 (Python 3.9.18 PyPy 7.3.15)
Submit At
2025-01-02 09:23:59
Judged At
2025-01-02 09:36:48
Judged By
Score
100
Total Time
372ms
Peak Memory
20.812 MiB