作者: 负雪明烛
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)的更多相关文章

  1. LeetCode——372. Super Pow

    题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...

  2. 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 ...

  3. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  4. 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 ...

  5. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  6. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  7. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  8. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  9. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

随机推荐

  1. 嵌入式Linux利用ppp实现4G模块联网

    https://blog.csdn.net/qq361294382/article/details/52136126 https://blog.csdn.net/qq361294382/article ...

  2. A Child's History of England.12

    Dunstan, Abbot of Glastonbury Abbey, was one of the most sagacious of these monks. He was an ingenio ...

  3. vue3 使用 data、computed、methods

    简单数据ref复杂数据reactive 使用方法: // useCount.js import {ref,reactive,computed} from 'vue' export default fu ...

  4. redis入门到精通系列(九):redis哨兵模式详解

    (一)哨兵概述 前面我们讲了redis的主从复制,为了实现高可用,会选择一台服务器作为master,多台服务器作为slave.现在有这样一种情况,master宕机了,这时系统会选择一台slave作为m ...

  5. 重量级&轻量级

    重量级 就是说包的大小,还有就是与个人项目的耦合程度,重量级的框架与项目耦合程度大些 代表EJB容器的服务往往是"买一送三",不要都不行 轻量级 就是相对较小的包,当然与项目的耦合 ...

  6. springboot-devtools实现项目的自动重启

    热部署的引入依赖: <!-- 热部署 --> <dependency> <groupId>org.springframework.boot</groupId& ...

  7. Js和Thymeleaf如何获取model中的值

    一.Jquery获取Model中的数据 1.将model中的值赋给hidden,然后Js获取隐藏域的值. 后台的实现: @RequestMapping("/QEditorMod1" ...

  8. 将前端请求中的数据绑定到Spring MVC响应方法中参数的四种方法

    一.映射URL绑定的占位符到方法参数 1.方法 使用@PathVariable注解 2.代码示例 a.接收请求方法 @RequestMapping(value = "/deleteInfo/ ...

  9. binlog浅析

    binlog浅析 一.基础知识 什么是binlog? (图一) 全称:Binary Log (二进制日志),包含描述数据库更改的" 事件 ",例如表创建操作或对表数据的更改.二进制 ...

  10. 7、Redis五大数据类型---集合(Set)

    一.集合(Set)简介 Set是string类型的无序集合.集合成员是唯一的,这就意味着集合中不能出现重复的数据. Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1). ...