题目描述:

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. matlab学习第一天

    >> x=linspace(-,,); >> y=x.^.*sin(x); >> plot(x,y,) >> plot(x,y,) 分析: linspa ...

  2. File类基础

    File类的作用: Java的io包中定义了File类,用于对文件或文件夹的管理操作. File类只能够用于表示文件或文件夹的信息(属性)和对该文件或文件夹的删除创建操作 (不能对内容进行访问) 通过 ...

  3. WPS 表格筛选两列相同数据

    选出B列中的数据是否在A列中出现:用countif 函数,在 B列右侧插入一列,C1输入 = countif 区域中选择 B列的内容区域(选择后加 按F4),条件选择B列所在的内容区域(选择后按F4) ...

  4. DOTA 2 Match History WebAPI(翻译)

    关于DOTA 2 Match History WebAPI 的 源网页地址: http://dev.dota2.com/showthread.php?t=47115 由于源网页全英文,这边做下翻译方便 ...

  5. React Native开发之npm start加速

    在Windows下好不容易安装好React Native环境之后,运行npm start,结果就是无限被等待,快的话160秒(将近3分钟啊....) 而Mac下因为有watchman所以是飞一样的速度 ...

  6. 阿里云分布式关系数据库DRDS笔记

    1.Join左边的表查询数据越少,性能越好 2.广播表作为Join的驱动表 3.SQL的Limit优化 SELECT * FROM t_order o WHERE o.id IN ( SELECT i ...

  7. 李洪强iOS经典面试题144-数据存储

    李洪强iOS经典面试题144-数据存储   数据存储 sqlite中插入特殊字符的方法和接收到处理方法. 除'其他的都是在特殊字符前面加"/",而 ' -> '' .方法:k ...

  8. *HDU3172 并查集

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. C# 禁止修改已装箱了的值类型的字段值,但是可以通过接口的方式实现

    C# 默认是不能修改已装箱了的值类型中字段的值,但是可以通过 值类型实现指定的接口来改变 首先定义一个接口 interface IChange { void Change(int a, int b); ...

  10. win7 hosts文件更改后无效,不生效问题

    通过记事本修改完hosts文件后,域名解析没有生效. 注:可以正常保存修改.服务dnsclient也已经启动 最后发现是hosts文件最后一行不是空行,回车后问题解决