Tired of understanding this

As the title says, my head got blown up by this problem and just on a brain fart-mode right now, I tried using a gcd(greatest common divisor) function , pairs, recursion and many other solutions to solve this problem i got really close to solving it but there are still alot of input samples that are just obstacles so i gave up and decided to see a solution and idk if its cause of the brain fart mode im in right now or idk but I can't seem to understand the maths/concept behind the 3 if conditions in the solution, anyways, here is the problem & the solution:

Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one.

Your friend often comes up with different statements. He has recently supposed that if the pair (a, b) is coprime and the pair (b, c) is coprime, then the pair (a, c) is coprime.

You want to find a counterexample for your friend's statement. Therefore, your task is to find three distinct numbers (a, b, c), for which the statement is false, and the numbers meet the condition l ≤ a < b < c ≤ r.

More specifically, you need to find three numbers (a, b, c), such that l ≤ a < b < c ≤ r, pairs (a, b) and (b, c) are coprime, and pair (a, c) is not coprime.

Input
The single line contains two positive space-separated integers l, r (1 ≤ l ≤ r ≤ 1018; r - l ≤ 50).

Output
Print three positive space-separated integers a, b, c — three distinct numbers (a, b, c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order.

If the counterexample does not exist, print the single number -1.

Examples
input
2 4
output
2 3 4
input
10 11
output
-1
input
900000000000000009 900000000000000029
output
900000000000000009 900000000000000010 900000000000000021
Note
In the first sample pair (2, 4) is not coprime and pairs (2, 3) and (3, 4) are.

In the second sample you cannot form a group of three distinct integers, so the answer is -1.

In the third sample it is easy to see that numbers 900000000000000009 and 900000000000000021 are divisible by three.


Accepted solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>


#define fl(n)    for(int i = 0; i < n; i++)

#define Max(a,b) ( (a) > (b) ? (a) : (b))
#define Min(a,b) ( (a) < (b) ? (a) : (b))

#define ll   long long
#define nl   endl
#define pb push_back
#define mp make_pair


#define EPS  1e-9
#define INF  1e9
#define init -1e9


using namespace std;

/*ll gcd(ll m, ll n) {
if (n == 0)
return m;
else
return gcd(n, m % n);
}*/

int main()
{
    ll l,r;
    cin >> l >> r;

    if(abs(l-r)+1 < 3) {
        cout << -1;
        return 0;
    }

    if(l % 2 == 0){
        cout << l << " " << l+1 << " " << l+2;
        return 0;
    }
    else if(abs(l-r) +1 > 3){
        cout << l+1 << " " << l+2 << " " << l+3;
        return 0;
    }
    cout << -1;


    return 0;
}


Thanks in advance!
Assume WMLOG that L < R (actually, that seems to be given in the question).

The first 'if' statement is, rather bizarrely, equivalent to
R - L < 2 or
or "R is at most 1 bigger than L".
Then there is no way of fitting a different number between L and R - the gap is too small; so output -1.

The second if condition says that L is even; L+2 must then be even as well.
a =L and b=L+1 must be coprime, since if they had any non-unit common factors, then their difference (=1), would also have to have the same non-trivial factor; this isn't possible. Similarly b=L+1 and c=L+2 must be coprime. However, a=L and c=L+2 are both even, so have a common factor of 2. This is your counterexample.

The third if statement can only be reached if L is odd. So a=L+1 is even, and b=L+2, c=L+3 lie within the acceptable difference between L and R. We then have a case similar to the above.

Line 47 can only be reached if L is odd and R=L+2. The only possible numbers would be a=L, b=L+1, c=L+2.
(a,b) must be coprime since they differ by 1 (similar to above).
(b,c) are coprime likewise.
c - a = 2, so the only possible common factor would be 2. However, a is odd, so not possible.


The program produces a different counterexample to the (large) one you have quoted. (You can see that the two cited numbers are both divisible by three because each has a sum of digits which is divisible by 3.)

Fundamentally, if two numbers have a common factor then so does their difference, and that, together with even/odd, is what the program is exploiting.
Topic archived. No new replies allowed.