▶ 指数取模运算 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. 网络编程 单机最大tcp连接数

    在tcp应用中,server事先在某个固定端口监听,client主动发起连接,经过三路握手后建立tcp连接.那么对单机,其最大并发tcp连接数是多少? 如何标识一个TCP连接 在确定最大连接数之前,先 ...

  2. 表统计信息(storge)相关

    select t1.NUM_ROWS,t1.BLOCKS,t1.EMPTY_BLOCKS,t1.AVG_SPACE,t1.CHAIN_CNT,t1.AVG_ROW_LEN from user_tab_ ...

  3. xtrabackup三种备份和还原(一)

    写这边博客心情不是太美好(博客已经停更2个多月了,实在是没心情学习新东西.2018我的黑暗年,呵呵)好了,不废话了,本文没有任何原理的部分,我也是刚开始接触xtrabackup这个工具.本文应该是一个 ...

  4. Python基础学习----参数和返回值

    # 函数的参数和返回值 # 4种组合方式 # 1.无参无返 # def methodone(): # 2.无参有返 def methodtwo(): a=10 return a # 3.有参无返 # ...

  5. STL标准库-hash

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 hash的结构图如下图 oject通过hashfunc转换成hashcode然后插入到相应篮子中 hash同rb_tree是一种 ...

  6. linux提权辅助工具(二):linux-exploit-suggester-2.pl

    来自:https://github.com/jondonas/linux-exploit-suggester-2/blob/master/linux-exploit-suggester-2.pl #! ...

  7. 判断一颗二叉树是否为二叉平衡树 python 代码

    输入一颗二叉树,判断这棵树是否为二叉平衡树.首先来看一下二叉平衡树的概念:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.因此判断一颗二叉平衡树的关键在于 ...

  8. HTML第一课——基础知识普及【1】

    请关注公众号:自动化测试实战 HTML概念及编写规范 html叫做超本文标记语言,注意它知识标记语言,不是编程语言. 编写规范: 由标记(html, div, p, h1等)组成 标记成对出现(< ...

  9. 【传输协议】HttpClient基本使用

    最近工作中是做了一个handoop的hdfs系统的文件浏览器的功能,是利用webhdfs提供的rest api来访问hdfs来与hdfs进行交互的,其中大量使用HttpClient,之前一直很忙,没什 ...

  10. 在WPF中使用CefSharp嵌入浏览器(转)

    在WPF中使用CefSharp嵌入浏览器   日常开发中,我们需要将一些Web页面嵌入到桌面客户端软件中.下面我们使用CefSharp嵌入浏览器来实现. 首先先介绍一下CefSharp嵌入式浏览器,它 ...