▶ 指数取模运算 ab % m

▶ 参考维基 https://en.wikipedia.org/wiki/Modular_exponentiation,给了几种计算方法:暴力计算法,保存中间结果法(分为左到右的二进制法和右到左的二进制法),矩阵法,优先群法,量子计算法。

● 代码,18 ms,令 a' = a % m,b = 10 * c + d(0 ≤ d < 10),则 ab % m == ((ac % m)10 % m) * (ad % m) % m,每次将指数的个位拆出来单独算,再和高位部分乘在一起。指数运算采用线性乘法,合并过程采用从右到左的二进制法,并使用了递归。

 class Solution
{
const int mod = ;
int powMod(int a, int k) //a^k mod 1337 where 0 <= k <= 10
{
a %= mod;
int i, result;
for (i = , result = ; i < k; result = (result * a) % mod, i++);
return result;
}
public:
int superPow(int a, vector<int>& b)
{
if (b.empty())
return ;
int last_digit = b.back();
b.pop_back();
return powMod(superPow(a, b), ) * powMod(a, last_digit) % mod;
}
};

● 代码,10 ms,与上述算法相同,指数运算采用从右到左的二进制法,合并过程采用从左到左到右二进制法,并使用了递归。

 class Solution
{
public:
const int mod = ;
int powMod(int a, int b)// a ^ b % mod
{
int result;
for (result = ; b; b >>= )
{
if (b & )
result = (result * a) % mod;
a = (a * a) % mod;
}
return result;
}
int superPow(int a, vector<int>& b)
{
int i, result;
for (a %= mod, i = b.size() - , result = ; i >= ; i--)
{
if (i < b.size() - )
a = powMod(a, );
result = (result * powMod(a, b[i])) % mod;
}
return result;
}
};

● 代码,8 ms,注意到欧拉 - 费马定理,对于任意正整数 a 和 n 有 aφ(n) ≡ 1 (mod n),于是令 b = φ(m) * c + d(0 ≤ d < φ(n)),则 ab % m == ad % m,这里 φ(1337) = 1337 * ( 1 - 1 / 7 ) * ( 1 - 1 / 191 ) = 1140

 class Solution
{
public:
const int mod = ;
int superPow(int a, vector<int>& b)
{
int p = , ret;
for (int i : b)
p = (p * + i) % ;
if (p == )
p += ;
for (ret = , a %= mod; p > ; a = a * a % mod, p >>= )
{
if (p & )
ret = ret * a % mod;
}
return ret;
}
};

372. Super Pow.txt的更多相关文章

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

  2. 372 Super Pow 超级次方

    你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出.示例 1:a = 2b = [3]结果: 8示例 2:a = 2b = [1,0]结果: 102 ...

  3. LeetCode——372. Super Pow

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

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

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

  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. 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 Pow 超级次方

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

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

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

  9. leetcode Super Pow

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

随机推荐

  1. python学习笔记(locust性能测试模块)

    locust是基于python的性能测试工具.支持python2.7及其以上的版本.相对于主流的LR与Jmeter工具使用的方式不一样.locust是通过编写python代码来完成性能测试的. 通过L ...

  2. Android之Fresco(facebook的强大Android图片加载的框架)

    Fresco是Facebook最新推出的一款用于Android应用中展示图片的强大图片库,可以从网络.本地存储和本地资源中加载图片.其中的Drawees可以显示占位符,直到图片加载完成.而当图片从屏幕 ...

  3. Linux下weblogic启动报错unable to get file lock的问题

    非正常结束weblogic进程导致weblogic无法启动 由于先前服务器直接down掉了,所有进程都非正常的进行关闭了,也就导致了下次启动weblogic的时候报了以下错误:<2012-3-2 ...

  4. APP的六种loading加载样式,全在这...

    今天这篇文章是给大家分享的loading加载的设计,文章里面会有一些实例在这分享给大家! 大多数App都要与服务器进行数据的交换,App向服务器发出数据请求,服务器接收到请求之后向App传输相应数据, ...

  5. hdu 3264 09 宁波 现场 E - Open-air shopping malls 计算几何 二分 圆相交面积 难度:1

    Description The city of M is a famous shopping city and its open-air shopping malls are extremely at ...

  6. 关于CMD中延迟环境变量嵌套的实现方法

    在我昨天做的一个bat中(自动按日期重命名文件名)涉及到这方面的问题 以前涉及到这里时就想别的办法替代过去,今天好好扒出来说说: 实现变量嵌套的2种方法: 1,使用call实现变量嵌套 变量嵌套:即在 ...

  7. linux查看端口对应的程序及pid

    linux中查看特定端口对应的进程以及进程的pid可以使用下面指令: lsof -i:port_number 杀死进程的指令是: kill -s 9 pid

  8. Python selenium chrome 环境配置

    Python selenium chrome 环境配置 一.参考文章: 1. 记录一下python easy_install和pip安装地址和方法 http://heipark.iteye.com/b ...

  9. mysql单列索引和联合索引的使用

    1,首先要确定优化的目标,在什么样的业务场景下,表的大小等等.如果表比较小的话,可能都不需要加索引. 2,哪些字段可以建索引,一般都where.order by 或者 group by 后面的字段. ...

  10. scanf()与gets()的区别

    scanf()与gets()的区别 1.scanf()可以同时接受多个字符串,而gets()一次只能接受一个字符串. #include<stdio.h>int main(){ char s ...