Calculate a*a mod n without overflow

后端 未结 5 1750

I need to calculate a*a mod n but a is fairly large, resulting in overflow when I square it. Doing ((a % n)*(a % n)) % n

5条回答
  •  日久生厌
    2020-12-02 00:59

    You can implement the multiplication yourself, adding n each run, and mod'ing the result right away.

提交回复
热议问题