You are not logged in. Please login at to post your questions!




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. :)

This question is marked "community wiki".

asked 14 Jan, 15:58

taran_1407's gravatar image

accept rate: 22%

edited 14 Jan, 19:35

admin's gravatar image

0★admin ♦♦

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


answered 14 Jan, 20:03

magic105's gravatar image

accept rate: 0%

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

(14 Jan, 20:07) taran_14076★

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


answered 15 Jan, 02:14

sk22's gravatar image

accept rate: 0%

can somebody help me. what is wrong in my solution


answered 05 Feb, 12:30

rishabh9717's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 14 Jan, 15:58

question was seen: 1,588 times

last updated: 05 Feb, 12:30