CSP-J2019 Jiangxi Province Problem Solutions
This problem requires comparing the area of a square with side length a against the area of a rectangle with sides b and c. The solution involves straightforward calculation with attention to equal area cases.
#include<iostream>
using namespace std;
int main() {
long long side, width, height;
cin >> side >> width >> height;
if(side * side > width * height) {
cout << "Alice";
} else {
cout << "Bob";
}
return 0;
}
Problem 2: Second Largest Modulo Value
Finding the second largest value from modulo operations between array elements. The approach involves processing largest elements and computing thier modulo results.
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
int main() {
int count;
cin >> count;
vector<int> numbers;
vector<int> results;
map<int, bool> seen;
for(int i = 0; i < count; i++) {
int value;
cin >> value;
if(!seen[value]) {
numbers.push_back(value);
seen[value] = true;
} else {
results.push_back(0);
}
}
sort(numbers.begin(), numbers.end());
int limit = max(0, (int)numbers.size() - 30);
for(int i = numbers.size() - 1; i >= limit; i--) {
for(int j = 0; j < numbers.size(); j++) {
if(i != j) {
results.push_back(numbers[j] % numbers[i]);
}
}
}
if(results.size() == 1) {
cout << -1;
return 0;
}
sort(results.begin(), results.end());
for(int i = results.size() - 2; i >= 0; i--) {
if(results[i + 1] != results[i]) {
cout << results[i];
return 0;
}
}
cout << -1;
return 0;
}
Problem 3: Road Demolition
This graph problem involves finding optimal paths from two sources to a common meeting point, then to the destination. The solution uses BFS to compute distances from each key point.
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int MAX_NODES = 3005;
struct PathNode {
int vertex, distance;
};
vector<int> graph[MAX_NODES];
int dist_from_start[MAX_NODES];
int dist_from_first[MAX_NODES];
int dist_from_second[MAX_NODES];
int node_count, edge_count;
int first_source, first_limit, second_source, second_limit;
bool visited[MAX_NODES];
queue<PathNode> traversal_queue;
void compute_start_distances() {
traversal_queue.push({1, 0});
visited[1] = true;
while(!traversal_queue.empty()) {
int current = traversal_queue.front().vertex;
int steps = traversal_queue.front().distance;
traversal_queue.pop();
dist_from_start[current] = steps;
for(int neighbor : graph[current]) {
if(!visited[neighbor]) {
visited[neighbor] = true;
traversal_queue.push({neighbor, steps + 1});
}
}
}
}
void compute_first_distances() {
memset(visited, 0, sizeof(visited));
traversal_queue.push({first_source, 0});
visited[first_source] = true;
while(!traversal_queue.empty()) {
int current = traversal_queue.front().vertex;
int steps = traversal_queue.front().distance;
traversal_queue.pop();
dist_from_first[current] = steps;
for(int neighbor : graph[current]) {
if(!visited[neighbor]) {
visited[neighbor] = true;
traversal_queue.push({neighbor, steps + 1});
}
}
}
}
void compute_second_distances() {
memset(visited, 0, sizeof(visited));
traversal_queue.push({second_source, 0});
visited[second_source] = true;
while(!traversal_queue.empty()) {
int current = traversal_queue.front().vertex;
int steps = traversal_queue.front().distance;
traversal_queue.pop();
dist_from_second[current] = steps;
for(int neighbor : graph[current]) {
if(!visited[neighbor]) {
visited[neighbor] = true;
traversal_queue.push({neighbor, steps + 1});
}
}
}
}
int main() {
cin >> node_count >> edge_count;
for(int i = 1; i <= node_count; i++) {
dist_from_start[i] = dist_from_first[i] = dist_from_second[i] = MAX_NODES * 2;
}
for(int i = 0; i < edge_count; i++) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
cin >> first_source >> first_limit >> second_source >> second_limit;
compute_start_distances();
compute_first_distances();
compute_second_distances();
int result = -1;
for(int i = 1; i <= node_count; i++) {
int first_path = dist_from_start[i] + dist_from_first[i];
int second_path = dist_from_start[i] + dist_from_second[i];
if(first_path > first_limit || second_path > second_limit) continue;
if(result == -1) {
result = dist_from_first[i] + dist_from_second[i] + dist_from_start[i];
} else {
result = min(result, dist_from_first[i] + dist_from_second[i] + dist_from_start[i]);
}
}
if(result == -1) {
cout << -1;
} else {
cout << edge_count - result;
}
return 0;
}
Problem 4: Non-Palindromic Strings
Counting non-palindormic permutations by subtracting palindromic cases from total permutations. Uses modular arithmetic and multiplicative inverses.
#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int MAX_LEN = 2005;
ll character_count[26];
ll factorial[MAX_LEN];
void initialize_factorials() {
factorial[0] = 1;
for(int i = 1; i < MAX_LEN; i++) {
factorial[i] = factorial[i - 1] * i % MOD;
}
}
ll modular_power(ll base, ll exponent) {
ll result = 1;
while(exponent > 0) {
if(exponent & 1) {
result = result * base % MOD;
}
base = base * base % MOD;
exponent >>= 1;
}
return result;
}
int main() {
initialize_factorials();
int length;
string input;
cin >> length >> input;
for(char ch : input) {
character_count[ch - 'a']++;
}
ll total_permutations = factorial[length];
bool has_odd_length = (length % 2 == 0);
ll palindrome_factor = factorial[length / 2];
for(int i = 0; i < 26; i++) {
if(character_count[i] % 2 == 1) {
if(has_odd_length) {
cout << total_permutations;
return 0;
}
has_odd_length = true;
}
palindrome_factor = palindrome_factor * factorial[character_count[i]] % MOD;
ll inverse = modular_power(factorial[character_count[i] / 2], MOD - 2);
palindrome_factor = palindrome_factor * inverse % MOD;
}
ll non_palindromic = (total_permutations - palindrome_factor + MOD) % MOD;
cout << non_palindromic;
return 0;
}