▶ 指数取模运算 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. 从零开始新建一个Maven 、springMVC工程

    相关代码:http://pan.baidu.com/s/1jIBUr1G 目标: 新建一个名为testSpringMvc的spring MVC工程,maven管理jar包: 1.新建maven管理的w ...

  2. vue 报错 Cannot read property '__ob__' of undefined的解决方法

    记不清第n次遇到这个错误了,但是脑子就是不好用,记不住解决办法啊,每次都要找好久才能找到错误,网上还一篇篇的全是错误答案......所以写篇随笔,记录下,方便大家也方便我自己. 网上有人说是组件循环了 ...

  3. localStorage(本地存储)使用总结

    1.https://www.cnblogs.com/st-leslie/p/5617130.html (localStorage使用总结)

  4. Xcode8出现问题总结

    上点干货,目前得知的一些bug解决汇总:iOS10相册相机闪退bughttp://www.jianshu.com/p/5085430b029fiOS 10 因苹果健康导致闪退 crashhttp:// ...

  5. python3连接MySQL数据库实例

    #python3连接MySQL实例 import pymysql """导入连接MySQL需要的包,没有安装pymysql需要先安装 使用命令行切换到python的安装路 ...

  6. hdu 3697 10 福州 现场 H - Selecting courses 贪心 难度:0

    Description     A new Semester is coming and students are troubling for selecting courses. Students ...

  7. bzoj3862

    题解: 这一道题目和模板有不同的地方就是在于可以修改只有一条边和i相邻 于是我们还要记录与这个点相邻的点有没有改变 代码: #pragma GCC optimize(2) #include<bi ...

  8. 【LeetCode 225_数据结构_栈_实现】Implement Stack using Queues

    class Stack { public: // Push element x onto stack. void push(int x) { int len = nums.size(); nums.p ...

  9. 201621123006 《Java程序设计》第14周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 2 ...

  10. SSH整合报错:Unable to instantiate Action, testAction, defined for 'test' in namespace '/'testAction

    报错如下: Struts Problem Report Struts has detected an unhandled exception: Messages: testAction Unable ...