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

Автор baowilliam, история, 19 месяцев назад, По-английски

with $$$(0\leq n\leq 2^{31}-1)$$$ caculate: $$$(2^{2^{n}} + 1)$$$ mod k $$$(1\leq k\leq 10^{6})$$$ i'm doing an exercise on mods for large numbers, i don't know if there is a more efficient solution than using binary exponentiation? Hope to help you, thanks (sorry for my bad english!!!!)

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

»
19 месяцев назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

Assuming the case when $$$k$$$ is a prime number.

Let, $$${ans = (2^{2^n} + 1)}$$$ mod $$$k$$$

let's modify a little bit

$$${2^{2^n}}$$$ mod $$${k = (ans + k - 1)}$$$ mod $$$k$$$ $$${ = x}$$$

Using Fermat Little Theorem, let $$${p = (2 ^ n)}$$$ mod $$$(k - 1)$$$

so, $$${x = (2 ^ p)}$$$ mod $$$k$$$.

so you may easily calculate $$$p$$$ first then calculate $$$x$$$ then can form the $$$ans$$$ easily :)

»
19 месяцев назад, # |
Rev. 7   Проголосовать: нравится 0 Проголосовать: не нравится

Deleted.

»
19 месяцев назад, # |
Rev. 6   Проголосовать: нравится +2 Проголосовать: не нравится

You can use the extension of Fermat's little theorem, the so-called Euler's Theorem. According to the theorem, the following is true.

$$$n^{\phi(m)} \equiv 1\pmod m$$$

Therefore the following is also true:

$$$n^k\equiv n^{k\bmod\phi(m)}\pmod m$$$

These problems, in CP and MO, are called Power Towers. They usually ask for a very big power modulo a composite number like these.

$$$a_0^{a_1^{a_2^{a_3^{a_4^{{\text{(omitted some powers)}}^{a_n}}}}}} \bmod m$$$

(btw how do I use antidiagonal dots on LaTeX? this long text looks ugly as heck)

  • »
    »
    19 месяцев назад, # ^ |
      Проголосовать: нравится +11 Проголосовать: не нравится

    but we just can use fermat's little theorem if and only if (n and m) are co-prime, <=> 2 and k are co-prime, and I really don't know the case when 2 and k aren't co-prime :(, can you explain a little bit?

    • »
      »
      »
      19 месяцев назад, # ^ |
      Rev. 2   Проголосовать: нравится +12 Проголосовать: не нравится

      Oops, I forgot to also explain this, the following is true regardless of whether the two are coprime or not.

      $$$x^{n}\equiv x^{\phi(m)+[n \bmod \phi(m)]} \mod m$$$

      UPD: LipArcanjo is correct, the exponent needs to exceed log2(m) for this

»
19 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Edit: as of chromate00's reply/explanation, I've realized this solution is bad and ugly. One should refer to his messages for a better solution.

Previous message
»
19 месяцев назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

The statement:

$$$n^e \ mod \ m = n^{\phi(m) + e \ mod \ \phi(m)} \ mod \ m$$$

is true for all $$$n ,m,$$$ and $$$e \geq log_2(m)$$$.

Source: https://nordic.icpc.io/ncpc2016/ncpc2016slides.pdf Problem E.

So If $$$n$$$ is greater or equal then $$$log_2(k)$$$, you use the equation, if not, you can compute $$$2^n$$$.