# ADDMULT - Editorial

**URL:** <https://discuss.codechef.com/t/addmult-editorial/13990>\
**Category:** editorial\
**Tags:** ad-hoc, addmult, cook79, editorial, game-theory, medium\
**Created:** [February 19, 2017, 10:02pm UTC](https://discuss.codechef.com/t/addmult-editorial/13990 "2017-02-19T22:02:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingofnumbers](https://discuss.codechef.com/user_avatar/discuss.codechef.com/kingofnumbers/32/3653_2.png) [@kingofnumbers](https://discuss.codechef.com/u/kingofnumbers)\
**Post date:** [February 19, 2017, 10:02pm UTC](https://discuss.codechef.com/t/addmult-editorial/13990/1 "2017-02-19T22:02:18Z")

</div>

### PROBLEM LINK:

[Contest](http://www.codechef.com/COOK79/problems/ADDMULT)  
[Practice](http://www.codechef.com/problems/ADDMULT)

**Author:** [Hasan Jaddouh](http://www.codechef.com/users/kingofnumbers)  
**Testers:** [Kamil Debowski](http://www.codechef.com/users/errichto)  
**Editorialist:** [Hasan Jaddouh](http://www.codechef.com/users/kingofnumbers)

# Difficulty:

Medium

# Prerequisites:

ad-hoc, game theory

# Problem Statement:

Two players are playing a game, there is a sequence of **N** integers, at one turn the player choose two adjacent numbers and remove them and put in their place either their sum or their product, game ends when there’s only one number left, if it was odd then one of players win if it’s even the other win, you will also given which player will play first

# Explanation:

since we only care about the parity of last number then we can change all even numbers in array to 0 and all odd numbers to 1, and when we sum two 1s the result will be 0.

for simplicity, we will denote the player who will win if last number was 0 by P0, and P1 for the other player.

First thing to notice that we have 4 possibilities for the status of array before the last move is made and they are (0, 0), (0, 1), (1, 0), (1, 1). we can observe that in all 4 cases we can make the result 0 so if the game started in way that P0 will make the last move then P0 will definitely win, after handling this case now we can assume that P1 will play that last move, that means we can assume P0 will play when array length is odd and P1 will play when array length is even.

now let **H** be maximum number of non-neighboring ones in the array, for now we will claim that P1 can play his moves such that he doesn’t decrease **H** , while P0 can play his moves to decrease **H** by at most 1 (i.e. he can’t decrease it by 2 or more), we will prove that later, but let’s assume this is correct for now.

P0 will win if and only if **H** became 0 in the end of the game, that means P1 should try not to decrease **H** while P0 should try to decrease it but since he can decrease it by 1 in each of his turns then this will lead us to conclude that P0 will win if and only if **H** at the beginning of the game is less or equal than number of turns that P0 will play. so to solve the problem we only need to calculate **H** which is trivial to calculate in O(N), then compare it with the number of turns that P0 will play.

## proof: P1 can always play such that H doesn’t decrease

if H \< N/2 where N is current length of the array then there must be at least one pair of adjacent elements that are not included in maximum non-consecutive ones, P1 can simply choose them and **H** will not decrease.

if H = N/2 then either the first element or the last element will not be part of the maximum non-consecutive ones (Remember N is even because it’s P1’s turn), if it was the first element then P1 should choose first two elements and replace them with 1, otherwise he should choose last two elements and replace them with 1, so in this case H will not decrease.

## proof: P0 can play so that to decrease H by at most 1

whatever two numbers P0 did choose at most one of them will be part of maximum set of non-neighboring ones, so if he removed them and replaced them with 0 then the same set (excluding deleted numbers) will work to be of non-neighboring ones of size **H-1**

# Author’s and Tester’s Solutions

[Setter](http://www.codechef.com/download/Solutions/COOK79/Setter/ADDMULT.cpp)  
[Tester](http://www.codechef.com/download/Solutions/COOK79/Tester/ADDMULT.cpp)

---

<div class="post-metadata">

**Author:** ![prayas\_sahni](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@prayas\_sahni](https://discuss.codechef.com/u/prayas_sahni)\
**Post date:** [February 20, 2017, 3:24pm UTC](https://discuss.codechef.com/t/addmult-editorial/13990/2 "2017-02-20T15:24:10Z")

</div>

if H=n/2 then how only one 0 is possible?  
In 1010 H=2 and zeros are 2.

---

<div class="post-metadata">

**Author:** ![shashank1209](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@shashank1209](https://discuss.codechef.com/u/shashank1209)\
**Post date:** [February 22, 2017, 5:38pm UTC](https://discuss.codechef.com/t/addmult-editorial/13990/3 "2017-02-22T17:38:06Z")

</div>

What is the issue with this code?

> **[Ideone.com](https://ideone.com/zqDhsZ)**
>
> Ideone is something more than a pastebin; it's an online compiler and debugging tool which allows to compile and run code online in more than 40 programming languages.

---

<div class="post-metadata">

**Author:** ![abcdef6199](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@abcdef6199](https://discuss.codechef.com/u/abcdef6199)\
**Post date:** [April 11, 2017, 4:26pm UTC](https://discuss.codechef.com/t/addmult-editorial/13990/4 "2017-04-11T16:26:01Z")

</div>

“if H = N/2 then either the first element or the last element will not be part of the maximum non-consecutive ones”.

I don’t understand this part. In 1001, H = 2 = 4 / 2 and both the 1st and 4th are part of the maximum non-consecutive ones".

Sorry if I misunderstood somewhere.

---

<div class="post-metadata">

**Author:** ![chan55555](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@chan55555](https://discuss.codechef.com/u/chan55555)\
**Post date:** [October 30, 2017, 10:35am UTC](https://discuss.codechef.com/t/addmult-editorial/13990/5 "2017-10-30T10:35:58Z")

</div>

Can anyone explain me the definition of the term “H” with an example, which is mentioned here.  
“let H be maximum number of non-neighboring ones in the array”

---

<div class="post-metadata">

**Author:** ![kingofnumbers](https://discuss.codechef.com/user_avatar/discuss.codechef.com/kingofnumbers/32/3653_2.png) [@kingofnumbers](https://discuss.codechef.com/u/kingofnumbers)\
**Post date:** [February 20, 2017, 3:29pm UTC](https://discuss.codechef.com/t/addmult-editorial/13990/6 "2017-02-20T15:29:40Z")

</div>

It’s fixed, thanks

---

<div class="post-metadata">

**Author:** ![prayas\_sahni](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@prayas\_sahni](https://discuss.codechef.com/u/prayas_sahni)\
**Post date:** [February 20, 2017, 4:00pm UTC](https://discuss.codechef.com/t/addmult-editorial/13990/7 "2017-02-20T16:00:10Z")

</div>

If the heuristic for P1 is to not let h=0 then p1 can also increase H when two adjacent 1’s are present.Could you please make it more clear

---

<div class="post-metadata">

**Author:** ![kingofnumbers](https://discuss.codechef.com/user_avatar/discuss.codechef.com/kingofnumbers/32/3653_2.png) [@kingofnumbers](https://discuss.codechef.com/u/kingofnumbers)\
**Post date:** [February 20, 2017, 5:40pm UTC](https://discuss.codechef.com/t/addmult-editorial/13990/8 "2017-02-20T17:40:08Z")

</div>

It’s not possible to increase H, can you give a counter-example?

---

<div class="post-metadata">

**Author:** ![prayas\_sahni](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@prayas\_sahni](https://discuss.codechef.com/u/prayas_sahni)\
**Post date:** [February 20, 2017, 8:39pm UTC](https://discuss.codechef.com/t/addmult-editorial/13990/9 "2017-02-20T20:39:31Z")

</div>

ex-\>010110 p1 can increase H by multiplying 4th and 5th element from h=1 to h=2

---

<div class="post-metadata">

**Author:** ![kingofnumbers](https://discuss.codechef.com/user_avatar/discuss.codechef.com/kingofnumbers/32/3653_2.png) [@kingofnumbers](https://discuss.codechef.com/u/kingofnumbers)\
**Post date:** [February 20, 2017, 9:31pm UTC](https://discuss.codechef.com/t/addmult-editorial/13990/10 "2017-02-20T21:31:12Z")

</div>

but initially h=2 (2nd and 4th elements)

---

<div class="post-metadata">

**Author:** ![chan55555](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@chan55555](https://discuss.codechef.com/u/chan55555)\
**Post date:** [October 30, 2017, 1:47pm UTC](https://discuss.codechef.com/t/addmult-editorial/13990/11 "2017-10-30T13:47:35Z")

</div>

Anyone, please help, I am stuck here.
