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. 思路 ...
随机推荐
- 性能测试即服务-docker部署jmeter及.netcore应用
前言 现在各种业务都追求上云,通俗的讲,“XX即服务”,作为一名专职的性能测试调优人员的我,由于会点三脚猫的开发功夫,“性能测试即服务”这种开发大任就落到我头上了,先做一个能完成核心压测功能的基础版. ...
- POI 身份证号码 手机号 日期值的处理方式
private static SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ...
- Vs连接Mysql数据库
Vs连接Mysql数据库步骤 1. 首先下载mysql数据库,安装,建库建表 https://www.yiibai.com/mysql/getting-started-with-mysql-store ...
- div+css 布局技巧总计
一.css 样式 1.float 首先需要了解块级元素(block element).每个块级元素都默认占用一行,在同一行只能添加一个块元素(float 除外).块级元素一般可以嵌套块级元素或者行内元 ...
- VUE-CLI3.0安装和使用echart方法
在Vue中使用echarts的两种方式 npm webpack vue-cli echarts vue.js 准备:使用vue-cli脚手架 如果你已经有自己的项目,可以跳过这一步. npm下载v ...
- 浅入深出Vue:发布项目
项目完成之后,当然不能满足于在我们的开发环境下跑一跑.我们可以打包发布到服务器上,让大家一起来欣赏一下你的作品. 那么 vue 项目如何打包发布呢,新建的项目目录下通常都有一个 README.md 的 ...
- STM32F072从零配置工程-自定义时钟配置详解
从自己的板子STM32F407入手,参考官方的SystemInit()函数: 核心在SetSysClock()这个函数,官方默认是采用HSE(设定为8MHz)作为PLL锁相环的输入输出168MHz的S ...
- acm 模板
Index 分类细则 说起分类准则,我也是很头疼,毕竟对于很多算法,他并不是单调的,而是多方面的都挂得上钩.所以,从始至终,分类准则一直都是我很纠结的问题. 经过思量,首先分出比较主流的几类:Numb ...
- [leetcode] 905. Sort Array By Parity [easy]
原题链接 很水的一道题,就是数组内部交换. 水题就想着减少复杂度嘛,于是学到一种交换写法. class Solution { public: vector<int> sortArrayBy ...
- [git] 基础命令笔记
--内容整理自廖雪峰的GIT教程-- git status 查看当前工作区状态,显示未跟踪的文件以及未上传的修改记录 git init 使当前文件夹变成Git可以管理的仓库 git add xxx 将 ...