devarshi09's blog

By devarshi09, history, 6 years ago, In English

How to calculate (a power b) mod p correctly if b is exceeding long long integer limit in c++.

  • Vote: I like it
  • +1
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

O(log2(b))?

»
6 years ago, # |
  Vote: I like it +29 Vote: I do not like it

Let b = b1b2.....bn(concatenated) By induction, Let x = a ^ (b1b2...bk) (mod p)

a ^ (b1b2....b(k+1)) = ( a^(b1b2....bk) ) ^ 10 * (a ^ b(k+1)) = x^10 * a^b(k+1)

now you can calculate modulo without overflow

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

first i need to store b. But b is exceeding the integer limit! How can i store b so that i correctly calculate (a power b) mod p. p is prime.

  • »
    »
    6 years ago, # ^ |
    Rev. 2   Vote: I like it +8 Vote: I do not like it

    If b is in the form of a string, then we can calculate with the following code:

    ll B = 0;
    for (char c: b) {
        B = (10*B+(c-'0'))%(p-1);
    }
    

    Now assuming that a is nonzero.

    EDIT: This is exactly what DongwonShin wrote above. If b is being calculated via a DP table, then you can just take all values mod p - 1 and there won't be overflow.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      got it. thanks!

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      but why p-1?

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Fermat's theorem tells us that if (a, p) = 1. So we can take the power by the modulo.

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Thanks man!

          • »
            »
            »
            »
            »
            »
            6 years ago, # ^ |
              Vote: I like it +2 Vote: I do not like it

            In general if you have two co prime integers m and n, then m^p modulo n = m^(p%f(n)) modulo n, where f(n) is Euler totient function This.

            For any prime n, f(n)=n-1.So, the Fermat's theorem follows from this.

            • »
              »
              »
              »
              »
              »
              »
              6 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              I am using precomputed factorial and inverse factorial array to calculate b so should i use n-1 as the mod there also?

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      What if b is being calculated through nCr using precomputed factorial and inverse modulo arrays?Then also we've to use same mod p-1 ?

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        As long as your base(b) and p are co prime, I don't see why it can't be done. So, yes use same mod (p-1).

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
          Rev. 3   Vote: I like it 0 Vote: I do not like it

          I tried but then for example 11*(inv[11)) didn't turn out to be 1 when i used p-1 as mod but while using p as mod it did show 1. here inv[11] = power(11,mod-2,mod) where power is fast exponential function.

    • »
      »
      »
      6 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      What if b is calculated as a-c and c requires division operation?Then while using inverse modulo for division i should use which mod?I am confused.Please help.