You are not logged in. Please login at www.codechef.com to post your questions!

×

World Codesprint 12 Hackerrank Editorial

Can Someone make a good editorial on World Codesprint 12 questions of hackerrank. Especially the "factorial array" question? Thanks in Advance.

asked 18 Dec '17, 23:53

vaibzz's gravatar image

4★vaibzz
1005
accept rate: 12%

edited 18 Dec '17, 23:54


I've solved this problem using the concept of Lazy Propagation in Segment Tree.

First pre-compute the factorial up to 39 with modulo N i.e. 10^9 and for integer greater than 39 it's factorial under modulo of N will be 0 so we will take advantage of that and store the factorial up to 39 in an array.

Now create the segment tree where each node of segment tree is an array of 40 integers given below

struct nod{ int num[40]; };

Since each node of segment tree will have info of a range, we will keep info of number of integers equals to i where i<40 we do not need to store info about integers >=40(Since their factorial under modulo N is 0). This process is similar to Hashing.

We will also create a Lazy Tree which will keep info about lazy updates i.e. what to add to each elements in a range of that lazy node.

For query of Type 1: We will update the segment tree by checking the corresponding lazy tree node. Let's say value at node of lazy tree is 3 it means we have to add 3 to each elements of the array. In segment tree we will shift the array by 3 i.e A[i+3]=A[i] in corresponding range of segment tree. We will loose info about elements which will become >=40 after updates.

For query of type 3: We will do same as Type 1 by updating in range(r,r) and whole segment tree will gets updated according to that.

For query of type 2: Reach to the node of segment tree from which is in the range and updating all the nodes coming in the path according to the lazy tree and calculate the answer by simple mathematics i.e. (Factorial of I * Number of integers equal to I)%N for all I<40

Code: link text

link

answered 19 Dec '17, 03:18

j_s_r_r806's gravatar image

5★j_s_r_r806
262
accept rate: 100%

Thanx for replyting! I can not get the core concept of your segTree. Can you please elaborate the part A[i+3] = A[i] ? It will be very nice of you if you make a video editorial on this.

(19 Dec '17, 18:17) vaibzz4★
1

'A' is an array of size 40 representing a node N of segment tree. Where A[i] is equals to number of integers equals to 'i' & i<40.If we've to add 'k' to each of the elements in the range of node N in original array then for this we will update the segment tree by shifting array 'A' by 'k' to right.Because the number of integers which were equals to 'i' will become equals to 'i+k' and we do not need to care about the integers 'i+k'>=40.i.e. A[i+k]=A[i]

(20 Dec '17, 14:27) j_s_r_r8065★

Yeah! I got your point. Thanks

(20 Dec '17, 18:28) vaibzz4★

@j_s_r_r806 can you plz check what's wrong with my code. I used same logic as yours and its coming right when I manually test it. but failing on hackerrank. Here is the link https://ideone.com/EGxBdK

(20 Dec '17, 23:44) vaibzz4★

FACTORIAL ARRAY

The thing to notice hear is 40 factorial has 9 zeroes at the end i.e it is divisible by 10^9. Hence, any factorial above this number will also be divisible by 10^9 (since it will always be a multiple of 40!)

since the operations are only of increment types, a bruteforce way of incrementing will require 40*n operations in worst case after which the elements will be zero.

ofcource, you can update the elements back to a smaller number, but only q times, hence 40*q at max for such numbers.

We build a segment tree on the array ad maintain a set which contains the indices of all the elements less than 40.

type 2 is simply a range sum query.

type 1:

to increment elements between l and r, we use lower bound on our set to find the first index greater than or equal to l which has value less than 40, and continue till the index exceeds r.

for each index, we update the segment tree with new factorial value also check if val>40 now, if it is, we remove this index from the set.

Type 3:

we update the element in the segment tree as usual and also see if the new value is less than 40 but old value wasn't. in this case we insert this index to our set.

My Solution: https://www.hackerrank.com/contests/world-codesprint-12/challenges/factorial-array/submissions/code/1304466105

link

answered 19 Dec '17, 00:51

abdullah768's gravatar image

6★abdullah768
2.4k420
accept rate: 17%

Thanks for Replying! what do we store in the segment tree, The values of A[i] or their factorials?

(19 Dec '17, 02:04) vaibzz4★

Since we need to query for the sum of factorials, we store factorials in the segment tree.

(19 Dec '17, 11:02) abdullah7686★

It would be very nice if someone just make a video editorial on this question. Thanks in Advance!

link

answered 19 Dec '17, 18:18

vaibzz's gravatar image

4★vaibzz
1005
accept rate: 12%

just click the editorial tab for each problem

link

answered 19 Dec '17, 21:01

we7d's gravatar image

2★we7d
222
accept rate: 0%

edited 19 Dec '17, 21:01

For all elements>=40, its factorial mod 10^9 will be zero. So the problem breaks down to maintaining the count of numbers from 1 to 39 in given range. We can do this by using square root decomposition or segment tree. I solved this problem using square root decomposition. Solution

link

answered 20 Dec '17, 01:19

rj25's gravatar image

4★rj25
2305
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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:

×310
×1

question asked: 18 Dec '17, 23:53

question was seen: 694 times

last updated: 20 Dec '17, 23:44