问题

  Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.

  Example1:

  a = 2
  b = [3]   Result: 8

  Example2:

  a = 2   b = [1,0]   Result: 1024

分析
  
  根据公式 ① ② ab mod 1337 可以优化为 (a mod 1337)b mod 1337 ,所以每次计算底数的时候,计算之前和之后,都进行mod,预防结果超过整数范围。
  根据公式 ③ ,可以根据题意对指数进行分解,本题的进制为10.
  根据公式 ④,对计算过程进行分解,每一步都是计算 ,由于ai知道,所以每步的计算其实是计算底数。

代码 

    int mod = ;
int superPow(int a, vector<int>& b) {
int answer = ,n = b.size();
if( n == )
return ;
for(int i = n - ;i >= ; i--)
{
if( b[i] > )
answer = answer * Inpow(a,b[i]) % mod;
a = Inpow(a,);
}
return answer;
} int Inpow(int base,int exp){
int result = ;
base = base%mod; for(int i = exp; i > ;i = i >> )
{
if( i & == )
result = result * base %mod;
base = base * base % mod;
} return result % mod; }

  

    

372. Super Pow的更多相关文章

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

  6. 372. Super Pow.txt

    ▶ 指数取模运算 ab % m ▶ 参考维基 https://en.wikipedia.org/wiki/Modular_exponentiation,给了几种计算方法:暴力计算法,保存中间结果法(分 ...

  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. Error Domain=NSURLErrorDomain Code=-999 “The operation couldn’t be completed.

    转:http://www.wangzhengdong.com/blog/error-domainnsurlerrordomain-code-999-the-operation-couldnt-be-c ...

  2. Spring拦截机制之后端国际化心得

    需求 前端请求的header里带有Prefer_Lang参数,向后端传递国际化信息,后端需要在处理业务之前(建立拦截机制),将Prefer_Lang保存于线程上下文. 思路分析 初次接收该需求时,为了 ...

  3. ffmpeg获取文件的总时长(mp3/mp4/flv等)

    使用ffmpeg.exe获取文件属性信息,C#中可以在进程外异步调用这个工具,如下: using (System.Diagnostics.Process pro = new System.Diagno ...

  4. dwarf tower

    dwarf tower(dwarf.cpp/c/pas)[问题描述]Vasya在玩一个叫做"Dwarf Tower"的游戏,这个游戏中有n个不同的物品,它们的编号为1到n.现在Va ...

  5. shell脚本自动拉起启动程序

    一.我们先看看编写的shell脚本的内容,该shell的作用主要是实现监控某个程序崩溃之后就自动重启该进程. while true do procnum=` ps -ef|grep "tes ...

  6. linux动态代码注入

    参考网上文章:http://www.freebuf.com/articles/system/6388.html 按照文章,实现了代码的动态注入,即对一个正在运行的进程,在不重启的情况下执行一段不在原程 ...

  7. posgresql

    ubuntu下 修改postgres数据库用户的密码为123456 sudo -u postgres psql postgres=# ALTER USER postgres WITH PASSWORD ...

  8. 各个浏览器显示版本(IE,火狐)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 12月13日上午Smarty模版原理

    模板主要是用来让前端和后端分离的,前台页面只是一个前台页面,后台页面用php代码写逻辑,写完逻辑拿到前台显示. 一.写法 一般需要以下:写3个页面: 1.显示页面aa.html <!DOCTYP ...

  10. Math小计

    20161231 黄金分割比:短/长=长/(短+长)=((根号5)-1)/2 ≍ 0.618 斐波那契数列前后两项的比值存在极限.设其中三个数为a.b.(a+b),则当项数趋于无穷时有a/b=b/(a ...