To solve this problem, find a prime number greater than \(10^9\). If the input contains 1, then there is no solution.
#include <bits>
using namespace std;
typedef long long ll;
void process() {
int size;
cin >> size;
bool valid = true;
vector<int> data(size);
for (int i = 0; i < size; ++i) {
cin >> data[i];
if (data[i] == 1) valid = false;
}
if (!valid) {
cout << "-1\n";
return;
}
cout << "1000000007\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int cases;
cin >> cases;
while (cases--) {
process();
}
return 0;
}
</int></bits>
Problem B - Unbroken Blade
This problem requires determining if a given tree can form a Hamiltonian path. This is possible only if the tree forms a chain, so check node degrees.
#include <bits>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int nodes;
cin >> nodes;
vector<int> degree(nodes + 1, 0);
for (int i = 1; i < nodes; ++i) {
int x, y;
cin >> x >> y;
degree[x]++;
degree[y]++;
if (degree[x] > 2 || degree[y] > 2) {
cout << "-1\n";
return 0;
}
}
vector<int> terminals;
for (int i = 1; i <= nodes; ++i)
if (degree[i] == 1) terminals.push_back(i);
cout << terminals[0] << " " << terminals.back() << "\n";
return 0;
}
</int></int></bits>
Problem C - Diligent Shift
The strategy involves moving all '1's to the top-left corner by first pushing them upwards and then leftwards, followed by adjusting any remaining '1's towards the center.
#include <bits>
using namespace std;
int main() {
int test_cases = 1;
cin >> test_cases;
while (test_cases--) {
// Implementation details omitted for brevity
}
return 0;
}
</bits>
Problem D - Twin Decision
Check if the array elements can be divided into exactly two disstinct sets of equal sizes.
#include <bits>
using namespace std;
typedef long long ll;
void evaluate() {
int length;
cin >> length;
set<int> unique_vals;
vector<int> arr(length);
for (int &val : arr) {
cin >> val;
unique_vals.insert(val);
}
if ((length % 2) != 0 || unique_vals.size() != 2) {
cout << "No\n";
return;
}
int count_first = count(arr.begin(), arr.end(), *unique_vals.begin());
int count_second = count(arr.begin(), arr.end(), *next(unique_vals.begin()));
cout << (count_first == count_second ? "Yes\n" : "No\n");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tests;
cin >> tests;
while (tests--) evaluate();
return 0;
}
</int></int></bits>
Problem E - Twin Mistake
Calculate the minimal operations needed to convert half the array to one value and the other half to another, considering the median for optimal transformation.
#include <bits>
using namespace std;
int main() {
int scenarios = 1;
cin >> scenarios;
while (scenarios--) {
// Simplified implementation based on original concept
}
return 0;
}
</bits>
Problem G - Balanced Order
Insure the sum of elemments equals the sum of indices from 1 to n. Then, sort the array and calculate the required adjustments.
#include <bits>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int elements;
cin >> elements;
ll total_sum = 0;
vector<int> values(elements + 1);
for (int i = 1; i <= elements; ++i) {
cin >> values[i];
total_sum += values[i];
}
if (total_sum != (ll)elements * (elements + 1) / 2) {
cout << "-1\n";
return 0;
}
sort(values.begin() + 1, values.end());
ll correction = 0;
for (int i = 1; i <= elements; ++i)
if (values[i] < i) correction += i - values[i];
cout << correction << "\n";
return 0;
}
</int></bits>
Problem H - Window Order
Sort intervals by their right endpoints, then assign each interval a unique number ensuring it fits within its bounds.
#include <bits>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int intervals;
cin >> intervals;
set<int> available;
vector<array>> ranges(intervals);
for (int i = 0; i < intervals; ++i) {
cin >> ranges[i][0] >> ranges[i][1];
ranges[i][2] = i + 1;
available.insert(i + 1);
}
sort(ranges.begin(), ranges.end(), [](auto& a, auto& b) {
return a[1] == b[1] ? a[0] > b[0] : a[1] < b[1];
});
vector<int> results(intervals + 1);
for (auto &[start, end, idx] : ranges) {
auto pos = available.lower_bound(start);
if (pos != available.end() && *pos <= end) {
results[idx] = *pos;
available.erase(pos);
} else {
cout << "-1\n";
return 0;
}
}
for (int i = 1; i <= intervals; ++i)
cout << results[i] << (i < intervals ? " " : "\n");
return 0;
}
</int></array></int></bits>