JGAMES - Editorial

PROBLEM LINK:

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

Setter: Janmansh Agarwal
Tester: Takuki Kurokawa
Editorialist: Kanhaiya Mohan

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

Janmansh and Jay are playing a game. They start with a number X and they play a total of Y moves. Janmansh plays the first move of the game.

In one move, a player can increment or decrement X by 1.

If the final number after performing Y moves is even, then Janmansh wins otherwise, Jay wins. Determine the winner of the game if both the players play optimally.

QUICK EXPLANATION:

  • If Y is odd, the parity of X would change after Y moves.
  • After Y moves, if X is even, print Janmansh. Else, print Jay.

EXPLANATION:

Observation 1

After each move, parity of the number changes. How?

Suppose X is odd. This means that both (X+1) and (X-1) are even.
Similarly, if X is even, (X+1) and (X-1) are odd.

Thus, irrespective of the type of move, parity of the number changes after each move.

Observation 2

After applying Y' moves on X, let the updated value be Z.

  • If Y' is even, X and Z have the same parity.
  • If Y' is odd, X and Z have different parity.

From observation 1, we know that each move changes the parity of the number. Thus, if we apply an even number of moves, we get the same parity as the initial number.

Similarly, if we apply odd number of moves, the initial and final numbers have different parities.

Solution

X is odd: If the value of Y is odd, after applying Y moves, X becomes even. Otherwise, X remains odd.

X is even: If the value of Y is odd, after applying Y moves, X becomes odd. Otherwise, X remains even.

To sum up, if the value of Y is odd, we change the parity of X. We check whether the final number is odd or even and decide the winner of the game.

TIME COMPLEXITY:

The time complexity is O(1) per test case.

SOLUTION:

Tester's Solution
#include <bits/stdc++.h>
using namespace std;

int main() {
    int tt;
    cin >> tt;
    while (tt--) {
        int x, y;
        cin >> x >> y;
        if (x % 2 == 0) {
            if (y % 2 == 0) {
                cout << "Janmansh" << endl;
            } else {
                cout << "Jay" << endl;
            }
        } else {
            if (y % 2 == 0) {
                cout << "Jay" << endl;
            } else {
                cout << "Janmansh" << endl;
            }
        }
    }
    return 0;
}
Editorialist's Solution
#include <iostream>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
	    int x, y;
	    cin>>x>>y;
	    
	    if(y%2) x++;
	    
	    if(x%2) cout<<"jAy";
        else cout<<"JaNmAnSh";
        cout<<endl;
	}
	return 0;
}