Блог пользователя snorkel

Автор snorkel, история, 5 лет назад, По-английски

Trying to solve this problem. I got the last non-zero digit by working with $$$2$$$-s and $$$5$$$-s, but here's an easier solution by using $$$10$$$ directly.

Can anyone explain why this works? How taking modulo $$$10^9$$$ does not make it wrong?

Code

Thanks.

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
 
Проголосовать: нравится -26 Проголосовать: не нравится

we're interested in the last digit so even taking modulo $$$10$$$ is right

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I think it's modulo 1e9 because the zeros will never go past that far on a single iteration, for example instead it could be 1000 if N went up to 99 because the most the zeros will go up at once is twice because of 25 (4*25 = 100).

  • »
    »
    5 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    boocoo What I don't understand is when we do modulo, we lose some information about the number.

    • »
      »
      »
      5 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      It is true that by doing modulo, we lose some information about the actual number. However, we are more interested in the least significant digit (rightmost non zero digit). Taking modulo removes part of the most significant digits (leftmost digits are lost).

      Here, the only purpose of having modulo 1e9 is to keep the answer in such a range that multiplying a big number like N ( <= 2e7 according to the question ), the product still fits in a long long variable.

      By repeatedly dividing ans by 10 inside the while loop, we are losing information about the least significant digits, only if these digits are 0 (ans % 10 == 0). In the same way, we don't need to keep track of unnecessary most significant digits. Doing so, we are focussed on the requirement, i.e., the last (rightmost) non zero digit.

      • »
        »
        »
        »
        5 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        I want to know exactly the reason for that, you haven't told how is it enough.

        • »
          »
          »
          »
          »
          5 лет назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          Let's say at some point you have ans = 123 (after removing the right hand side 0s).

          • Now you multiply ans with some big number 49834344.
          • ans becomes 6,129,624,312.
          • The next number you would be multiplying is 49834343
          • ans would become 305,465,800,425,347,016.
          • On going one step further, you will easily move beyond the range of long long.

          Instead, when you do modulo 1e9 at each step, it will look something like this

          • After multiplying 49834344, ans = 129,624,312 ( modulo 1e9 )
          • Multiply this again 49834343, ans = 6,459,742,425,347,016 = 425,347,016 ( modulo 1e9 ).

          Notice that doing the modulus operation doesn't change the last digits, It only gets rid of the digits to the left. With modulo and without modulo, in both cases the last digits are the same since the modulus is done with a power of 10.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Maybe you need to calculate factorial[n] * inverse(factorial[m])?

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

Any better explanations are still welcome.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

It doesn't work for $$$5^{10}=9765625$$$ as far as I understand

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I was convinced this was true and was trying to prove it, but got stuck in some part of the proof. I'll leave my attempt with a mark in the error.

False proof