×

# 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[i]
D[i + 1] = D[i]
} else if operation == 'A' {
K[i + 1] = K[i]
D[i + 1] = D[i] + A
} else if operation == 'M' {
K[i + 1] = K[i] * B
D[i + 1] = D[i] * 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[i] 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

This question is marked "community wiki".

161446376
accept rate: 0%

19.8k350498541

i was wondering why O(n^2) was giving TLE because N<=1000 and time limit was 2sec. Wasn't it enough ??

(13 Jan '14, 23:09) 3★

Yes, from the theoretical complexity, it should work. But, you should also consider the implementations. If your implementation uses the mod operations too many times, or some other reasons lead to a big constant in time complexity, it may get TLE.

(14 Jan '14, 11:38)

@shangjingbo can you explain what is happening in this fast multiplication or give me some link

(14 Jan '14, 16:12) 3★

in the first approach what are the initial values of K[0] and D[0] ?

(14 Jan '14, 16:21)

Initially, K[0] = 1 and D[0] = 0, because L0[i] = 1 * L[i] + 0

(15 Jan '14, 12:11)

@sud210 we can use write b in binary, and prepare a, 2 * a, 4 * a, 8 * a, ..., 2^k * a. 2^k <= b < 2^(k+1). Then, we can get the b * a in logb times of addition.

(15 Jan '14, 12:13)

@shangjingbo why is it log(C)in the complexity? . shouldnt the multiplication step be log(B)?

(18 Jan '14, 01:53) 3★
showing 5 of 7 show all

 3 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') add=(add+a)%c; else if(ch=='M') mult=(mult*b)%c; add=(add*b)%c; /// This helped in maintaining the order of addition & multiplication.  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) :) answered 14 Jan '14, 11:04 498●1●7●16 accept rate: 22%
 1 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). answered 13 Jan '14, 15:18 16.9k●49●115●225 accept rate: 11% 3 Yes, brute force is feasible for this problem. But we can learn better algorithms from the editorial :P (13 Jan '14, 18:54)
 1 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; }  answered 14 Jan '14, 15:25 146●2●3●11 accept rate: 11% what is 'y' here in the expression "if((z =z+ y) >= c)"??? (14 Jan '14, 16:23) it was a typo , corrected now (14 Jan '14, 17:10)
 0 Why my solution gives wrong answer? The algorithm is same, only i am doing modulo exponent. http://www.codechef.com/viewsolution/3251026 answered 13 Jan '14, 16:12 1 accept rate: 0% 1 Have you tried to replace your multiply_hack with the multiple function mentioned in the editorial? (13 Jan '14, 18:55) Thanks, although got the TLE. I have to use the continuous multiplication like mentioned in editorial. (13 Jan '14, 22:57) Can anyone tell what is worong with that multiply_hack here?? (26 Jul '14, 18:57)
 0 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. answered 13 Jan '14, 17:23 16●2●4 accept rate: 0% 1 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. (13 Jan '14, 19:00) thanks for pointing out the problem in my code. (14 Jan '14, 07:17)
 0 Can you please check my solution?Unable to find the mistake. Thanks in advance. http://www.codechef.com/viewsolution/3240662 answered 13 Jan '14, 20:04 3★aq1_ 1●1●2 accept rate: 0% @aq1_ If you'll see addmod function, you have done x-m+y, but if even this "x-m+y" is greater than c, we again need to take the mod. You function should have the recursive call which returns(x+y+m) when x+y-m<0. (13 Jan '14, 20:09) damn_me3★
 0 can you please check the solution..unable to find mistake.... http://www.codechef.com/viewsolution/3246455 answered 13 Jan '14, 22:28 2★picasso 1 accept rate: 0% "multiplicativeConstant = (multiplicativeConstant * bModC) % C;" may exceed the unsigned long long when multiplication, please look into the editorial to find the solution to avoid exceeding. (13 Jan '14, 22:56)
 0 Can You Plz See My Solution....Unable to find Any Mistake....http://www.codechef.com/viewsolution/3214607 THANKS... answered 14 Jan '14, 13:41 0●1●3 accept rate: 0%
 0 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. answered 14 Jan '14, 22:45 16●4 accept rate: 0%
 0 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 }  answered 15 Jan '14, 11:04 296●8●21●22 accept rate: 11%
 0 can anyone explain me why this O(n) solution is giving TLE ?? answered 15 Jan '14, 23:43 3★paramvi 16●5 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:

×15,852
×1,722
×966
×281
×51

question asked: 13 Jan '14, 15:12

question was seen: 11,463 times

last updated: 22 Apr '15, 17:34