Contest: Division 1
Contest: Division 2

Setter: Hruday Pabbisetty
Tester: Xiuhan Wang
Editorialist: Taranpreet Singh




Game-Theory and Basic Math would do.


Given an array A containing N elements, on which Bob and Alice are playing a game with Bob playing first, both of them having lucky numbers a and b respectively. In one move, a player can remove any number of elements from the array A which are divisible by their lucky number. The player unable to remove any element loses the game. Find winner of the game if both players play optimally.


  • If A contains elements which are divisible by both a and b, It is optimal for Bob to delete all such elements in the very first move. Turn goes to Alice.
  • Now, it is optimal for both players to remove exactly one number they can remove, until one of the players is unable to make a move, thus losing the game. The number of moves Bob can make is Number of elements divisible by a after deleting numbers divisible by both a and b. Same way for Alice.


First of all, let us classify the numbers present in the array into four categories.

  • Numbers divisible by both a and b.
  • Numbers divisible by a but not b.
  • Numbers divisible by b but not a.
  • Numbers not divisible by a or b.

Let Number of elements of the second type be the number of reserve moves of Bob and Number of numbers of the third type be Number of reserve moves of Alice.

Since Bob is having the first move, it is ideal for Bob to remove all elements of the first type in the first move itself to force Alice to use reserve move in next move, if the array contains any number of the first type. This is because if after Bob’s move, if there are any number of the first type present in the array, Alice can remove all of them, forcing Bob to use his reserve move in next move.

Now, the player having lesser reserve move loses since that player shall run out of moves. In case both players had the same number of reserve moves, the player first to move shall lose (After removing numbers of the first type).

It can be easily implemented by making two counter variables counting reserve moves of each player, and a flag determining whether there are any numbers of the first type in the array.

Time Complexity

Time complexity is O(N) per test case.


Setter’s solution
Tester’s solution
Editorialist’s solution

Feel free to Share your approach, If it differs. Suggestions are always welcomed. :slight_smile:

@admin This problem also do not have access to view! Please make all problem solutions of January long challenge public.

Talked to admin. Links now working, refresh the page.

This part of the editorial was very confusing: “In case both players had the same number of reserve moves, the player first to move shall lose (After removing numbers of the first type).”. But thanks for the editorial. my solution

can somebody help me.
what is wrong in my solution