Project Euler Problem 97: Large non-Mersenne prime【快速冪】

PE其他解題報告請參考這裏,本題答案在留言首條

Large non-Mersenne prime

Problem 97


The first known prime found to exceed one million digits was discovered in 1999, and is a Mersenne prime of the form 2697259312^{6972593}−1; it contains exactly 2,098,960 digits. Subsequently other Mersenne primes, of the form 2p−1, have been found which contain more digits.

However, in 2004 there was found a massive non-Mersenne prime which contains 2,357,207 digits: 28433×27830457+1.28433×2^{7830457}+1.

Find the last ten digits of this prime number.


題意:

讓你求28433×27830457+1.28433×2^{7830457}+1.的最後十位

分析:

快速冪直接搞定

參考代碼
mod = int(1e10)
def quick_pow(a, b):
    res = 1
    while (b):
        if (b & 1): res = res * a % mod
        a *= a
        b >>= 1
    return res
print((28433 * quick_pow(2, 7830457) + 1)%mod)

閱讀好習慣:點贊 + 收藏~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章