作者: 负雪明烛
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. 多选项、多个选择项【c#】

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="AddDataInfoCe ...

  2. javascript的原型与原型链

    首先套用一句经典名言,JavaScript中万物皆对象. 但是对象又分为函数对象和普通对象. function f1(){}; var f2=function(){}; var f3=new Func ...

  3. acquaint

    Interpersonal relationships are dynamic systems that change continuously during their existence. Lik ...

  4. 乱序拼图验证的识别并还原-puzzle-captcha

    一.前言 乱序拼图验证是一种较少见的验证码防御,市面上更多的是拖动滑块,被完美攻克的有不少,都在行为轨迹上下足了功夫,本文不讨论轨迹模拟范畴,就只针对拼图还原进行研究. 找一个市面比较普及的顶像乱序拼 ...

  5. 【leetcode】598. Range Addition II

    You are given an m x n matrix M initialized with all 0's and an array of operations ops, where ops[i ...

  6. C++ 素数对猜想

    我的解法是先将2到n的所有素数全部列出来,再计算.将全部的素数列出来用了一个叫"埃拉托色尼筛法"的方法. 算法参照这里:https://www.sohu.com/a/2526745 ...

  7. 基本类型、引用类型NPE异常

    1.null是Java中的关键字,像public.static.final.它是大小写敏感的,你不能将null写成Null或NULL,编译器将不能识别它们然后报错. 2.就像每种原始类型都有默认值一样 ...

  8. Linux基础命令---httpd守护进程

    httpd httpd是apache超文本传输协议的主程序,它被设计成一个独立运行的守护进程.httpd会建立一个线程池来处理http请求. 此命令的适用范围:RedHat.RHEL.Ubuntu.C ...

  9. 【Java 基础】Java日期格式问题

    1. Use SimpleDateFormat to format Date. Watch out, SDF is NOT THREAD-SAFE, it might not be important ...

  10. request.getRequestDispatcher()和response.sendRedirect()区别

    一.request.getRequestDispatcher().forward(request,response): 1.属于转发,也是服务器跳转,相当于方法调用,在执行当前文件的过程中转向执行目标 ...