Search code examples
mathfunctional-programmingbrute-forcenumber-theory

Asking ideas of a codeforces problem (Problem-483A Counterexample)


Problem link: http://codeforces.com/problemset/problem/483/A

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. The single line contains two positive space-separated integers l, r (1 ≤ l ≤ r ≤ 10^(18); r - l ≤ 50).

I want to know ideas of solving this problem.

Edit:

Here is my attempt:

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    int l, r, i, c;
    cin >> l >> r;
    c = r - l;

    if(c <= 50)
    {
        if(c >= 2)
        {
            for(i = l; i <= r; i++)
            {
                if(i % 2 == 0)
                {
                    cout << i << " " << i+1 << " " << i+2 << endl;
                    break;
                }
            }
        }
        else if(c <= 1)
        {
            cout << "-1" << endl;
        }
    }
}

What is wrong with my code? The error message on the site is

Probably, the solution is executed with error 'uninitiaized value usage' on the line 10

Solution

  • If l and r have distance less than 2, then no such triplets exist since we want them to be all distinct.

    If their distance is 2, the only possibility is if the choice are l, l+1, l+2. If l is even, then it is a valid counter example. However, if l is odd, then there is no such counter example, since l and l+1 are coprime, l+1 and l+2 must be coprime, but l and l+2 can't have 2 as a common factor since they are odd numbers.

    If the distance is greater 2, just pick a to be the smallest even number at least as big as l, and return (a, a+1, a+2). a and a+2 have common factor 2.

    Edit after attempt is shown, here is a possible C++ code:

    #include<iostream>
    #include<cstdlib>
    using namespace std;
    int main()
    {
        long long l,r,i,c, first;
        cin >> l >> r;
        c=r-l;
        if ((c<2) || (c==2) && (l%2 == 1)){
            cout << "-1" << endl;
        }
        else{
            first = l + l % 2;
            cout << first << " " <<first+1 << " " << first+2 << endl;
        }
    }