Hello everyone,
I’m currently stuck on a Codeforces problem — Problem 2137B. While reading the official editorial, I got confused about one particular transformation step:
Each element is replaced by n + 1 - p[i].
I understand the use of n + 1, since we’re working with adjacent elements and need a length of n + 1. However, I don’t quite understand why we subtract p[i].
Also, I noticed that the editorial mentions a condition like: G C D ( p i + q i , p i + 1 + q i + 1 ) ≥ 3
I’m wondering if this relates to the property gcd(a, b) = gcd(a - b, b) where (a > b). Is that property being applied here?
And finally, does the condition hold simply because (n > 3) (since (n + 1 > 3)) — or is there a deeper reason behind it?
// C++ Version of the logic
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) b[i] = n - a[i] + 1; // ??
for (int i = 0; i < n; i++) cout << b[i] << " ";
cout << endl;
}
return 0;
}
I’d really appreciate it if someone could explain this step more clearly. I’m still a beginner, so a simple explanation would be very helpful.
Thank you in advance!
Полный текст и комментарии »