50. Pow(x, n)

372. Super Pow

https://www.cnblogs.com/grandyang/p/5651982.html

https://www.jianshu.com/p/b256bd531df0

做这个题之间先了解两个公式:

公式一:a^b mod c = (a mod c)^b mod c
公式二:(ab) mod c = (a mod c)(b mod c) mod c

这道题题让我们求一个数的很大的次方对1337取余的值,即a^b mod 1337。输入的b是一个数组,且每一个数组代表一位,所以将求次方转换为223 = (22)10 * 23这种形式。

利用公式二将原式转化,即223 mod 1337 = ((22)10 mod 1337)*(23 mod 1337)mod 1337。

class Solution {
public:
int superPow(int a, vector<int>& b) {
int res = ;
for(int i = ;i < b.size();i++){
res = pow(res,) * pow(a,b[i]) % ;
}
return res;
}
int pow(int a,int n){
if(n == )
return ;
if(n == )
return a % ;
return pow(a % ,n/) * pow(a % ,n - n/) % ;
}
};

leetcode 50. Pow(x, n) 、372. Super Pow的更多相关文章

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

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

  2. LeetCode——372. Super Pow

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

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

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

  8. LeetCode 50. Pow(x, n) 12

    50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...

  9. LeetCode 50 Pow(x, n) (实现幂运算)

    题目链接:https://leetcode.com/problems/powx-n/?tab=Description   Problem:实现幂运算即 pow(x,n)   设形式为pow(x,n)  ...

随机推荐

  1. 浅谈CopyOnWriteArraySet

    CopyOnWriteArraySet结构图   CopyOnWriteArraySet.png CopyOnWriteArraySet主要方法 public boolean add(E e); pu ...

  2. RNN、LSTM介绍以及梯度消失问题讲解

    写在最前面,感谢这两篇文章,基本上的框架是从这两篇文章中得到的: https://zhuanlan.zhihu.com/p/28687529 https://zhuanlan.zhihu.com/p/ ...

  3. 在linux上搭建SVN服务器并自动更新至WEB目录

    1.仓库放在 /var/svn/ 目录下,并且仓库名为 project 2.创建用户组user,该组下添加两个成员user1.user2,密码直接用用户名,两用户可以checkout代码和提交代码 3 ...

  4. canvans知识点

    1.绘制圆的角度示意图: 2 倒计时中,时钟数字的渲染逻辑: 3 直线边缘样式的设置 context.lineCap = "butt"; context.lineCap = &qu ...

  5. dede5.7评论框不显示,文章页表情不显示,解决办法

    dede5.7评论框不显示,文章页表情不显示,解决办法 进入dede后台,系统-系统基本参数-其它选项,找到模板引擎禁用标签,去掉里面的php,如图 进入dede模板目录,默认是/templets/d ...

  6. export的几种用法

    记录一下export的几种写法. 0.入口文件为index.js,引用add-content.js的内容 1.  export default 方式,直接导出变量 add-content.js的内容如 ...

  7. [NgRx] NgRx Entity Adapter Configuration - Understanding sortComparer and selectId

    import { Course, compareCourses } from "../model/course"; import { EntityState, createEnti ...

  8. 通过类型断言获取error类型,获得更详细的信息

    package main import ( "fmt" "os" ) func main() { f, err := os.Open("/test.t ...

  9. 7中漂亮的纯css字体

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. mac webstorm 安装破解

    下载: 链接:https://pan.baidu.com/s/1A1afhcpPWMrQtOr1Suqs-g  密码:5r7b 激活码 K6IXATEF43-eyJsaWNlbnNlSWQiOiJLN ...