leetcode Super Pow
题目描述:
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的更多相关文章
- [LeetCode] Super Pow 超级次方
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...
- 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] 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 ...
- LeetCode——372. Super Pow
题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...
- 【LeetCode】372. Super Pow 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/super-po ...
- 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] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] Super Washing Machines 超级洗衣机
You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...
- [Swift]LeetCode372. 超级次方 | Super Pow
Your task is to calculate ab mod 1337 where a is a positive integer and bis an extremely large posit ...
随机推荐
- Java基础知识点3:集合类
集合类是Java编程中经常会用到的一类常用类库,在这里将会对整个集合类进行介绍: Collection接口: Collection接口是所有集合类的根接口,代表了所有含有多个元素的集合,无论这个集合中 ...
- GnuStep使用
gcc -o main main1.m -I/GNUstep/System/Library/Headers -fconstant-string-class=NSConstantString -L/GN ...
- CentOS 6.5 下安装 Redis 2.8.7
wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make ...
- InstallShield制作升级安装包
1. 在General Information里,修改Product Version为新的版本号,此版本号应大于旧的版本号.点击Product Code右面的{...},获取一个新的Product C ...
- 洛谷 P1026 统计单词个数 Label:dp
题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k<=40),且每份中包含的单 ...
- BZOJ4563: [Haoi2016]放棋子
Description 给你一个N*N的矩阵,每行有一个障碍,数据保证任意两个障碍不在同一行,任意两个障碍不在同一列,要求你在 这个矩阵上放N枚棋子(障碍的位置不能放棋子),要求你放N个棋子也满足每行 ...
- word2013删除下载的模板
word2013删除下载的模板 删除步骤: 1)关闭相关的word文档. 2)按照以下的路径找到相应的位置:"%系统根目录%\Users\Administrator\AppData\Roam ...
- java 对象输入输出流
对象的输入输出流的作用: 用于写入对象 的信息读取对象的信息. 对象的持久化. 比如:用户信息. ObjectInputStream : 对象输入流 ...
- Twentydaysgone
这个暑假,在学校呆了一个月,考了两次科目二,被驾校坑了五百块,四级又挂了,段位差点掉到白银 参加二十多天工作室的培训,学长直接带着做itheima的某某安全卫士,跟着视频也迷迷瞪瞪敲完了代码,一知半解 ...
- elasticsearch运维实战之2 - 系统性能调优
elasticsearch性能调优 集群规划 独立的master节点,不存储数据, 数量不少于2 数据节点(Data Node) 查询节点(Query Node),起到负载均衡的作用 Linux系统参 ...