/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 34ms 17.371 MiB
#2 Wrong Answer 34ms 17.246 MiB

Code

import math

def min_operations_to_make_gcd(A, X, N):
    total_operations = 0
    for i in range(N):
        if math.gcd(A[i], X) > 1:
            continue  # No operations needed, this element is already valid
        # If gcd(A[i], X) is not valid, check how many increments are needed
        diff = X - A[i] % X
        total_operations += diff if diff != X else 0  # Only add the remainder to make it divisible
    return total_operations

def solve():
    # Read the number of test cases
    T = int(input())
    
    for _ in range(T):
        # Read the test case input values
        N, X = map(int, input().split())
        A = list(map(int, input().split()))
        
        # Split the array into two halves
        half_size = N // 2
        
        # First half should be (X+1)-ordinary
        first_half = A[:half_size]
        # Second half should be X-ordinary
        second_half = A[half_size:]
        
        # Check if we can make the first half (X+1)-ordinary
        first_half_operations = min_operations_to_make_gcd(first_half, X + 1, half_size)
        
        # Check if we can make the second half X-ordinary
        second_half_operations = min_operations_to_make_gcd(second_half, X, N - half_size)
        
        # If we can make both halves ordinary, return the total number of operations
        if first_half_operations != -1 and second_half_operations != -1:
            print(first_half_operations + second_half_operations)
        else:
            print(-1)

Information

Submit By
Type
Submission
Problem
P1168 F. x ordinary array
Language
PyPy 3 (Python 3.9.18 PyPy 7.3.15)
Submit At
2025-06-11 21:23:56
Judged At
2025-06-11 21:23:56
Judged By
Score
0
Total Time
34ms
Peak Memory
17.371 MiB