372. Super Pow
来源:互联网 发布:淘宝店铺虚假交易扣2分 编辑:程序博客网 时间:2024/05/21 10:25
Your task is to calculate a^b 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: 8Example2:a = 2b = [1,0]Result: 1024
One knowledge: ab % k = (a%k)(b%k)%k
Since the power here is an array, we’d better handle it digit by digit.
One observation:
a^1234567 % k = (a^1234560 % k) * (a^7 % k) % k = (a^123456 % k)^10 % k * (a^7 % k) % k
Looks complicated? Let me put it other way:
Suppose f(a, b) calculates a^b % k; Then translate above formula to using f :
f(a,1234567) = f(a, 1234560) * f(a, 7) % k = f(f(a, 123456),10) * f(a,7)%k;
Implementation of this idea:
class Solution {public: int base = 1337; int superPow(int a, vector<int>& b) { if(b.empty()) return 1; int last_digit = b.back(); b.pop_back(); return ( helper(superPow(a,b),10) * helper(a,last_digit) )% base; } int helper(int a ,int k){ a %= base; int result = 1; for (int i = 0; i < k; ++i) { result = (result*a)%base; } return result; }};
0 0
- leetcode.372. Super Pow
- LeetCode 372. Super Pow
- 372. Super Pow
- 372. Super Pow
- [LEETCODE] 372. super pow
- LeetCode--372. Super Pow
- 372. Super Pow
- leetcode 372. Super Pow
- Leetcode 372. Super Pow
- [leetcode] 372. Super Pow
- Leetcode: 372.Super Pow
- 372. Super Pow
- 372. Super Pow(LeetCode)
- 372. Super Pow
- LeetCode 372. Super Pow
- 372. Super Pow
- [LeetCode]372. Super Pow
- 372. Super Pow
- 最长递增子序列
- Git的基本了解与使用、向github提交代码
- mysql Access denied for user \'root\'@\'localhost\'”解决办法
- 极限定理
- 图解手动全面检查管理本机端口
- 372. Super Pow
- 线段树 区间更新 区间求和以及最值
- 开始学习
- 计算机网络基础知识点学习(一)(概述)
- Git 提交本地工程到服务器
- 将手机中的音乐信息显示出来
- 开源 java CMS - FreeCMS2.5 标签 infoNextList
- 修改MySQL存储过程、函数、事件、触发器、视图的DEFINER
- 天梯赛决赛 l2-016 愿天下有情人都是失散多年的兄妹 简单深搜水题