题目链接:https://leetcode.com/problems/super-pow/description/

Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.

Example1:

a =
b = [] Result:

Example2:

a =
b = [,] Result:

计算一个a的b次幂,然后对1337取模。

刚开始没注意取模直接看Example就写了程序:

static int superPow(int a, int[] b) {
int sub = 1,mod = 0,ex = 1;
for(int i = b.length - 1; i >= 0; i--){
mod += (b[i] * ex);
ex *= 10;
}
System.out.println(mod);
for(;mod > 0; mod--){
sub = sub * a;
}
return sub;
}

本来以为直接对结果取模就行,但其实是会出现很大的问题的,因为题目说了这个a将是一个很大的数字,假如a的b次幂大于int的范围就会出错,所以不能在最后的结果取模。

用到的数学公式:a ^ b % p = ((a % p)^b) % p

所以第二次修改就是:

static int superPow(int a, int[] b) {
int sub = 1,mod = 0,ex = 1;
for(int i = b.length - 1; i >= 0; i--){
mod += (b[i] * ex);
ex *= 10;
}
a = a%1337;
System.out.println(mod);
for(;mod > 0; mod--){
sub = sub * a;
}
return sub%1337;
}

这里一开始就对a进行了取模运算,然后在求幂,最后再进行一次取模运算,得到结果。

然后还有问题,就是在求幂的时候也会超出范围。所以在求幂的时候也要注意不要超出范围。

这次用到的公式就是:(a * b) % p = (a % p * b % p) % p

所以第三次修改就是:

static int superPow(int a, int[] b) {
int sub = 1,mod = 0,ex = 1;
for(int i = b.length - 1; i >= 0; i--){
mod += (b[i] * ex);
ex *= 10;
}
a = a%1337;
System.out.println(mod);
for(;mod > 0; mod--){
sub = sub%1337 * a;
}
return sub%1337;
}

到目前为止幂的运算一直都没问题,一直忽略了b数组的读取,万一b数组代表的数是大于int的范围也同样会出错。

这里使用的公式大体就是:

a^123 = (a^3)^1*(a^2)^10*(a^1)^100;

class Solution {
public int superPow(int a, int[] b) {
int sub = 1, j = 1;
for(int i = b.length - 1; i >= 0; i--) {
sub *=(Pow(Pow(a, b[i]), j))%1337;
j *= 10;
}
return sub%1337;
}
int Pow(int a, int k) {
int sub = 1;
if(k == 0) return 1;
a = a%1337;
for(;k > 0; k--){
sub = (sub %1337) * a;
}
return sub%1337;
}
}

LeetCode——372. Super Pow的更多相关文章

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

  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】372. Super Pow 解题报告(Python)

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

  4. 372 Super Pow 超级次方

    你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出.示例 1:a = 2b = [3]结果: 8示例 2:a = 2b = [1,0]结果: 102 ...

  5. 372. Super Pow

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

  6. 372. Super Pow.txt

    ▶ 指数取模运算 ab % m ▶ 参考维基 https://en.wikipedia.org/wiki/Modular_exponentiation,给了几种计算方法:暴力计算法,保存中间结果法(分 ...

  7. C#版(击败100.00%的提交) - Leetcode 372. 超级次方 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

  8. [LeetCode] Super Pow 超级次方

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

  9. leetcode Super Pow

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

随机推荐

  1. 串门赛: NOIP2016模拟赛——By Marvolo 丢脸记

    前几天liu_runda来机房颓废,顺便扔给我们一个网址,说这上面有模拟赛,让我们感兴趣的去打一打.一开始还是没打算去看一下的,但是听std说好多人都打,想了一下,还是打一打吧,打着玩,然后就丢脸了. ...

  2. 20131228-sql命令

    开始 --cmd net start mssqlservernetnet stop mssqlserver

  3. Element ui colorpicker在Vue中的使用

    首先要有一个color-picker组件 <el-color-picker v-model="headcolor"></el-color-picker> 在 ...

  4. python3.5学习笔记(第六章)

    本章内容: 正则表达式详解(re模块) 1.不使用正则表达式来查找文本的内容 要求从一个字符串中查找电话号码,并判断是否匹配制定的模式,如:555-555-5555.传统的查找方法如下: def is ...

  5. Eclipse中Cannot nest src folder解决方法

    错误示例: : Java Model Status [Cannot nest output folder 'xxx/bin/main' inside output folder 'xxx/bin'] ...

  6. 《ElasticSearch6.x实战教程》之父-子关系文档

    第七章-父-子关系文档 打虎亲兄弟,上阵父子兵. 本章作为复杂搜索的铺垫,介绍父子文档是为了更好的介绍复杂场景下的ES操作. 在非关系型数据库数据库中,我们常常会有表与表的关联查询.例如学生表和成绩表 ...

  7. JAVA面试题 请谈谈你对Sychronized关键字的理解?

    面试官:sychronized关键字有哪些特性? 应聘者: 可以用来修饰方法; 可以用来修饰代码块; 可以用来修饰静态方法; 可以保证线程安全; 支持锁的重入; sychronized使用不当导致死锁 ...

  8. Linux AUFS 文件系统

    AUFS 的英文全称为 Advanced Mult-Layered Unification Filesystem,曾经是 Another Mult-Layered Unification Filesy ...

  9. Servlet和JSP知识总结

    1.Servlet接口有哪些方法及Servlet生命周期 Servlet接口定义了5个方法,前三个方法与Servlet生命周期有关: void init() void service() void d ...

  10. 小白学python-day02-二进制、计算机单位、编程语言分类介绍、

    今天是第二天,以下是学习内容总结. 但行努力,莫问前程. ----------------------------------------------------------------------- ...