asim2005's blog

By asim2005, 4 months ago, In English

Why should I modulo the variable ? I couldn't understand the mathematical things they have used in the tutorial.

  • Vote: I like it
  • 0
  • Vote: I do not like it

4 months ago, # |
  Vote: I like it 0 Vote: I do not like it


Here you don't turn the page unless you have more than 'm' names to write. Suppose, on some day, you have more than 'm' names that you have to write, i.e., a[i] > m, then you have to turn the page a[i]/m times.

The above, is assuming you start from fresh page every time.

But since you continue writing on the same page, you might have a partially filled page at the end of the day. So you need to continue from that page itself.

modulo(%) gives the remainder on division. So, for any 'a' and 'b', you can write a = floor((a/b)) * b + a%b.(dividend = divisor * quotient + remainder).

Here a/b gives the number of turns of pages, while a%b gives the measure of 'how much of the newest page is filled'.

Taking an example n= 3, m= 5 a = [3, 7, 9]

On the first day, you have to write 3 names. Since page length is 5 names long, you don't have to turn pages.

On second day, you will have to write 7 names. Since you have to start where you left off, at the end of second day, you have written 10 names(3 in first day, 7 in secod). You write first 2 names in the first page, filling it, then write next 5 names in the second. So you have turned 2 pages at the end of second day. Since 2 pages exactly hold 10 names written so far, you will start from fresh page. Note that you will turn the page immediately after it is filled.

On the third day, you have to write 9 names. You write 5 names, turn the page, then write 4 names.

Printing the result of a/b and update the number of names written in the newest page in each iteration.

The C++ code is here.