问题

  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. wxpython绘制折线图

    environment:win10 + eclipse + pydev + python2.7.11 + wxpython3.0.2 code sample: #!/usr/bin/env pytho ...

  2. PHPstorm同步文件时与ftp断开连接

    一用PHPstorm同步对比服务器端和本地文件的差异时,一会就断开ftp再也连不上了,弄了好久终于找到原因了,好像这个同步会频繁请求建立连接,服务器本地安全策略屏蔽了ip,还是下载后再做修改或者直接在 ...

  3. NFS服务器配置文档

    Server:192.168.1.206/WindowsClient:192.168.1.208/CentOS一.搭建windows NFS服务:1.安装NFS服务器:打开"服务管理器&qu ...

  4. AnjularJS系列2 —— 表单控件功能相关指令

    第二篇,表单控件功能相关指令. ng-checked控制radio和checkbox的选中状态 ng-selected控制下拉框的选中状态 ng-disabled控制失效状态 ng-multiple控 ...

  5. 懵懂的js原型

    说起原型,就得提起函数,js中据说是这样定义的,每个函数都有一个prototype 属性,(这个属性啊,又是个对象).为了构造函数能复用,就引出了原型,要把共享的东西放到原型上去.例如 //构造函数, ...

  6. Hibernate(Control)

    案例:http://blog.csdn.net/jiuqiyuliang/article/details/39380465 对象关系映射框架,它对JDBC进行了轻量级的对象封装,可以使用对象编程思维来 ...

  7. Apache服务器网站访问伪静态内页出现No input file specified.的完美解决方案

    原文地址:Apache服务器网站访问伪静态内页出现No input file specified.的完美解决方案 启用REWRITE的伪静态功能的时候,首页可以访问,而访问内页的时候,就提示:&quo ...

  8. 逆向工程学习第二天--动手开发自己的第一个shellcode

    一个简单的c语言添加windows管理员账号的小程序,之前在渗透的时候经常用到,现在拿它来做自己的第一个shellcode. C代码: #pragma comment(lib, "netap ...

  9. [译]Writing Custom Middleware in ASP.NET Core 1.0

    原文: https://www.exceptionnotfound.net/writing-custom-middleware-in-asp-net-core-1-0/ Middleware是ASP. ...

  10. rails数据库查询 N + 1 查询的解决办法

    schema.rb ActiveRecord::Schema.define(version: 20150203032005) do create_table "addresses" ...