help me to understand the proof of 2137B
Difference between en2 and en3, changed 7 character(s)
**Hello everyone,**↵

I’m currently stuck on a Codeforces problem — [Problem 2137B](https://mirror.codeforces.com/problemset/problem/2137/B). While reading the [official editorial](https://mirror.codeforces.com/blog/entry/146121), 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:↵
<math xmlns="http://www.w3.org/1998/Math/MathML">↵
  <mi>G</mi>↵
  <mi>C</mi>↵
  <mi>D</mi>↵
  <mo stretchy="false">(</mo>↵
  <msub>↵
    <mi>p</mi>↵
    <mi>i</mi>↵
  </msub>↵
  <mo>+</mo>↵
  <msub>↵
    <mi>q</mi>↵
    <mi>i</mi>↵
  </msub>↵
  <mo>,</mo>↵
  <msub>↵
    <mi>p</mi>↵
    <mrow class="MJX-TeXAtom-ORD">↵
      <mi>i</mi>↵
      <mo>+</mo>↵
      <mn>1</mn>↵
    </mrow>↵
  </msub>↵
  <mo>+</mo>↵
  <msub>↵
    <mi>q</mi>↵
    <mrow class="MJX-TeXAtom-ORD">↵
      <mi>i</mi>↵
      <mo>+</mo>↵
      <mn>1</mn>↵
    </mrow>↵
  </msub>↵
  <mo stretchy="false">)</mo>↵
  <mo>&#x2265;<!-- ≥ --></mo>↵
  <mn>3</mn>↵
</math>↵

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 
codelogic
#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! ↵




History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English ahnaf09 2025-10-07 08:04:24 7 Tiny change: 'on of the code\n#include' -> 'on of the logic\n#include'
en2 English ahnaf09 2025-10-07 08:04:03 516
en1 English ahnaf09 2025-10-07 08:00:44 1673 Initial revision (published)