题目描述:

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

思路描述:

本题的难点是当a和b足够大时会造成溢出,因此应考虑其他算法来实现。

理论支持(转幂算法):

(a^b) mod c = ((a mod c)^b) mod c ----公式1

(x*y) mod c = ((x mod c) * (y mod c)) mod c  :积的取余等于取余的积的取余。 -----公式2

基于上述的算法,有:

首先将a对c取余,不妨设余数值为x,则:(a^b) mod c = (x^b)mod c    ---基于式1

(x^b)mod c = (((x ^ (b/2)) mod c) * ((x ^ (b/2)) mod c) ) mod c    :b为偶数

(x^b)mod c = (((x ^ (b/2)) mod c) * ((x ^ (b/2)) mod c) * x) mod c  :b为奇数

----基于式2

基于上述分析,问题解决思路如下: 首先将a对1337取余;然后将数组组成的整数除2,然后带入superPow(a, b/2),递归进行一直到b为0返回。

其中b/2实现比较绕,可以考虑我们初学除法时的步骤,实现算法与之很类似。

leetcode上的AC代码:

public class Solution {
public int superPow(int a, int[] b) {
/*数学公式:
(a^b) mod c = ((a mod c)^b)
(x*y) mod c = ((x mod c)*(y mod c)) mod c :积的取余等于取余的积的取余 */
if(b.length == 0 || isZero(b)){
return 1;
} a = a%1337;
boolean flag = false;
if(b[b.length-1]%2 == 1){
flag = true; //幂次是奇数
}
div(b,2);
int ans = superPow(a,b);
ans = (ans*ans)%1337;
if(flag){
ans = (ans*a)%1337;
} return ans; } boolean isZero(int[] num){// 判断数组组成的整数是否为0
for(int i=num.length-1; i>=0; i--){
if(num[i]>0){
return false;
}
} return true;
} void div(int[] num, int y){ //完成一次数组组成的整数的除法
int tmp = 0;
for(int i=0; i<num.length; i++){
num[i] += tmp*10;
tmp = num[i]%y;
num[i] = num[i]/y;
}
}
}

leetcode Super Pow的更多相关文章

  1. [LeetCode] Super Pow 超级次方

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

  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] 50. Pow(x, n) 求x的n次方

    Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...

  4. LeetCode——372. Super Pow

    题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...

  5. 【LeetCode】372. Super Pow 解题报告(Python)

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

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

  7. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  8. [LeetCode] Super Washing Machines 超级洗衣机

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  9. [Swift]LeetCode372. 超级次方 | Super Pow

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

随机推荐

  1. ajax 中一些常用的操作

    $.post.$.get是一些简单的方法,如果要处理复杂的逻辑,还是需要用到jQuery.ajax() 一.$.ajax的一般格式$.ajax({ type: 'POST', url: url , d ...

  2. ViewPager图片切换的简单案例

    1)ViewPager类直接继承了ViewGroup类,所有它是一个容器类,可以在其中添加其他的view类. 2)ViewPager类需要一个PagerAdapter适配器类给它提供数据. 向右滑动切 ...

  3. 【Ajax 基础学习】

    http://www.cnblogs.com/guduoduo/p/3681296.html 今天简单的学习了 Ajax 的基础知识,总结在这里.部分代码不是原创,特此说明. [Ajax 简介] AJ ...

  4. Skype无法收发组消息

    我用微软账户登录的Skype 发现无法收发组消息  -  提示发送消息不可用 卸了重装  -  提示 "无法发送消息, 请尝试获取最新的消息版本, 或者是组内成员使用旧版本无法同时视频和发送 ...

  5. compass color 颜色 对比色[Sass和compass学习笔记]

    最基本的api 是对比色,对与我这种菜鸟来说,没有什么比在一个背景色下 用什么颜色的文字坑蛋疼的事情了,这个工具可以帮助大家很好解决这个问题 api 地址 http://compass-style.o ...

  6. Git 常用命令行

    最近在公司的服务器上安装了Git Sever,开始从SVN转向到Git了,整理了一些在Git常用的命令. 取得Git仓库 初始化一个版本仓库 git initClone远程版本库 git clone ...

  7. 移动端lCalendar纯原生js日期时间选择器

    网上找过很多的移动端基于zepto或jquery的日期选择器,在实际产品中也用过一两种,觉得都不太尽如人意,后来果断选择了H5自己的日期input表单,觉得还可以,至少不用引用第三方插件了,性能也不错 ...

  8. 最难面试的IT公司之ThoughtWorks代码挑战——FizzBuzzWhizz游戏(C#解法)

    原题 看到那么多人看到前面这么糟粕的代码各种不忍直视后,楼主还是把最终实现放在页首吧.             Console.WriteLine("说出三个不同的特殊数,用','隔开 &q ...

  9. 关于malloc函数的动态分配问题

    malloc函数动态分配了一个整型的内存空间,让abc都指向刚申请的空间,所以只有最后一个赋值语句的值保留在了空间里 #include<stdio.h> main() { int *a,* ...

  10. centeros bash: ifconfig: command not found

    如果ifconfig命令不存在 yum upgrade yum install net-tools