PROBLEM LINK:Author: Nikhil Garg DIFFICULTY:EasyMedium 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 ith loop as L_{i}[]. The first trick is a math trick  we can maintain a slope K_{i} and a intercept D_{i}, such that all current numbers in L_{i}[] equals to the original numbers K_{i} * L[] + D_{i}. For each operation, we can update the K and D as the following rules:
The second trick is a programming trick  at any time, L_{i}[] is an interval of L[] (may reversed). Therefore, we can record the begin, end, and direction each time such that the L_{i}[i] equals to the L[begin]. That is,
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.
For explanation about fast multiplication, please refer to this 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.
This question is marked "community wiki".
asked 13 Jan '14, 15:12
showing 5 of 7
show all

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
So, in the end ans would simply be
Link to my codes: http://www.codechef.com/viewsolution/3173590 (Java) , http://www.codechef.com/viewsolution/3173373 (Python) :) answered 14 Jan '14, 11:04

Just my 2 cents:
My Java solution (accepted in last minute of the contest). answered 13 Jan '14, 15:18
3
Yes, brute force is feasible for this problem. But we can learn better algorithms from the editorial :P
(13 Jan '14, 18:54)

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
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)

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). answered 13 Jan '14, 16:17
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.
(13 Jan '14, 18:57)
http://ideone.com/dFm38p In this java code, i tried so many test cases, even with 10^18 but it gives Wa.. please tell where m getting wrong.
(13 Jan '14, 19:37)
oh, you need to mod all L[i] by c. Try System.out.print((list[i] % c)+ " ");.
(13 Jan '14, 19:39)
the mod c should be applied even if the all operations are "reverse"
(13 Jan '14, 19:40)
http://ideone.com/dFm38p This code gives WA (if i use xor for reversing), and TLE if i do it using loop(done in comments).
(13 Jan '14, 19:50)
However, i saw various solutions in which xor was used, but then they are not taking mod after reversing, is it such??
(13 Jan '14, 19:51)
oh, in your reverse, "int to=num" should be "int to=num  1" I think. You can mod them before output.
(13 Jan '14, 22:51)
@shangjingbo Its still giving WA, i dont know whats getting wrong!!!
(13 Jan '14, 23:13)
Please calm down and look your codes line by line, sentence by sentence carefully. I think you can figure out the bugs. :)
(14 Jan '14, 11:42)
And I have tested your code locally. It even failed by the sample cases! After I fixed the problem, I found your code TLE. There are too many unnecessary mod operations. And also, you can read the editorial to find a faster solution. Only O(N log C) for each case.
(14 Jan '14, 12:09)
showing 5 of 10
show all

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
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)

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
@aq1_ If you'll see addmod function, you have done xm+y, but if even this "xm+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+ym<0.
(13 Jan '14, 20:09)

can you please check the solution..unable to find mistake.... http://www.codechef.com/viewsolution/3246455 answered 13 Jan '14, 22:28
"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)

Can You Plz See My Solution....Unable to find Any Mistake....http://www.codechef.com/viewsolution/3214607 THANKS... answered 14 Jan '14, 13:41

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

Can u please explain what this function does in detail....
answered 15 Jan '14, 11:04

i was wondering why O(n^2) was giving TLE because N<=1000 and time limit was 2sec. Wasn't it enough ??
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.
@shangjingbo can you explain what is happening in this fast multiplication or give me some link
in the first approach what are the initial values of K[0] and D[0] ?
Initially, K[0] = 1 and D[0] = 0, because L0[i] = 1 * L[i] + 0
@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.
@shangjingbo why is it log(C)in the complexity? . shouldnt the multiplication step be log(B)?