题目链接: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. Java项目案例之---计算公司员工的工资(面向对象复习)

    计算公司员工的工资(面向对象复习) 某公司的雇员分为以下若干类: Employee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份.方法:double getSalary(int month) ...

  2. GitHub & Git 的学习之始

    唉,简单地说,感受只有四个字:蓝瘦香菇. 我的GitHub地址为: https://github.com/LinJingYun  (这个,,我不知道具体从哪里找到自己地址啊) 接下来说一下我对git和 ...

  3. EnjoyingSoft之Mule ESB开发教程系列第五篇:控制消息的流向-数据路由

    目录 1. 使用场景 2. 基于消息头的路由 2.1 使用JSON提交订单的消息 2.2 使用XML提交订单的消息 2.3 使用Choice组件判断订单格式 3. 基于消息内容的路由 4. 其他控制流 ...

  4. py+selenium运行时报错Can not connect to the Service IEDriverServer.exe

    问题: 运行用例时,出现报错(host文件已加入127.0.0.1 localhost): raise WebDriverException("Can not connect to the ...

  5. [算法]Python判断一个点是否在多边形内部

    题目: 代码: # -*- coding:utf-8 -*- def rayCasting(p, poly): px = p['x'] py = p['y'] flag = False i = 0 l ...

  6. 个人永久性免费-Excel催化剂功能第40波-工资、年终奖个人所得税计算函数

    学Excel的表哥表姐们必定有接触过个人所得税的案例学习,在计算个人所得税这个需求上,大家的层次也是很多种多样,当然Excel催化剂推荐的方式仍然是经过封装后的简单明了的自定义函数的方式,此篇已为财务 ...

  7. 关于C#调用WebServices的方法

    2018-1-22 前情是我使用vs在引用高通的webservice时出现了下载错误导致无法引用这个服务,先是在网上查询了这个错误的问题及解决方案,将这个问题与解决方法发给了高通同事,可惜的是他也不清 ...

  8. C#命名规范(简述)

    命名空间,类,事件,接口,常量,属性,方法使用Pascal命名,即首字母大写  参数,变量(类字段)使用camel命名法,即首字母小写. Pascal 方式--所有单词第一个字母大写,其他字母小写.  ...

  9. js异步解决方法

    在浏览器端,耗时很长的操作都应该异步执行,避免浏览器失去响应,最好的例子就是Ajax操作.在服务器端,"异步模式"甚至是唯一的模式,因为执行环境是单线程的,如果允许同步执行所有ht ...

  10. JavaScript-基本语法和数据类型

           前奏:在HTML中使用JavaScript 1_推荐src引用外部JavaScript文件,方便管理与维护,标签位置在页面最下面,使浏览器更优先加载页面内容. 2_HTML页面需要有标准 ...