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. ISCC之web2

    Php代码审计 PHP代码 <?php error_reporting(0); require 'flag.php'; $value = $_GET['value']; $password = ...

  2. c#中关于textbox换行

    要让一个Windows Form的TextBox显示多行文本就得把它的Multiline属性设置为true. 这个大家都知道,可是当你要在代码中为Text属性设置多行文本[的时候可能会遇到点麻烦:) ...

  3. python 程序练习题

    1.实现isOdd(),参数为整数,如果整数为奇数,返回True,否则返回Flase 代码如下: def isOdd(a): if a%2==0: return False else: return ...

  4. python之钉钉机器人zabbix报警

    转自:https://blog.51cto.com/m51cto/2051945 首先在钉钉群聊里添加一个自定义的机器人 并复制webhook的内容 https://oapi.dingtalk.com ...

  5. oracle 查询表重复数据 并 删除保留一条

    语法:select count(字段名),字段名  from  表名 group by 字段名 having count(字段名)>1 实例: select  count(name),name ...

  6. python 不能加载pip install的site-package文件

    python -m pip  install tensorflow-gpu==1.0.1 -i https://pypi.tuna.tsinghua.edu.cn/simple/

  7. Hive 数据类型与文件格式

    一.基本数据类型 1.基本数据类型 Tinyint  1byte有符号整数  比如20 Smalint 2byte有符号整数 比如20 Int          4byte有符号整数 比如20 Big ...

  8. mage Ansible学习1 常用模块

    一.Ansible特点 二.Ansible架构 1.core modules实现常用模块 2.Custom modules实现自定义模块 3.Connection Plugins 连接插件,可通过SS ...

  9. js之大文件断点续传

    文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...

  10. learning java 获取环境变量及系统属性

    通过System.getenv( ) 获取环境变量 通过System.getProperties() 获取系统属情 通过System.currentTimeMillis()  System.nanoT ...