【LeetCode】372. Super Pow 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/super-pow/description/
题目描述:
Your task is to calculate a^b
mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example 1:
Input: a = 2, b = [3]
Output: 8
Example 2:
Input: a = 2, b = [1,0]
Output: 1024
题目大意
实现a的b次方的函数。但是给出的b是超级巨大的,而且是用数组保存着每一位的。
解题方法
这个题是50. Pow(x, n)的拓展题,都是求幂的问题,但是这个题由于数值大,需要模1337,对于模什么数一般都是瞎选的,不用考虑这个题为什么模这个数。
我觉得这个题的难点在于,如何求数组表示的超级大的数字b次幂。原来是求幂也可以做展开,比如求223,相当于求(22)^10 * (2^3).也就是说把前面以求的结果求一次10次幂,然后再去求后面的幂。
注意这里每次计算的结果都要%1337,保留后面的部分。前面被模掉的部分对结果不影响的,所以不用顾虑太多,直接求模。
时间复杂度是O(N),空间复杂度是O(1)。N是b的长度。
class Solution(object):
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
res = 1
for x in b:
res = self.pow(res, 10) * self.pow(a, x) % 1337
return res
def pow(self, a, b):
if b == 0 or a == 1: return 1
if b % 2:
return a * self.pow(a, b - 1) % 1337
return self.pow((a * a) % 1337, b / 2) % 1337
参考资料:
http://www.cnblogs.com/grandyang/p/5651982.html
日期
2018 年 10 月 7 日 —— 假期最后一天!!
【LeetCode】372. Super Pow 解题报告(Python)的更多相关文章
- LeetCode——372. Super Pow
题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...
- Leetcode 372. Super Pow
使用公式 c = ab => c mod d = [a mod d * b mod d] mod d 所以a^423 mod d = (a^100)^4 * (a ^10)^2 * a^3 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- leetcode 50. Pow(x, n) 、372. Super Pow
50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
随机推荐
- 68-Binary Tree Postorder Traversal
Binary Tree Postorder Traversal My Submissions QuestionEditorial Solution Total Accepted: 97358 Tota ...
- idea数据库报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
通过idea操作数据库,进行数据的增加,运行时报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 原因:没有导入mysql-connec ...
- 大规模 K8s 集群管理经验分享 · 上篇
11 月 23 日,Erda 与 OSCHINA 社区联手发起了[高手问答第 271 期 -- 聊聊大规模 K8s 集群管理],目前问答活动已持续一周,由 Erda SRE 团队负责人骆冰利为大家解答 ...
- 3.5 Rust Generic Types, Traits, and Lifetimes
Every programming language has tools for effectively handling the duplication of concepts. In Rust, ...
- Linux基础命令---mysqlimport导入数据库
mysqlimport mysqlimport指令可以用来将文本文件中的数据导入到数据库.在导入文本文件的时候,必须确保数据库中有一张表,而且他的名字和文本文件的名字是一样的. 此命令的适用范围:Re ...
- ssm动态查询向前台传json
1.数据协议层 public User selectById(Integer id);//通过id值查询用户 2.数据层 <select id="selectById" re ...
- Log4j 被曝核弹级漏洞,开发者炸锅了!
大家好,我是鱼皮,开门见山,知名的开源项目 Apache Log4j 出事了! 2021 年 12 月 9 日,该项目被曝存在 严重安全漏洞 ,攻击者只需要向目标机传入一段特殊代码,就能触发漏洞,自由 ...
- Mysql资料 存储索引
- Jenkins pipeline声明式语法
目录 一.最简结构 二.简单例子 一.最简结构 pipeline { agent any stages { stage('pull') { st ...
- react的diff算法与antd中switch组件不更新问题
问题描述: 现在有个需求,现有一个列表table,里面的数据有启用的也有关闭的,switch组件会根据数据状态展示,同时进行排序,启用数据在前面,未启用的在后面.如图 然后现在需要操作,假如我将第四条 ...