任务 4
实现模幂算法,通过服务器的检验。
访问http://xxx/step_04
服务器会给你10个问题,每个问题包含三个数(a,b,c),请给出a^b%c的值。返回值写入字段ans,10个数字用逗号,隔开,提交到http://xxx/step_04
提示:注意逗号必须是英文逗号。
{"is_success": true, "questions": "[[744514414427, 559362725, 935740772056], [195406089, 496563474, 106218341108], [295455309208, 8117, 201041123261], [325, 452310091, 7262], [756939895, 838624500, 5986], [2147, 943269921393, 7414], [666963763, 424, 222941841190], [6036, 374096724, 341319300], [800, 7716, 968212866], [851, 529550654, 5736]]"}
给的数据。。。day2直接访问了这个出题网址,没有提示卡了好久,气死
任务四就是实现模幂算法后计算十族数据根据day2的方法提交即可
参考:https://blog.csdn.net/storyfull/article/details/103423919
参考:https://blog.csdn.net/sinat_34853107/article/details/104885711
参考:https://blog.csdn.net/ggdhs/article/details/90141960
参考:https://blog.csdn.net/iwts_24/article/details/79780596
参考:https://blog.csdn.net/lozcy/article/details/88370691
参考:https://www.pythonheidong.com/blog/article/410709/76eb5111d7d01abec11e/
参考:https://my.oschina.net/u/4401036/blog/3713526
其实都大同小异,差不多的实现方法,不过不同的人分析不太一样可以借鉴一下。
模幂原理可以看这里的介绍:https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/modular-exponentiation
eval函数:https://blog.csdn.net/quanlingtu1272/article/details/95454722
快速模幂:
def modular_exponentiation(base, exponential, mod):
base = base % mod
res = 1
while exponential:
if exponential & 1 == 1: #判断最后一位
res = (res * base) % mod #为1则乘幂
base = (base * base) % mod #累乘记录幂
exponential >>= 1 #右移一位
return res
# (a ^ b) % c = (a%c) ^ (b %c)
a = eval(input("base:"))
b = eval(input("exponential:"))
c = eval(input("mod:"))
print("快速模幂:%d" % (modular_exponentiation(a, b, c)))
正确结果:328384448867,21329413997,132731516131,3733,4673,4815,132835840231,163917216,624329404,361
提交即可,到第五步