/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Wrong Answer 1ms 340.0 KiB
#3 Wrong Answer 2ms 320.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;

// Function to compute GCD of two numbers
int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}

// Function to compute GCD of an array
int array_gcd(vector<int> &arr)
{
    int result = arr[0];
    for (int i = 1; i < arr.size(); i++)
    {
        result = gcd(result, arr[i]);
    }
    return result;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<int> A(n);
        for (int i = 0; i < n; i++)
        {
            cin >> A[i];
        }

        // Separate odd and even indexed elements
        vector<int> odd_elements, even_elements;
        for (int i = 0; i < n; i++)
        {
            if (i % 2 == 0)
            {
                even_elements.push_back(A[i]);
            }
            else
            {
                odd_elements.push_back(A[i]);
            }
        }

        // Compute initial GCDs
        int odd_gcd = array_gcd(odd_elements);
        int even_gcd = array_gcd(even_elements);

        // Initial maximum sum of GCDs
        int max_sum = odd_gcd + even_gcd;

        // Try to optimize by swapping elements
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                swap(A[i], A[j]);

                // Recompute GCDs after swapping
                odd_elements.clear();
                even_elements.clear();
                for (int k = 0; k < n; k++)
                {
                    if (k % 2 == 0)
                    {
                        even_elements.push_back(A[k]);
                    }
                    else
                    {
                        odd_elements.push_back(A[k]);
                    }
                }

                int new_odd_gcd = array_gcd(odd_elements);
                int new_even_gcd = array_gcd(even_elements);
                int new_sum = new_odd_gcd + new_even_gcd;

                // Update maximum sum if new sum is greater
                if (new_sum > max_sum)
                {
                    max_sum = new_sum;
                }

                // Swap back to original configuration
                swap(A[i], A[j]);
            }
        }

        // Output the maximum possible sum of GCDs
        cout << max_sum << endl;
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1076 Even Odd GCD (Easy Version)
Language
C++17 (G++ 13.2.0)
Submit At
2024-09-06 12:37:19
Judged At
2024-11-11 02:56:19
Judged By
Score
1
Total Time
2ms
Peak Memory
340.0 KiB