ALTERADD - Editorial


Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4

Setter: Utkarsh Gupta
Tester: Takuki Kurokawa, Lavish Gupta
Editorialist: Yash Kulkarni




Basic Math


Chef has 2 numbers A and B (A \lt B).

Chef will perform some operations on A.

In the i^{th} operation:

  • Chef will add 1 to A if i is odd.
  • Chef will add 2 to A if i is even.

Chef can stop at any instant. Can Chef make A equal to B?


It is clear that after a pair of operations (one for odd i and one for even i) A increases by 3. Hence, after an even number (2K) of operations, A changes to A + 3K, where K is any whole number.
Considering the above fact, after an odd number (2K + 1) of operations, A takes the form A + 3K + 1, where K is any whole number.
Therefore, A can be changed to take 2 forms, A + 3K or A + 3K + 1.
If we want A to change to B then B should have either of the 2 forms, A + 3K or A + 3K + 1. In other words, (B - A) (mod 3) should either be 0 or 1.


O(1) for each test case.


Setter's solution
Editorialist's Solution
#include <iostream>
using namespace std;

int main() {
	int T;
	cin >> T;
	    int A,B;
	    cin >> A >> B;
	    if((B-A)%3<=1)cout << "YES" << endl;
	    else cout << "NO" << endl;
	return 0;