372. Super Pow
来源:互联网 发布:北京plc编程培训学校 编辑:程序博客网 时间:2024/06/16 10:21
class Solution(object):
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
s = 1
idx = 0
d = {1:0}
res = [1]
while True:
idx += 1
s = (s * a) % 1337
if s in d:
break
res.append(s)
d[s] = idx
ib = int(''.join(map(str, b)))
idx1 = (ib - d[s]) % (idx - d[s])
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
s = 1
idx = 0
d = {1:0}
res = [1]
while True:
idx += 1
s = (s * a) % 1337
if s in d:
break
res.append(s)
d[s] = idx
ib = int(''.join(map(str, b)))
idx1 = (ib - d[s]) % (idx - d[s])
return res[d[s]+idx1]
https://discuss.leetcode.com/topic/50416/simple-python-solution-by-find-the-loop/2
思路:
1.a的所有幂次与1337取余的结果个数是有限的,所以先把结果保存到一个数组res中。然后根据幂的次数与结果周期取余得到具体幂对应的位置。
2.b为所求幂次,b可能大于结果周期,而周期开始的位置为s所在位置,所以ib-d[s]与周期长度-开始位置的余为基于s位置为起点的步长,而res数据结构是数组,所以最终结果为
res[d[s]+idx1]:
idx1是相对周期起点的长度,
d[s]是周期起点。
阅读全文
0 0
- leetcode.372. Super Pow
- LeetCode 372. Super Pow
- 372. Super Pow
- 372. Super Pow
- [LEETCODE] 372. super pow
- LeetCode--372. Super Pow
- 372. Super Pow
- leetcode 372. Super Pow
- Leetcode 372. Super Pow
- [leetcode] 372. Super Pow
- Leetcode: 372.Super Pow
- 372. Super Pow
- 372. Super Pow(LeetCode)
- 372. Super Pow
- LeetCode 372. Super Pow
- 372. Super Pow
- [LeetCode]372. Super Pow
- 372. Super Pow
- oracle 问题 二
- 开发手记--spinner 下拉控件加小箭头
- Android自动化测试
- 51、构建乘积数组
- Python3.X 爬虫实战(先爬起来嗨)
- 372. Super Pow
- Linux 常用部署命令
- linux系统中安装jdk出现bash: ./java: cannot execute binary file 问题
- 给初学者的RxJava2.0教程(一)
- Java网络爬虫(十一)--使用多线程全面提升爬虫性能
- 软件测试的发展历史
- VOLTE呼叫流程
- STL源码剖析读书笔记4
- Android 控件架构与自定义控件(一)