×

# HP18 - EDITORIAL

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

Simple

# PREREQUISITES:

Game-Theory and Basic Math would do.

# PROBLEM:

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.

# SUPER QUICK EXPLANATION

• 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.

# EXPLANATION

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.

# AUTHOR'S AND TESTER'S SOLUTIONS:

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

This question is marked "community wiki".

4.0k31104
accept rate: 22%

19.8k350498541

 0 @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 2★magic105 0 accept rate: 0% Talked to admin. Links now working, refresh the page. (14 Jan, 20:07)
 0 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 2★sk22 0 accept rate: 0%
 0 can somebody help me. what is wrong in my solution https://www.codechef.com/viewsolution/22179724 answered 05 Feb, 12:30 1 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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:

×1,220
×1,191
×726
×323
×112
×8