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. 思路 ...
随机推荐
- Granting and Managing Item Level Permission using SharePoint2013 Designer Workflow
https://gnanasivamgunasekaran.wordpress.com/2015/12/29/granting-and-managing-item-level-permission-u ...
- WebApi用Post的方式提交Json时,获取不到值或不进对应方法的问题
又是一个通宵,终于搞明白了. 被WebApi坑得好惨. 之前用各种方法Post上来,有时可以读到结构,但没值,有时直接就是一个Null,有时连方法都没进就跑了,只是来控制器里看了一下…… 最后好友说还 ...
- 用压测模拟并发、并发处理(synchronized,redis分布式锁)
使用工具:Apache an 测压命令: ab -n 100 -c 100 http://www.baidu.com -n代表模拟100个请求,-c代表模拟100个并发,相当于100个人同时访问 ab ...
- SpringMvc中的Interceptor拦截器的学习
拦截器是SpringMvc框架中常用的一个东东,它跟Filter相似,但是也有区别,以前也没用过,今天看到就顺便学习了一下. SpirngMvc中的Interceptor主要是通过HandlerInt ...
- ASP.NET MVC 使用NPOI导出Excel 无法访问已关闭的流(转)
第一步重写MemoryStream , 让它不能自动关闭. //新建类 重写Npoi流方法 public class NpoiMemoryStream : MemoryStream { public ...
- 那些实用的Nginx规则
1. 概述 大家都知道Nginx有很多功能模块,比如反向代理.缓存等,这篇文章总结下我们这些年实际环境中那些有用的Nginx规则和模块,大部分是用法的概括及介绍,具体细节在实际配置时再自行google ...
- latex的使用
要写论文了,要用到latex,总算明白了一些,在ubuntu下安装好texlive和texmaker后,在终端测试,输入命令tex: 出现上面的图说明安装好了. 在texmaker下编写tex文档,保 ...
- InputStream,String和Reader之间的转换
1.String –> InputStreamInputStrem is = new ByteArrayInputStream(str.getBytes());orByteArrayInputS ...
- SqlServer一些常用函数(持续更新。。。)
1. 字符串拼接: + 拼接 SELECT 'AA' + 'BB' A //AABB在2012版本sqlserver之后,可以使用cancat进行字符串拼接了. 2. 判断是否为空,并取另外的值 :I ...
- PTA编程总
7-1 币值转换 (20 分) 输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式.如23108元,转换后变成“贰万叁仟壹百零捌”元.为了简化输出,用小写英文字 ...