372. Super Pow.txt
▶ 指数取模运算 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的更多相关文章
- 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 ...
- 372. Super Pow
问题 Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large p ...
- 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 ...
- [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. 思路 ...
随机推荐
- org.springframework.transaction 包改成 spring-tx
org.springframework.transaction 包改成 spring-tx org.springframework.transaction 3.2.2以后的版本,全改到 spring ...
- Mybatis出现参数错误Parameter 'cateCode_search' not found. Available parameters are [3, 2, 1, 0, param1, param2, 5, param3, 4, param4, param5, param6]
1.参数改为序号 2.在mybatis借口上加@Param注解
- Android手机无线adb
1.首先电脑,手机通过数据线链接电脑,然后通过adb devices 查看到已连接 2.输入:adb tcpip 5555 3.输入:adb connect 222.222.221.137:5555 ...
- 剑指offer面试题19 二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- VirtualBox使用物理硬盘建立磁盘
VirtualBox,只能用命令行来 建立磁盘才可以使用物理硬盘. 1.运行cmd,cd进入你的VirtualBox目录,如: cd C:\Program Files\Sun\VirtualBox ...
- 052——VUE中使用vue-cli初始化单页面应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- IIS7 部署 MVC3
IIS7 部署 MVC3 (2013-02-28 11:06:39) 转载▼ 标签: iis7 mvc3 it 分类: ASP.NET 在IIS7下部署MVC已经简化了许多,基本按照一般的项目部署即可 ...
- ES6介绍二 函数的增强
ES6对于函数的使用新增了很多实用的API,JS的函数跟很多后台语言PHP,ASP.NET开始看齐: 1. 参数默认值: 以前我们为了给函数创建默认值,必须用一种冗杂的语句,而且有歧义的语句. //E ...
- mysql 简单级联的学习
数据库上面一直是我的弱项,昨天突然想到,简单的级联,即一个表中的列表删除了,另外一个依赖这个表的其他数据应该也会删除,当时想了下,可以根据外键来判断把其他表的数据给删除了,但是这样一来好像要必须知道其 ...
- keras_基本网络层结构(1)_常用层
参考文献: https://blog.csdn.net/sinat_26917383/article/details/72857454 http://keras-cn.readthedocs.io/e ...