Super Pow
来源:互联网 发布:jquery 数组如何清空 编辑:程序博客网 时间:2024/05/18 01:38
一、问题描述
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 = 2b = [3]Result: 8
Example2:
a = 2b = [1,0]Result: 1024
二、思路
1140是如何计算出来的?
phi(1337) = phi(7) * phi(191) = 6 * 190 = 1440
根据ab % 1337 = ab % phi(1337) % 1337 = ab % 1140 % 1337.
所以我们需要两个函数分别处理指数和底数分别对1140和1337求余。
三、代码
class Solution {public: int superPow(int a, vector<int>& b) { if(a % 1337 == 0) return 0; int index = 0; for(int i : b) index = (index * 10 + i) % 1140; if(index == 1140) index = index + 1140; return power(a,index,1337); } int power(int x, int n, int mod) { int result = 1; for(x = x % mod; n; x = x * x % mod,n = n >> 1) if(n & 1) result = result * x % mod; return result; }};
0 0
- Super Pow
- Super Pow
- Super Pow
- Super Pow
- Super Pow
- Pow(x, n) / Super Pow
- leetcode.372. Super Pow
- LeetCode 372 Super Pow
- leetcode372. Super Pow
- LeetCode 372. Super Pow
- 372. Super Pow
- LeetCode Super Pow详解
- 372. Super Pow
- [LEETCODE] 372. super pow
- leetcode Super Pow
- LeetCode--372. Super Pow
- 372. Super Pow
- LeetCode: Super Pow
- 浅谈前端与SEO
- 这可以使程序更容易理解。
- HTTP和Socket通信的区别?Socket连接相关库,TCP,UDP的连接方法,Http的几种常用方式有哪些?
- Joda-Time 简介
- 数组越界会造成死循环
- Super Pow
- ViewPager和Fragment的混合使用
- JSTL c标签库remove方法的使用
- Linux下使用apktool打包apk报brut.androlib.AndrolibException
- sha1加密
- SAP HR是什么
- memcpy函数
- dijkstra算法心得
- 成长小计-控制器中有UIScrollView控件时,无法响应触摸事件