LeetCode——372. Super Pow
题目链接: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的更多相关文章
- 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 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】372. Super Pow 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/super-po ...
- 372 Super Pow 超级次方
你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出.示例 1:a = 2b = [3]结果: 8示例 2:a = 2b = [1,0]结果: 102 ...
- 372. Super Pow
问题 Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large p ...
- 372. Super Pow.txt
▶ 指数取模运算 ab % m ▶ 参考维基 https://en.wikipedia.org/wiki/Modular_exponentiation,给了几种计算方法:暴力计算法,保存中间结果法(分 ...
- C#版(击败100.00%的提交) - Leetcode 372. 超级次方 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- [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 Super Pow
题目描述: superPow(int a, int[] b),b是一个int数组,每个元素都是正的个位数,组合起来表示一个正整数,例如b=[1,2,3]表示123,求解a^b mod 1337. 思路 ...
随机推荐
- c++学习书籍推荐《C标准库(英文版)》下载
<C标准库(英文版)>是由世界级C语言专家编写的C标准库经典著作,影响了几代程序员. <C标准库(英文版)>集中讨论了C标准库,全面介绍了ANSI/ISO C语言标准的所有库函 ...
- ASP.NET MVC ActionFilterAttribute 方法解释(区别)
1.OnActionExecuting 在Action方法调用前使用,使用场景:如何验证登录等. 2.OnActionExecuted 在Action方法调用后,result方 ...
- python面向过程编程小程序- 模拟超市收银系统
6.16自我总结 功能介绍 程序功能介绍: 商品信息再读取修改买卖均已xlsx格式 且生成购物记录也按/用户名/购买时间.xlsx格式生成 账号密码输入错误三次按照时间进行冻结 用户信息已json格式 ...
- 6.2.初识Flutter应用之路由管理
路由管理 路由(Route)在移动开发中通常指页面(Page),这跟web开发中单页应用的Route概念意义是相同的,Route在Android中通常指一个Activity,在iOS中指一个ViewC ...
- [剑指offer] 10. 旋转数组的最小数字
题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 思路: 利用dp[i]保存盖2*i的矩形有多少种办法. 通过 ...
- 关于C#多线程、易失域、锁的分享
一.多线程 windows系统是一个多线程的操作系统.一个程序至少有一个进程,一个进程至少有一个线程.进程是线程的容器,一个C#客户端程序开始于一个单独的线程,CLR(公共语言运行库)为该进程创建了一 ...
- linux初学者-文件权限
linux初学者-文件权限 lunix系统都是以文件的形式存在,自然而然的就会要求不同的用户拥有不同的权限,这也是系统能够运行的根本保证,下文将对文件的权限管理进行简要的介绍. 1.文件属性的查看 - ...
- 小白开学Asp.Net Core 《十》
小白开学Asp.Net Core <十> — — Session.Cookie.Cache(老生常谈) 一.背景 在常谈Session和Cookie之前我们先来简单的了解下Http(可以说 ...
- 微信小程序的尺寸单位rpx介绍
rpx单位是微信小程序中css的尺寸单位,rpx可以根据屏幕宽度进行自适应. 规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则750rpx = 375 ...
- jQuery的核心思想
jQuery?----www.jQuery.com jQuery的理念:write less, do more jQuery的成就:世界排名前100的公司,46%都在使用jQuery,远远超过其他库, ...