 # MTRICK - Editorial

Author: Nikhil Garg
Tester: Gerald Agapov
Editorialist: Jingbo Shang

Easy-Medium

### PREREQUISITES:

Programming Language, Simple Math.

### PROBLEM:

Perform the “Ancient Algorithm” described in the problem. And output the results in all steps. Remember that all numbers and operations are modulo by C.

### EXPLANATION:

As same as the title of the problem “Magic Trick”, there are some math and programming tricks in this problem.

Denote the array after i-th loop as Li[].

The first trick is a math trick – we can maintain a slope Ki and a intercept Di, such that all current numbers in Li[] equals to the original numbers Ki * L[] + Di. For each operation, we can update the K and D as the following rules:

``````if operation == 'R' {
K[i + 1] = K*
D[i + 1] = D*
} else if operation == 'A' {
K[i + 1] = K*
D[i + 1] = D* + A
} else if operation == 'M' {
K[i + 1] = K* * B
D[i + 1] = D* * B
}
``````

The second trick is a programming trick – at any time, Li[] is an interval of L[] (may reversed). Therefore, we can record the begin, end, and direction each time such that the Li* equals to the L[begin]. That is,

``````begin = 1
end = N
direction = 1
for i = 1 to N do {
if operation == 'R' {
swap(begin, end)
direction = -direction
}
UPDATE K and D
print L[begin] * K + D
begin = begin + direction
}
``````

Beside these magic tricks, there is also a common trick of long long exceeding. That is, because C is as large as 10^18, the multiple operation may exceed the type of long long. We can use a fast multiple, similar to fast power, to solve this exceeding problem without big integer in O(logC) time.

``````long long multiple(long long a, long long b, long long c) // a * b % c
{
if (b == 0) {
return 0
}
long long ret = multiple(a, b >> 1, c)
ret = (ret + ret) % c
if (b & 1) {
ret = (ret + a) % c
}
return ret
}
``````

In summary, we can solve this problem in O(N log C) for each test case.

### AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.
Tester’s solution can be found here and here

19 Likes

Just my 2 cents:

• constraints were so, that you can implement reverse operation in timelimit
• in Java you can use BigInteger (this time, but I have to learn that trick with long long)

My Java solution (accepted in last minute of the contest).

1 Like

Why my solution gives wrong answer?
The algorithm is same, only i am doing modulo exponent.
http://www.codechef.com/viewsolution/3251026

Whats the mistake in these codehttp://www.codechef.com/viewsolution/3224368 , http://www.codechef.com/viewsolution/3217878.
(It uses the last method given in the editorial).

What if we use two variables instead of two arrays to store the result of the multiplication and addition operation.My solution uses the same thinking.
http://www.codechef.com/viewsolution/3248148
But the solution produces WA.Can someone aid me to bring out the mistake in it.

Can you please check my solution?Unable to find the mistake.
http://www.codechef.com/viewsolution/3240662

can you please check the solution…unable to find mistake…
http://www.codechef.com/viewsolution/3246455

What I did was this. For reverse I kept count of no.of reverse operations. If this count is odd, your ans will be the last element of array & if even it will be the first element. I stored this element in ans_to_print variable. Since we didn’t need to manipulate it further. I deleted the element from the array. So, my trick of last for odd & first for even worked better. Now, for add & multiply, I kept two variable add & mult. As we know if we add some number to another number & than multiply with some another number like `(10+50)*20`, it will be `10*20 + 50*20`. So picking a trick from this I modified add & mult as below:

``````if(ch=='A')
else if(ch=='M')
mult=(mult*b)%c;
``````

So, in the end ans would simply be

``````ans_to_print=(ans_to_print*mult+add)%c
``````

Link to my codes: http://www.codechef.com/viewsolution/3173590 (Java) , http://www.codechef.com/viewsolution/3173373 (Python) 3 Likes

Can You Plz See My Solution…Unable to find Any Mistake…http://www.codechef.com/viewsolution/3214607
THANKS…

A better/time effective approach to (a*b)%c could be

``````long long int mult(long long int a, long long int b, long long int c)
{
a = a % c;
b = b % c;
long long int z = 0;
for (1; a; a >>= 1){
if(a & 1)
if((z =z+ b) >= c)
z = z- c;
if((b = 2 * b) >= c)
b =b- c;
}
return z;
}``````
1 Like

Why my this solution:
http://www.codechef.com/viewsolution/3206481
was giving nzec . All numbers in the listwere integers so i was just making a array out of those numbers while just making a simple list was working fine.

Can u please explain what this function does in detail…

``````long long multiple(long long a, long long b, long long c) // a * b % c
{
if (b == 0) {
return 0
}
long long ret = multiple(a, b >> 1, c)
ret = (ret + ret) % c
if (b & 1) {
ret = (ret + a) % c
}
return ret
}``````

can anyone explain me why this O(n) solution is giving TLE ??

Yes, brute force is feasible for this problem. But we can learn better algorithms from the editorial 3 Likes

Have you tried to replace your multiply_hack with the multiple function mentioned in the editorial?

1 Like

It looks like “void add(int s)” has a bug. You should have one more mod after the +. Try to find such tiny bugs around, and thus your debug skills will be improved fast.

The multiplication operator in your code, although mod c, may exceed unsigned long long. Please read the editorial to find the tricky way to get the product withou any exceeding. Thanks.

1 Like

In this java code, i tried so many test cases, even with 10^18 but it gives Wa… please tell where m getting wrong.

oh, you need to mod all L* by c. Try System.out.print((list* % c)+ " ");.

the mod c should be applied even if the all operations are “reverse”