[leetcode]372. Super Pow
来源:互联网 发布:社交软件英语 编辑:程序博客网 时间:2024/05/17 01:40
题目链接:https://leetcode.com/problems/super-pow/#/description
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
思路:
公式: ab % k = (a%k)(b%k)%k
而题目里面是一个vector,那么我们可以从后往前逐个扫描数组里面的数字。
比如: a^1234567 % k = (a^1234560 % k) * (a^7 % k) % k = (a^123456 % k)^10 % k * (a^7 % k) % k
用公式表示就是:假设f(a, b) 计算 a^b % k,那么f(a,1234567) = f(a, 1234560) * f(a, 7) % k = f(f(a, 123456),10) * f(a,7)%k;
class Solution{private: const int base=1337; //ex. (7^3)%k=(7*7*7)%k=((7%k)*((7*7)%k))%k=((7%k)*((7%k)*(7%k)%k))%k int powmod(int a,int k) { a%=base; int result=1; for(int i=0;i<k;i++) result=(result * a)%base; return result; }public: // ex. a^1234567 % k = (a^1234560 % k) * (a^7 % k) % k = (a^123456 % k)^10 % k * (a^7 % k) % k int superPow(int a,vector<int>& b) { if(b.empty()) return 1; int last_digit=b.back(); b.pop_back(); return powmod(superPow(a,b),10)*powmod(a,last_digit)%base; }};
0 0
- leetcode.372. Super Pow
- LeetCode 372. Super Pow
- [LEETCODE] 372. super pow
- LeetCode--372. Super Pow
- leetcode 372. Super Pow
- Leetcode 372. Super Pow
- [leetcode] 372. Super Pow
- Leetcode: 372.Super Pow
- 372. Super Pow(LeetCode)
- LeetCode 372. Super Pow
- [LeetCode]372. Super Pow
- [leetcode]372. Super Pow
- leetcode 372. Super Pow
- LeetCode—372. Super Pow
- Leetcode 372. Super Pow[medium]
- [leetcode] 372. Super Pow 解题报告
- 【leetcode】372. Super Pow【M】【40】
- leetcode 336. Palindrome Pairs 372. Super Pow
- JS对象和JSON
- 如何使用yum来下载RPM包而不进行安装
- 【架构设计】【程序指标】鲁棒性与健壮性的细节区别
- linux c动态链接库的使用
- git使用
- [leetcode]372. Super Pow
- CentOS配置Nginx官方的Yum源
- 最大数(提高+/省选-)
- CSS中rem em px的区别
- 最大数(提高+/省选-)
- 写一个进度条程序
- 同步,异步,阻塞,非阻塞
- hashmap实现原理浅析
- 概述