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.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的更多相关文章
- 【LeetCode】372. Super Pow 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/super-po ...
- LeetCode——372. Super Pow
题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...
- 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 ...
- 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,给了几种计算方法:暴力计算法,保存中间结果法(分 ...
- [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 ...
- LeetCode 50. Pow(x, n) 12
50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...
- LeetCode 50 Pow(x, n) (实现幂运算)
题目链接:https://leetcode.com/problems/powx-n/?tab=Description Problem:实现幂运算即 pow(x,n) 设形式为pow(x,n) ...
随机推荐
- Jenkins 邮件配置 || Jenkins 发送邮件失败,提示:Error sending to the following VALID addresses
jenkins---系统管理---系统设置 在Jenkins URL下填写URL链接 在系统管理员邮件地址下填写发件邮箱,这将是以后发送邮件通知的发件人 如果下载了外部邮件通知 配置一下SMTP se ...
- 项目中使用express,只是单纯项目中使用
安装express npm install express --save-dv 建议安装到dev依赖里面 安装body-parse npm install body-parser --save-dev ...
- try catch 场景
面试官:什么情况下用到try-catch?程序员:代码执行预料不到的情况,我会使用try-catch.面试官:什么是预料不到的情况呢?程序员:比如我要计算a除以b,但是b是变量,如果b等于0程序就会出 ...
- 使用Redis分布式锁处理并发,解决超卖问题
一.使用Apache ab模拟并发压测 1.压测工具介绍 $ ab -n 100 -c 100 http://www.baidu.com/ -n表示发出100个请求,-c模拟100个并发,相当是100 ...
- Hdu 2157 How many ways??(DP||矩阵乘法)
How many ways?? Time Limit:1000 MS Memory Limit: 32768 K Problem Description 春天到了, HDU校园里开满了花, 姹紫嫣红, ...
- codecs 1264 芳香数
1264 芳香数 题目描述 Description This question involves calculating the value of aromatic numbers which are ...
- top,free,df,iostat,netstat
服务器程序员除了编写功能之外,若想往上走需要在外网环境打磨很久,以下是行走江湖的制胜法宝. top(综合查看) free(查看内存占用) ps aux|sort -nk6 (查看内存占用具体使用物理内 ...
- windows下powershell的包管理工具
scoop github 开源地址:https://github.com/lukesampson/scoop 安装命令->powershell管理员模式下输入 Invoke-Expression ...
- python中的zip函数的使用
>>> x = [, , ] >>> y = [, , ] >>> z = [, , ] >>> xyz = list(zip( ...
- Note_4.9
2019/4/9 奇奇怪怪的笔记 关于代码,基本上是现写的,可能连编译都过不了 因为是简单算法场,所以就很不走心了昂,/小纠结 图论相关 最小生成树 prim,kruskal 最小生成树的切割性质 ...