/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Time Exceeded ≥5098ms ≥532.0 KiB
#3 Time Exceeded ≥5100ms ≥532.0 KiB

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>

using namespace std;

// Function to calculate GCD of two numbers
int gcd(int a, int b)
{
	while (b != 0)
	{
		int temp = b;
		b = a % b;
		a = temp;
	}
	return a;
}

// Function to calculate the GCD of a subset
int calculate_gcd(const vector<int> &subset)
{
	int result = subset[0];
	for (size_t i = 1; i < subset.size(); i++)
	{
		result = gcd(result, subset[i]);
	}
	return result;
}

// Function to generate all combinations of size k
void generate_combinations(const vector<int> &arr, int k, int start, vector<int> &current, set<int> &gcd_values)
{
	if (current.size() == k)
	{
		gcd_values.insert(calculate_gcd(current));
		return;
	}
	for (size_t i = start; i < arr.size(); i++)
	{
		current.push_back(arr[i]);
		generate_combinations(arr, k, i + 1, current, gcd_values);
		current.pop_back();
	}
}

// Main function to find the maximum GCD for each k
vector<int> max_gcd_of_combinations(int n, const vector<int> &arr)
{
	vector<int> result;
	for (int k = 1; k <= n; k++)
	{
		set<int> gcd_values;
		vector<int> current;
		generate_combinations(arr, k, 0, current, gcd_values);
		result.push_back(*max_element(gcd_values.begin(), gcd_values.end()));
	}
	return result;
}

int main()
{
	int n;
	cin >> n;
	vector<int> arr(n);
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}

	vector<int> result = max_gcd_of_combinations(n, arr);
	for (int val : result)
	{
		cout << val << " ";
	}
	cout << endl;

	return 0;
}

Information

Submit By
Type
Submission
Problem
P1151 Max gcd group
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 15:34:46
Judged At
2025-01-02 15:34:46
Judged By
Score
0
Total Time
≥5100ms
Peak Memory
≥532.0 KiB