题目描述:

superPow(int a, int[] b),b是一个int数组,每个元素都是正的个位数,组合起来表示一个正整数,例如b=[1,2,3]表示123,求解a^b mod 1337.

思路描述:

本题的难点是当a和b足够大时会造成溢出,因此应考虑其他算法来实现。

理论支持(转幂算法):

(a^b) mod c = ((a mod c)^b) mod c ----公式1

(x*y) mod c = ((x mod c) * (y mod c)) mod c  :积的取余等于取余的积的取余。 -----公式2

基于上述的算法,有:

首先将a对c取余,不妨设余数值为x,则:(a^b) mod c = (x^b)mod c    ---基于式1

(x^b)mod c = (((x ^ (b/2)) mod c) * ((x ^ (b/2)) mod c) ) mod c    :b为偶数

(x^b)mod c = (((x ^ (b/2)) mod c) * ((x ^ (b/2)) mod c) * x) mod c  :b为奇数

----基于式2

基于上述分析,问题解决思路如下: 首先将a对1337取余;然后将数组组成的整数除2,然后带入superPow(a, b/2),递归进行一直到b为0返回。

其中b/2实现比较绕,可以考虑我们初学除法时的步骤,实现算法与之很类似。

leetcode上的AC代码:

public class Solution {
public int superPow(int a, int[] b) {
/*数学公式:
(a^b) mod c = ((a mod c)^b)
(x*y) mod c = ((x mod c)*(y mod c)) mod c :积的取余等于取余的积的取余 */
if(b.length == 0 || isZero(b)){
return 1;
} a = a%1337;
boolean flag = false;
if(b[b.length-1]%2 == 1){
flag = true; //幂次是奇数
}
div(b,2);
int ans = superPow(a,b);
ans = (ans*ans)%1337;
if(flag){
ans = (ans*a)%1337;
} return ans; } boolean isZero(int[] num){// 判断数组组成的整数是否为0
for(int i=num.length-1; i>=0; i--){
if(num[i]>0){
return false;
}
} return true;
} void div(int[] num, int y){ //完成一次数组组成的整数的除法
int tmp = 0;
for(int i=0; i<num.length; i++){
num[i] += tmp*10;
tmp = num[i]%y;
num[i] = num[i]/y;
}
}
}

leetcode Super Pow的更多相关文章

  1. [LeetCode] Super Pow 超级次方

    Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...

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

  3. [LeetCode] 50. Pow(x, n) 求x的n次方

    Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...

  4. LeetCode——372. Super Pow

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

  5. 【LeetCode】372. Super Pow 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/super-po ...

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

  7. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  8. [LeetCode] Super Washing Machines 超级洗衣机

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  9. [Swift]LeetCode372. 超级次方 | Super Pow

    Your task is to calculate ab mod 1337 where a is a positive integer and bis an extremely large posit ...

随机推荐

  1. win10中将默认输入法设置为英文

    开始 设置 时间和语言 区域和语言 语言--中文--选项 微软拼音输入法--选项 IME默认模式--英语

  2. 卸载oracle删除注册表脚本

    一.前言 在我们操作系统中,有时要卸载oracle数据库,每一次都要去删除win下的注册表,为了方便删除注册表的信息,下面通过一种删除注册表快捷的脚本. 二.脚本信息 Windows Registry ...

  3. [NOIP2011]观光公交 题解

    题目大意: 就省了吧 思路: 应该算是贪心. 不难发现,加速只对所有在使用加速器之后连续的一段下车时不用等人的站点下车的人有用.这非常重要. 先算出不加速时的和,并预处理出每个站点最迟到的人的时间.每 ...

  4. android 修改 SwitchPreferenceCompat 高度,内边距,字体大小

    public class FontSizeSwitchPreferenceCompat extends SwitchPreferenceCompat { private Context mContex ...

  5. JDBC连接数据库(数据源的方式)

    在tomcat安装目录下的context.xml文件中配置DataSource <Resource name="jdbc/news"(JNDI的名字,news是数据库的实例名 ...

  6. css基础2

    标准流(normalflow) ◆元素默认的显示方式都是按照标准流的方式显示 ◆块级元素默认独占一行显示===>标准流显示方式 ◆行内元素在一行上显示===>    标准流的显示方式 浮动 ...

  7. Android sdk资源包res里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)

    (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854) (2)drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x ...

  8. java.math.RoundingMode 几个参数详解

    java.math.RoundingMode里面有几个参数搞得我有点晕,现以个人理解对其一一进行总结: 为了能更好理解,我们可以画一个XY轴 RoundingMode.CEILING:取右边最近的整数 ...

  9. WAF攻防研究之四个层次Bypass WAF

    从架构.资源.协议和规则4个层次研究绕过WAF的技术,助于全方位提升WAF防御能力. 绕过WAF的相关技术研究是WAF攻防研究非常重要的一部分,也是最有趣的部分,所以我在写WAF攻防时先写攻击部分.还 ...

  10. javascript学习之运动框架

    模仿新浪博客首页的,最新评论: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"&g ...