# MARM - Editorial

**URL:** <https://discuss.codechef.com/t/marm-editorial/39304>\
**Category:** editorial\
**Tags:** array, simple, oct19, r\_64\
**Created:** [September 27, 2019, 4:15am UTC](https://discuss.codechef.com/t/marm-editorial/39304 "2019-09-27T04:15:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![r\_64](https://discuss.codechef.com/user_avatar/discuss.codechef.com/r_64/32/214520_2.png) [@r\_64](https://discuss.codechef.com/u/r_64)\
**Post date:** [September 27, 2019, 4:15am UTC](https://discuss.codechef.com/t/marm-editorial/39304/1 "2019-09-27T04:15:56Z")

</div>

# PROBLEM LINK:

[Div 1](https://www.codechef.com/OCT19A/problems/MARM)  
[Div 2](https://www.codechef.com/OCT19B/problems/MARM)  
[Practice](https://www.codechef.com/problems/MARM)

_Author:_ [Mayank Agrawal](https://www.codechef.com/users/mayank1601)  
_Tester:_ [Hanlin Ren](https://www.codechef.com/users/r_64)  
_Editorialist:_ [Hanlin Ren](https://www.codechef.com/users/r_64)

# DIFFICULTY:

SIMPLE

# PREREQUISITES:

Array manipulation

# PROBLEM:

You are given an array A[0\sim (N-1)], and you need to perform the following operations for i from 0 to K-1:  
A[i\bmod N]\gets A[i\bmod N]\text{ xor } A[N-(i\bmod N)-1].

# QUICK EXPLANATION:

- Solution 1: for every pair of indices (i,N-i-1), we compute the number of operations done on this pair, and use the corresponding formula.
- Solution 2: We simply let K\gets K\bmod 3N and do what we’re told to do. When N is odd, we need to deal with A[\frac{N-1}{2}] as a special case, though.

# EXPLANATION:

## Subtask 1

Since K is small in this subtask, the problem is easy: we simply implement what the problem asks us to do.

Time complexity: O(K+N).

## Subtask 2

For every 0\le p\<N, we pair the indices p and N-p-1 together. (Note that if p=N-q-1, then p is also paired with N-p-1=q.) It’s easy to see that each A[p] only interacts with A[N-p-1].

Assume p\<N-p-1, and for convenience let’s denote q=N-p-1. Let’s denote the original array as A, and the modified array as A'. Let’s simulate the process in the problem statement, let i\gets 0 to K-1, and see when A'[p] and A'[q] would make changes:

- After the operation that i=p, A'[p]=A[p]\oplus A[q], and A'[q]=A[q].
- After the operation that i=q, A'[p]=A[p]\oplus A[q], and A'[q]=A[q]\oplus (A[p]\oplus A[q])=A[p].
- After the operation that i=N+p, A'[p]=(A[p]\oplus A[q])\oplus A[p]=A[q], and A'[q]=A[p].
- …

Or let’s make a table so we can see it more clearly:

```
After i = | A'[p] | A'[q]
        p | A[p] xor A[q] | A[q]
        q | A[p] xor A[q] | A[p]
    N + p | A[q] | A[p]
    N + q | A[q] | A[p] xor A[q]
   2N + p | A[p] | A[p] xor A[q]
   2N + q | A[p] | A[q]
   3N + p | A[p] xor A[q] | A[q]
   3N + q | A[p] xor A[q] | A[p]
   4N + p | A[q] | A[p]
   4N + q | A[q] | A[p] xor A[q]
   5N + p | A[p] | A[p] xor A[q]
   5N + q | A[p] | A[q]
      ... | ... | ...

```

Let b[i] be the number of operations done on A'[i]. If i\<K\bmod n, then b[i]=\lfloor n/k\rfloor+1; Otherwise b[i]=\lfloor n/k\rfloor. We can see that

- the first operation on A'[p] changes it to A[p]\oplus A[q];
- the second operation on A'[p] changes it to A[q];
- the third operation changes it back to A[p].

The above argument only works for p\<N/2. For q\>N/2, we can still find the pattern:

- the first operation on A'[q] changes it to A[p];
- the second operation changes it to A[p]\oplus A[q];
- the third operation changes it back to A[q].

Therefore, we can compute the values of each A'[p],A'[q] according to b[p]\bmod 3 and b[q]\bmod 3 respectively. For more details, please refer to Setter’s solution.

## A special case

When N is odd and p=(N-1)/2, we have p=N-p-1=q. In this case, the first operation on A[p] becomes A[p]\gets A[p]\oplus A[p], therefore it sets A[p] to 0. Further operations on A[p] can only leave it as 0 forever.

Thus there is a special case: if p=(N-1)/2, then let’s examine b[p], _i.e._ the number of operations done on A[p]. If b[p]=0, then A'[p]=A[p]; otherwise A'[p]=0.

# ALTERNATE EXPLANATION:

In fact, there is a simpler-looking solution: we simply let K\gets K\bmod 3N and do what we’re asked to do. The reason is also simple from the above table: 3N operations restore the same array as before, except for the special case where A[(N-1)/2] is set to zero. The pseudocode is very clear:

```auto
if (N is odd and K >= (N - 1) / 2) then
  A[(N - 1) / 2] = 0
K = K % (3 * N)
for i = 0 to K - 1
  A[i mod N] = A[i mod N] xor A[N - (i mod N) - 1]

```

In the tester’s alternate solution, I set  
K\gets\begin{cases}K&K\<3N\\3N+(K\bmod 3N)&K\>3N\end{cases},  
and didn’t handle the special case at all. If you are interested, you can think about why this works. 🙂

# SOLUTIONS:

> **Setter's Solution**
>
> ```auto
> #include <bits/stdc++.h>
> using namespace std;
> #define mod 1000000007
> #define mp make_pair
> #define pb push_back
> #define ll long long int
> int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
> 
> int main()
> {
> int i,j,x,y,t;
> cin>>t;
> while(t--){
> ll n,k;
> cin>>n>>k;
> ll p = k/n;
> k%= n;
> ll a[n] , b[n];
> for(i=0;i<n;++i){
> cin>>a[i];
> b[i]= p;
> if(i<k)
> ++b[i];
> }
> for(i=0;i<n/2;++i){
> if(b[i]%3LL==0LL){
> b[i] = a[i];
> }else if(b[i]%3LL==1LL){
> b[i] = a[i]^a[n-i-1];
> }else{
> b[i] = a[n-i-1];
> }
> 			
> if(b[n-i-1]%3LL==0LL){
> b[n-i-1] = a[n-i-1];
> }else if(b[n-i-1]%3LL==1){
> b[n-i-1] = a[i];
> }else{
> b[n-i-1] = a[i]^a[n-i-1];
> }
> }
> if(n%2){
> if(b[n/2]==0){
> b[n/2]= a[n/2];
> }else{
> b[n/2]= 0;
> }
> }
> for(i=0;i<n;++i){
> cout<<b[i]<<" ";
> }cout<<"\n";
> }   
> 	
> return 0;
> }
> 
> ```

> **Tester's Solution**
>
> ```auto
> #include <bits/stdc++.h>
> using namespace std;
> 
> template <class Q>
> void gi(Q &x) {char ch = getchar(); x = 0; while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();}
> template <class Q>
> void pi(Q x) {if (x > 9) pi(x / 10); putchar(x % 10 + 48);}
> 
> int n;
> long long k;
> int a[100200];
> void doit() {
> gi(n); gi(k);
> for (int i = 0; i < n; i++) gi(a[i]);
> for (int i = 0; i < n; i++) {
> int ans = 0, num_of_op = k / n + ((k % n) > i), j = n - i - 1;
> if (i < j) {
> switch (num_of_op % 3) {
> case 0: ans = a[i]; break;
> case 1: ans = a[i] ^ a[j]; break;
> case 2: ans = a[j]; break;
> }
> } else if (i > j) {
> switch (num_of_op % 3) {
> case 0: ans = a[i]; break;
> case 1: ans = a[j]; break;
> case 2: ans = a[i] ^ a[j]; break;
> }
> } else {
> if (k > i) ans = 0;
> else ans = a[i];
> }
> pi(ans); putchar(' ');
> }
> putchar('\n');
> }
> 
> int main() {int t; gi(t); while (t--) doit(); return 0;}
> 
> ```

> **Tester's alternate Solution**
>
> ```auto
> #include <bits/stdc++.h>
> using namespace std;
> 
> template <class Q>
> void gi(Q &x) {char ch = getchar(); x = 0; while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();}
> template <class Q>
> void pi(Q x) {if (x > 9) pi(x / 10); putchar(x % 10 + 48);}
> 
> int n;
> long long k;
> int a[100200];
> 
> void doit() {
> int i;
> gi(n); gi(k);
> if (k >= n * 3) k = n * 3 + (k % (n * 3));
> for (i = 0; i < n; i++) gi(a[i]);
> for (i = 0; i < k; i++) {
> a[i % n] = a[i % n] ^ a[n - (i % n) - 1];
> }
> for (i = 0; i < n; i++) pi(a[i]), putchar(' ');
> putchar('\n');
> }
> 
> int main() {int t; gi(t); while (t--) doit(); return 0;}
> 
> ```

---

<div class="post-metadata">

**Author:** ![akshitdesai](https://discuss.codechef.com/user_avatar/discuss.codechef.com/akshitdesai/32/38594_2.png) [@akshitdesai](https://discuss.codechef.com/u/akshitdesai)\
**Post date:** [October 14, 2019, 6:40pm UTC](https://discuss.codechef.com/t/marm-editorial/39304/2 "2019-10-14T18:40:42Z")

</div>

I did the same thing…  
Can anybody point out where i am wrong?  
My solution:-  
[https://www.codechef.com/viewsolution/27350673](https://www.codechef.com/viewsolution/27350673)

---

<div class="post-metadata">

**Author:** ![s5960r](https://discuss.codechef.com/user_avatar/discuss.codechef.com/s5960r/32/9612_2.png) [@s5960r](https://discuss.codechef.com/u/s5960r)\
**Post date:** [October 14, 2019, 7:15pm UTC](https://discuss.codechef.com/t/marm-editorial/39304/3 "2019-10-14T19:15:41Z")

</div>

Just take the brute force solution and add this line…and get AC😛

```auto
if(k>3*n){
    k=k%(3*n) + 3*n;
}

```

---

<div class="post-metadata">

**Author:** ![anon70990627](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon70990627](https://discuss.codechef.com/u/anon70990627)\
**Post date:** [October 14, 2019, 9:16pm UTC](https://discuss.codechef.com/t/marm-editorial/39304/4 "2019-10-14T21:16:40Z")

</div>

This was really very clever. Thank you, got something new

---

<div class="post-metadata">

**Author:** ![doga5743](https://discuss.codechef.com/user_avatar/discuss.codechef.com/doga5743/32/9878_2.png) [@doga5743](https://discuss.codechef.com/u/doga5743)\
**Post date:** [October 15, 2019, 5:53am UTC](https://discuss.codechef.com/t/marm-editorial/39304/5 "2019-10-15T05:53:54Z")

</div>

Used the same approach.  
After every three iteration array comes back to its original form.  
Just need to check if k\>n then set the element a[n/2]=0.

[https://www.codechef.com/viewsolution/27158295](https://www.codechef.com/viewsolution/27158295)

---

<div class="post-metadata">

**Author:** ![sudheerays123](https://discuss.codechef.com/user_avatar/discuss.codechef.com/sudheerays123/32/231186_2.png) [@sudheerays123](https://discuss.codechef.com/u/sudheerays123)\
**Post date:** [October 15, 2019, 6:12am UTC](https://discuss.codechef.com/t/marm-editorial/39304/6 "2019-10-15T06:12:20Z")

</div>

Why is this is giving wrong answer for me ?

---

<div class="post-metadata">

**Author:** ![s5960r](https://discuss.codechef.com/user_avatar/discuss.codechef.com/s5960r/32/9612_2.png) [@s5960r](https://discuss.codechef.com/u/s5960r)\
**Post date:** [October 15, 2019, 6:45am UTC](https://discuss.codechef.com/t/marm-editorial/39304/7 "2019-10-15T06:45:59Z")

</div>

What’s your solution link?

---

<div class="post-metadata">

**Author:** ![sudheerays123](https://discuss.codechef.com/user_avatar/discuss.codechef.com/sudheerays123/32/231186_2.png) [@sudheerays123](https://discuss.codechef.com/u/sudheerays123)\
**Post date:** [October 15, 2019, 6:54am UTC](https://discuss.codechef.com/t/marm-editorial/39304/8 "2019-10-15T06:54:56Z")

</div>

> **[\#include using namespace std;typedef long long ll;#define - Pastebin.com](https://pastebin.com/E9McYyQG)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

---

<div class="post-metadata">

**Author:** ![ankit\_6564](https://discuss.codechef.com/user_avatar/discuss.codechef.com/ankit_6564/32/12658_2.png) [@ankit\_6564](https://discuss.codechef.com/u/ankit_6564)\
**Post date:** [October 15, 2019, 6:58am UTC](https://discuss.codechef.com/t/marm-editorial/39304/9 "2019-10-15T06:58:50Z")

</div>

can anybody help me with my solution…I am getting wrong answer with my solution  
[https://www.codechef.com/viewsolution/27380639](https://www.codechef.com/viewsolution/27380639)

The logic exactly same for another solution which is giving AC  
[https://www.codechef.com/viewsolution/27358186](https://www.codechef.com/viewsolution/27358186)

---

<div class="post-metadata">

**Author:** ![s5960r](https://discuss.codechef.com/user_avatar/discuss.codechef.com/s5960r/32/9612_2.png) [@s5960r](https://discuss.codechef.com/u/s5960r)\
**Post date:** [October 15, 2019, 7:10am UTC](https://discuss.codechef.com/t/marm-editorial/39304/10 "2019-10-15T07:10:53Z")

</div>

It’s just an issue with operator’s precedence

Change This

```auto
if(k>3*n){
    k=k%3*n + 3*n;
 }

```

To This, By adding brackets around 3n…

```auto
if(k>3*n){
    k=k%(3*n) + 3*n;
}

```

It’ll Work Now 😄

---

<div class="post-metadata">

**Author:** ![sudheerays123](https://discuss.codechef.com/user_avatar/discuss.codechef.com/sudheerays123/32/231186_2.png) [@sudheerays123](https://discuss.codechef.com/u/sudheerays123)\
**Post date:** [October 15, 2019, 7:24am UTC](https://discuss.codechef.com/t/marm-editorial/39304/11 "2019-10-15T07:24:04Z")

</div>

YES  
it worked  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![da\_grt\_aru](https://discuss.codechef.com/user_avatar/discuss.codechef.com/da_grt_aru/32/7646_2.png) [@da\_grt\_aru](https://discuss.codechef.com/u/da_grt_aru)\
**Post date:** [October 15, 2019, 2:06pm UTC](https://discuss.codechef.com/t/marm-editorial/39304/12 "2019-10-15T14:06:54Z")

</div>

Oh man!! Nice 😉

---

<div class="post-metadata">

**Author:** ![himanshu42](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@himanshu42](https://discuss.codechef.com/u/himanshu42)\
**Post date:** [October 15, 2019, 4:29pm UTC](https://discuss.codechef.com/t/marm-editorial/39304/13 "2019-10-15T16:29:05Z")

</div>

Great Explanation !

---

<div class="post-metadata">

**Author:** ![aryan12](https://discuss.codechef.com/user_avatar/discuss.codechef.com/aryan12/32/208155_2.png) [@aryan12](https://discuss.codechef.com/u/aryan12)\
**Post date:** [October 15, 2019, 4:48pm UTC](https://discuss.codechef.com/t/marm-editorial/39304/14 "2019-10-15T16:48:04Z")

</div>

You made the same mistake which I had made in the contest and later rectified it.

> **Test Case**
>
> 1  
> 3 1  
> 1 2 3

> **Your Output**
>
> 2 0 3

> **Expected Output**
>
> 2 2 3

> **Hint to AC**
>
> Just add a condition for the middle element, if k is greater than the index of the middle element[1 indexed].

Hope this helps. 🙂

---

<div class="post-metadata">

**Author:** ![priyanshi\_garg](https://discuss.codechef.com/user_avatar/discuss.codechef.com/priyanshi_garg/32/272346_2.png) [@priyanshi\_garg](https://discuss.codechef.com/u/priyanshi_garg)\
**Post date:** [October 15, 2019, 5:57pm UTC](https://discuss.codechef.com/t/marm-editorial/39304/15 "2019-10-15T17:57:08Z")

</div>

I am checking condition that in case when (k\>(3_n)), so if n is odd then make mid element 0 and if even then just do nothing and after checking this condition , i perform iteration from 0 to (k modulo (3_n)) over list and then prints it but it gives me wrong answer .  
But if I use your suggestion, it works fine.  
Pls tell me the reason why this happens ?

---

<div class="post-metadata">

**Author:** ![suprabhat\_25](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@suprabhat\_25](https://discuss.codechef.com/u/suprabhat_25)\
**Post date:** [October 16, 2019, 5:56am UTC](https://discuss.codechef.com/t/marm-editorial/39304/16 "2019-10-16T05:56:57Z")

</div>

Where am i committing mistake?

> **[Ideone.com](https://ideone.com/gQcRHk)**
>
> 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:** ![codinginveins](https://discuss.codechef.com/user_avatar/discuss.codechef.com/codinginveins/32/27895_2.png) [@codinginveins](https://discuss.codechef.com/u/codinginveins)\
**Post date:** [October 16, 2019, 2:22pm UTC](https://discuss.codechef.com/t/marm-editorial/39304/18 "2019-10-16T14:22:52Z")

</div>

Please anyone explain why this approach is wrong?

[https://www.codechef.com/viewsolution/27207067](https://www.codechef.com/viewsolution/27207067)

---

<div class="post-metadata">

**Author:** ![ssjgz](https://discuss.codechef.com/user_avatar/discuss.codechef.com/ssjgz/32/346011_2.png) [@ssjgz](https://discuss.codechef.com/u/ssjgz)\
**Post date:** [October 16, 2019, 2:52pm UTC](https://discuss.codechef.com/t/marm-editorial/39304/19 "2019-10-16T14:52:27Z")

</div>

Here’s a random testcase it fails on:

```auto
1
27 93
642 424 865 356 33 504 691 881 460 121 154 661 598 520 110 274 430 197 666 547 881 118 504 365 288 231 209 

```

(the answer should be

```auto
595 335 577 9 473 398 450 338 854 188 308 903 598 0 110 274 430 197 666 547 881 118 504 365 288 231 209

```

)

---

<div class="post-metadata">

**Author:** ![kp99](https://discuss.codechef.com/user_avatar/discuss.codechef.com/kp99/32/24843_2.png) [@kp99](https://discuss.codechef.com/u/kp99)\
**Post date:** [October 16, 2019, 6:37pm UTC](https://discuss.codechef.com/t/marm-editorial/39304/20 "2019-10-16T18:37:20Z")

</div>

i am using dp but my solution is giving wrong answer  
here it is…  
#include \<bits/stdc++.h\>  
using namespace std;  
#define gcd(a,b) \_\_gcd((a),(b))  
#define lcm(a,b) ((a)_(b))/gcd((a),(b))  
typedef long long int lli;  
int main()  
{  
ios\_base::sync\_with\_stdio(false);  
cin.tie(NULL);  
cout.tie(NULL);  
lli tc;  
cin\>\>tc;  
while(tc–)  
{  
lli i,j,n,k,c;  
cin\>\>n\>\>k;  
lli arr[n],dp[3][n];  
for(i=0;i\<n;i++)  
cin\>\>arr[i];  
if(n%2==0)  
{  
for(i=0;i\<3;i++)  
{  
for(j=0;j\<n;j++)  
{  
if(i==0)  
{  
if(j\<n/2)  
{  
dp[i][j]=arr[j] ^ arr[n-1-j];  
}  
else  
{  
dp[i][j]=arr[j] ^ dp[0][n-1-j];  
}  
}  
else  
{  
if(j\<n/2)  
{  
dp[i][j]=dp[i-1][j] ^ dp[i-1][n-1-j];  
}  
else  
{  
dp[i][j]=dp[i-1][j] ^ dp[i][n-1-j];  
}  
}  
}  
}  
}  
else  
{  
for(i=0;i\<3;i++)  
{  
for(j=0;j\<n;j++)  
{  
if(i==0)  
{  
if(j\<=n/2)  
{  
dp[i][j]=arr[j] ^ arr[n-1-j];  
}  
else  
{  
dp[i][j]=arr[j] ^ dp[0][n-1-j];  
}  
}  
else  
{  
if(j\<=n/2)  
{  
dp[i][j]=dp[i-1][j] ^ dp[i-1][n-1-j];  
}  
else  
{  
dp[i][j]=dp[i-1][j] ^ dp[i][n-1-j];  
}  
}  
}  
}  
}  
/_  
for(i=0;i\<3;i++)  
{  
for(j=0;j\<n;j++)  
{  
cout\<\<dp[i][j]\<\<" “;  
}  
cout\<\<’\n’;  
}\*/  
lli l,r;  
l=k%n;  
r=(k-1)%3;  
if(r==0)  
{  
for(i=0;i\<n;i++)  
{  
if(i\<l)  
{  
cout\<\<dp[2][i]\<\<” “;  
}  
else  
{  
cout\<\<dp[1][i]\<\<” “;  
}  
}  
}  
else if(r==1)  
{  
for(i=0;i\<n;i++)  
{  
if(i\<l)  
cout\<\<dp[2][i]\<\<” “;  
else  
cout\<\<dp[1][i]\<\<” “;  
}  
}  
else  
{  
for(i=0;i\<n;i++)  
{  
if(i\<l)  
{  
cout\<\<dp[r][i]\<\<” “;  
}  
else  
{  
cout\<\<dp[r-1][i]\<\<” ";  
}  
}  
}  
for(i=0;i\<n;i++)  
{

```
  }

cout<<'\n';

```

}  
return 0;  
}

---

<div class="post-metadata">

**Author:** ![kp99](https://discuss.codechef.com/user_avatar/discuss.codechef.com/kp99/32/24843_2.png) [@kp99](https://discuss.codechef.com/u/kp99)\
**Post date:** [October 16, 2019, 6:38pm UTC](https://discuss.codechef.com/t/marm-editorial/39304/21 "2019-10-16T18:38:16Z")

</div>

pls anyone can explain me how to solve this problem using dynamic programming

[Next page](https://discuss.codechef.com/t/marm-editorial/39304.md?page=2)
