372. Super Pow
问题
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3] Result: 8
Example2:
a = 2 b = [1,0] Result: 1024
分析

根据公式 ① ② ab mod 1337 可以优化为 (a mod 1337)b mod 1337 ,所以每次计算底数的时候,计算之前和之后,都进行mod,预防结果超过整数范围。
根据公式 ③ ,可以根据题意对指数进行分解,本题的进制为10.
根据公式 ④,对计算过程进行分解,每一步都是计算
,由于ai知道,所以每步的计算其实是计算底数。
代码
int mod = ;
int superPow(int a, vector<int>& b) {
int answer = ,n = b.size();
if( n == )
return ;
for(int i = n - ;i >= ; i--)
{
if( b[i] > )
answer = answer * Inpow(a,b[i]) % mod;
a = Inpow(a,);
}
return answer;
} int Inpow(int base,int exp){
int result = ;
base = base%mod; for(int i = exp; i > ;i = i >> )
{
if( i & == )
result = result * base %mod;
base = base * base % mod;
} return result % mod; }
372. Super Pow的更多相关文章
- 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 ...
- 372 Super Pow 超级次方
你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出.示例 1:a = 2b = [3]结果: 8示例 2:a = 2b = [1,0]结果: 102 ...
- LeetCode——372. Super Pow
题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...
- 【LeetCode】372. Super Pow 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/super-po ...
- 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.txt
▶ 指数取模运算 ab % m ▶ 参考维基 https://en.wikipedia.org/wiki/Modular_exponentiation,给了几种计算方法:暴力计算法,保存中间结果法(分 ...
- [LeetCode] Super Pow 超级次方
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...
- [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 Super Pow
题目描述: superPow(int a, int[] b),b是一个int数组,每个元素都是正的个位数,组合起来表示一个正整数,例如b=[1,2,3]表示123,求解a^b mod 1337. 思路 ...
随机推荐
- Brackets
按下Ctrl + E("编辑")或退出编辑.Brackets将搜索项目下所有CSS文件 Ctrl/Cmd + Alt + P 打开即时预览功能 alt + command + O目 ...
- Python迭代器,可迭代对象,生成器
迭代器 迭代器(iterator)有时又称游标(cursor)是程式设计的软件设计模式,可在容器物件(container,例如链表或阵列)上遍访的界面,设计人员无需关心容器物件的内存分配的实现细节. ...
- asp.net core 如何在Controller获取配置文件的值
场景:我们会把一些配置信息,写在配置文件文件中,便于我们修改和配置.在之前的asp.net 中可以通过ConfigurationManger来获取web.config里面的配置.在.net core ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- QEMU 中的QOM分析
QOM (QEMU Object Model) 类对象的意义: 1:每个类型在系统中都只有且只有一个类对象 2:当系统中的某个类型的实例对象都被销毁了,那么系统就会销毁该类对象了 3:类对象的作用:负 ...
- style,currentStyle,getComputedStyle的区别和用法
先说说层叠样式表的三种形式(三种的叫法不一,按照各自的习惯): 一.内联样式:在HTML标签用style属性设置.如: 1 <p >这是内联样式</p> 二.嵌入样式:通过&l ...
- struts.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC " ...
- 11i和R12配置JAR包
R11:$IAS_ORACLE_HOME/Apache/Jserv/etc/jserv.properties R12: 方法1:直接解压JAR包放到$JAVA_TOP下: 方法2:编辑:$ORA_CO ...
- php.h: No such file or directory
建立一个php的include路径到/usr/include的软连接就好了 ln -s /usr/include/php-zts/* /usr/include/
- Linux学习之三--scp命令
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...