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

×

HP18 - EDITORIAL

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2

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

DIFFICULTY:

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:

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

6★taran_1407
4.0k31104
accept rate: 22%

edited 14 Jan, 19:35

admin's gravatar image

0★admin ♦♦
19.8k350498541


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

link

answered 14 Jan, 20:03

magic105's gravatar image

2★magic105
0
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

link

answered 15 Jan, 02:14

sk22's gravatar image

2★sk22
0
accept rate: 0%

can somebody help me. what is wrong in my solution https://www.codechef.com/viewsolution/22179724

link

answered 05 Feb, 12:30

rishabh9717's gravatar image

2★rishabh9717
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

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

question asked: 14 Jan, 15:58

question was seen: 1,596 times

last updated: 05 Feb, 12:30