Leetcode 372.超级次方
超级次方
你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出。
示例 1:
输入: a = 2, b = [3]
输出: 8
示例 2:
输入: a = 2, b = [1,0]
输出: 1024
解题思想
这道题需要计算 a^b % c 的值,其中b非常的大,大到只能使用数组来表示。这道题是ACM里面常见的快速幂的解题方式,这其中有一个数学的推论,可以看我代码里附带的那个解释。
总之,这个公式的意思就是,(a*b)%c=(a%c)*(b%c),因此我们可以在每一步计算结果之后都这么处理,防止溢出。
第二个算法部分其实很容易理解,就是可以做类似于二分的分割,比如当b是偶数的时候,我们可以转化为计算a^(b/2)后再平方,而对于基础,则再乘一个a就可以,总之你看代码就知道了
public class Solution {
// 判断是否大于0
public static boolean morethanzero(int[] x){
for(int i=x.length-1;i>=0;i--){
if(x[i]>0)
return true;
}
return false;
}
//高精度除法
public static void div(int[] x,int y){
int tmp=0;
for(int i=0;i<x.length;i++){
x[i] += tmp*10;
tmp = x[i] % y;
x[i] = x[i] /y;
}
}
public static int superPow(int a, int[] b) {
if (morethanzero(b) == false)
return 1;
a=a%1337;
boolean isEven = false;
if(b[b.length-1] % 2 == 0)
isEven = true;
div(b,2);
int result = superPow(a,b);
result = result % 1337;
result*=result;//result由于div分成了两部分,现在把两部分合在一起
result = result % 1337;
if(isEven==false){
result*=a;//奇数的话,再乘以a
result = result % 1337;
}
return result;
}
}
Leetcode 372.超级次方的更多相关文章
- C#版(击败100.00%的提交) - Leetcode 372. 超级次方 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- Java实现 LeetCode 372 超级次方
372. 超级次方 你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出. 示例 1: 输入: a = 2, b = [3] 输出: 8 示例 2: ...
- [LeetCode] Super Pow 超级次方
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...
- 372 Super Pow 超级次方
你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出.示例 1:a = 2b = [3]结果: 8示例 2:a = 2b = [1,0]结果: 102 ...
- LeetCode 372
题目: Your task is to calculate a^b mod 1337 where a is a positive integer and b is an extremely large ...
- [Swift]LeetCode372. 超级次方 | Super Pow
Your task is to calculate ab mod 1337 where a is a positive integer and bis an extremely large posit ...
- Leetcode 517.超级洗衣机
超级洗衣机 假设有 n 台超级洗衣机放在同一排上.开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的. 在每一步操作中,你可以选择任意 m (1 ≤ m ≤ n) 台洗衣机,与此同时将每台洗衣机 ...
- Leetcode 313.超级丑数
超级丑数 编写一段程序来查找第n个超级丑数. 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数. 示例: 输入: n = 12, primes = [2,7,13,19] ...
- LeetCode——372. Super Pow
题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...
随机推荐
- dispaly:none 和visibility :hidden的区别
display:none 通常被 JavaScript 用来在不删除元素的情况下隐藏或显示元素. 它和 visibility 属性不一样.把 display 设置成 none 元素不会占据它本来应该显 ...
- 一行JS搞定快速关机
一.在本地新建一个文件js文件 JS代码: (new ActiveXObject("Shell.Application")).ShutdownWindows(); 二.设置快捷键 ...
- c语言中的->代表什么意思
c语言中 ->符号是什么意思? 比如c=a->b a为结构体或联合体的指针,->表示调用其成员
- Yii2.0数据格式器
平时我们在写代码中,总是要写一个单独的文件来全局处理常用的数据格式.Yii2.0却很人性化,为我们内置了一套数据格式器. 1.格式化日期和时间 Yii::$app->formatter-> ...
- 洛谷 P1181 数列分段Section I(水题日常)
题目描述 对于给定的一个长度为N的正整数数列A[i],现要将其分成连续的若干段,并且每段和不超过M(可以等于M),问最少能将其分成多少段使得满足要求. 输入输出格式 输入格式: 输入文件divide_ ...
- codevs 1277 生活大爆炸 2012年CCC加拿大高中生信息学奥赛
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description Sheldon and Leonard are physicists wh ...
- C#中加锁问题
今天在工作中遇到了一个问题 当我使用多线程访问同一个方法资源时,为了不对结果进行冲突于是加了个死锁,还遇到了一些坑,特此来进行一些记录 static object obj=new object(); ...
- Mybatis generator自动生成代码包括实体,dao,xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- idea下使用码云插件进行git提交
1)下载插件 file->setting->plugins->右侧搜索gitee->安装->重启ide 2)配置版本控制 file->setting->Ver ...
- springboot上传linux文件无法浏览,提示404错误
1.配置文件地址置换 @Componentclass WebConfigurer implements WebMvcConfigurer { @Autowired ConfigUtil bootdoC ...