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.com/p/b256bd531df0
做这个题之间先了解两个公式:
公式一:a^b mod c = (a mod c)^b mod c
公式二:(ab) mod c = (a mod c)(b mod c) mod c
这道题题让我们求一个数的很大的次方对1337取余的值,即a^b mod 1337。输入的b是一个数组,且每一个数组代表一位,所以将求次方转换为223 = (22)10 * 23这种形式。
利用公式二将原式转化,即223 mod 1337 = ((22)10 mod 1337)*(23 mod 1337)mod 1337。
class Solution {
public:
int superPow(int a, vector<int>& b) {
int res = ;
for(int i = ;i < b.size();i++){
res = pow(res,) * pow(a,b[i]) % ;
}
return res;
}
int pow(int a,int n){
if(n == )
return ;
if(n == )
return a % ;
return pow(a % ,n/) * pow(a % ,n - n/) % ;
}
};
leetcode 50. Pow(x, n) 、372. Super Pow的更多相关文章
- 【LeetCode】372. Super Pow 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/super-po ...
- LeetCode——372. Super Pow
题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...
- 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 ...
- 372 Super Pow 超级次方
你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出.示例 1:a = 2b = [3]结果: 8示例 2:a = 2b = [1,0]结果: 102 ...
- 372. Super Pow
问题 Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large p ...
- 372. Super Pow.txt
▶ 指数取模运算 ab % m ▶ 参考维基 https://en.wikipedia.org/wiki/Modular_exponentiation,给了几种计算方法:暴力计算法,保存中间结果法(分 ...
- [LeetCode] 50. Pow(x, n) 求x的n次方
Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...
- LeetCode 50. Pow(x, n) 12
50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...
- LeetCode 50 Pow(x, n) (实现幂运算)
题目链接:https://leetcode.com/problems/powx-n/?tab=Description Problem:实现幂运算即 pow(x,n) 设形式为pow(x,n) ...
随机推荐
- 使用Arduino开发板制作交流电压表
在本文中,我们将使用Arduino开发板制作一个交流电压测量装置,测量我们家中交流电源的电压.我们将在Arduino IDE的串行监视器上打印输出该电压,并在万用表上显示出来. 制作数字电压表比模拟电 ...
- 【2019牛客多校第一场】XOR
题意: 给你一个集合A,里边有n个正整数,对于所有A的.满足集合内元素异或和为0的子集S,问你∑|S| n<=1e5,元素<=1e18 首先可以转化问题,不求∑|S|,而是求每个元素属于子 ...
- python+selenium+chrome初级自动化操作
例1. #coding=utf- from selenium import webdriver import os,time chromedriver = "C:\Users\AppData ...
- HTTPS——https下的静态资源中如何使用http的地址
前言 今天在改博皮的时候,将一些图片上传到七牛,但是引入的时候出问题了,因为七牛cnd设置的不是https域名,所以加载资源的时候导致自动转为https请求. 步骤 错误的写法 background: ...
- 从GITHUB下载源码
从昨天开始就想着从GitHub上下载一个开源的Vue的实战项目,希望能从中学习更多的Vue的实用内容,结果搞了半天好不容易下载了,不知道怎么弄.然而,今天终于成功了,激动地我赶紧来记录一下. 如何从G ...
- nginx配置静态资源:配置绝对路径
nginx配置静态资源:配置绝对路径 项目都是html格式的文件,我的项目路径:E:\javaservice\nginx-1.15.7\html assets:静态资源 html:站点文件 uploa ...
- 019_Python3 输入和输出
在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. ************************************ 1 ...
- 几个方便的基于es 的开源文档索引系统
Apache Tika 比较有名的内容提取工具 FsCrawler 使用java 开发,内部使用了Tika Ambar nodejs,python应用开发,轻量,支持基于docker 的快速部署,同时 ...
- mysqli的方法测试小结
<?php class MysqlController extends ControllerBase { public $config = array(); public $mysql = NU ...
- GDB的安装
1.下载GDB7.10.1安装包 #wget http://ftp.gnu.org/gnu/gdb/gdb-7.10.1.tar.gz或者可以远程看下有哪些版本 http://ftp.gnu.org/ ...