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 ...
随机推荐
- VMware如何实现和主机共享网络上网
VMware虚拟机的三种联网方法及原理 一.Brigde--桥接 :默认使用VMnet0 1.原理: Bridge 桥"就是一个主机,这个机器拥有两块网卡,分别处于两个局域网中,同时在& ...
- xor和gates的专杀脚本
前段时间的一次样本,需要给出专杀,应急中遇到的是linux中比较常见的两个家族gates和xor. 首先是xor的专杀脚本,xor样本查杀的时候需要注意的是样本的主进程和子进程相互保护(详见之前的xo ...
- .NET 农码一生
农码一生博文索引 http://www.cnblogs.com/zhaopei/p/Indexes.html 那些年搞不懂的术语.概念:协变.逆变.不变体 http://www.cnblogs.com ...
- HDU 1512 Monkey King ——左偏树
[题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...
- Spring Bean后处理器以及容器后处理器【转】
Bean后处理器:即当spring容器实例化Bean实例之后进行的增强处理. 容器后处理器:对容器本身进行处理,并总是在容器实例化其他任何Bean之前读取配置文件的元数据并可能修改这些数据. 一.Be ...
- C#最简单例子
using System; namespace ConsoleApplication1 { class People { int age; string name; public string get ...
- 后台拼接input 后,动态获取input的值
//前台 <input id=" /> //后台 string text = request.form["text"].toString();
- 葱类 Allium
韭菜 Allium tuberosum (Chinese chives) 韭黄(韭芽):不见光的特殊培养的软化韭菜品种 藠头(薤) Allium chinense (Chinese onion) 蒜 ...
- EL函数以及自定义标签的应用
一.EL函数(调用普通类的静态方法) 编写步骤(自定义EL函数的编写步骤即自定义标签的编写步骤): ①编写一个普通的java类,提供一个静态方法,功能自定,例如下: package cn.wzbril ...
- Java_ClassLoader内存溢出-从tomcat的reload说起
原文链接:http://nius.me/classloader-memory-leak/ 对于j2ee项目,一直使用eclipse的wtp,每次修改代码后,可以自动热部署.简单的项目wtp似乎没什么问 ...