n次方
1、问题描述
计算 an
2、算法分析
先将 n 变一变,寻找新的计算路径。预处理就是变治法的根本。
如果单纯循环执行 n 次相乘,那么时间复杂度为 O(n)。可以利用二进制幂大大改进效率。
主要思路是:将十进制的 n 转换成二进制的数组序列 b[]。二进制幂求解有两种方法:从左至右二进制幂和从右至左二进制幂。
从左至右二进制幂
变换:an = a(b[n]2m + ... + b[0]20)
先求 n 的二进制串,如:n = 5 => 1 0 1,那么 b[2] = 1, b[1] = 0, b[0] = 1
二进制求 n 的伪代码:
Horner(b[0...n], x)
k = b[n]
for i = n-1 downto 0 do
p = x*k + b[i]
return p
那么 n 用作 a 的指数时意义是什么样的呢:
ap = a1
for i = n - 1 downto 0 do
ap = a(2p+b[i])
从右至左二进制幂
n 变换方法与上面相同,然后从 b[0] -> b[n] 方向逐步求解。
时间复杂度:O(logn)
3、代码实现
#include <stdio.h>
#include <stdlib.h>
/**
* @brief 返回 x 的二进制串(数组)
*/
int GetBinArray(int x, int arr[], int length)
{
int idx = 0;
while(x > 0) {
// 获取末位的二进制
arr[idx++] = (x & 1) ? 1 : 0;
if (idx == length)
break;
// 右移两位
x = x >> 1;
}
return idx;
}
/**
* @brief a^n = a^(b[n]2^n + ... + b[0]2^0)= a^(b[n]2^n)* ... * a^b[0]。 b 数组元素不是 1 就是 0
*/
int Pow_Bin_RightToLeft(int number, int power)
{
if (power == 0)
return 1;
int length = sizeof(int) * 8; // 32
int *pint = (int *)malloc(length);
// 获取幂的二进制数组
length = GetBinArray(power, pint, length);
int item = number;
int ret = 1;
for (int i = 0; i < length; i++) {
// 二进制值为 1,计入结果
if (pint[i] == 1)
ret *= item;
item *= item;
}
free(pint);
return ret;
}
/**
* @brief a^n = a^(b[n]2^n + ... + b[0]2^0)=((b[n]*2 + b[n-1])*X + ....)2 + b[0]。 b 数组元素不是 1 就是 0
*/
int Pow_Bin_LeftToRight(int number, int power)
{
if (power == 0)
return 1;
int length = sizeof(int)*8;
int *pint = (int *)malloc(length);
length = GetBinArray(power, pint, length);
int ret = number;
for (int i = length - 1 - 1; i >= 0; i--) {
ret *= ret;
if(pint[i] == 1)
ret *= number;
}
free(pint);
return ret;
}
int main()
{
int num = 8, power = 6;
int ret1 = Pow_Bin_RightToLeft(num, power);
int ret2 = Pow_Bin_LeftToRight(num, power);
printf("Pow_Bin_RightToLeft: %d^%d == %d\n", num, power, ret1);
printf("Pow_Bin_LeftToRight: %d^%d == %d\n", num, power, ret2);
return 0;
}
Pow_Bin_RightToLeft: 8^6 == 262144
Pow_Bin_LeftToRight: 8^6 == 262144
4、内容来源
n次方的更多相关文章
- [LeetCode] Super Pow 超级次方
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...
- [LeetCode] Power of Four 判断4的次方数
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...
- [LeetCode] Power of Three 判断3的次方数
Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...
- [LeetCode] Power of Two 判断2的次方数
Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...
- [LeetCode] Pow(x, n) 求x的n次方
Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...
- 剑指Offer面试题:10.数值的整数次方
一.题目:数值的整数次方 题目:实现函数double Power(doublebase, int exponent),求base的exponent次方.不得使用库函数,同时不需要考虑大数问题. 在.N ...
- GDUFE-OJ 1203x的y次方的最后三位数 快速幂
嘿嘿今天学了快速幂也~~ Problem Description: 求x的y次方的最后三位数 . Input: 一个两位数x和一个两位数y. Output: 输出x的y次方的后三位数. Sample ...
- 《剑指offer》面试题11: 数值的整数次方
面试题11: 数值的整数次方 剑指offer面试题11,题目如下 实现函数double power(double base,int exponent),求base的exponent次方, 不得使用库 ...
- 打出10的n次方,上标,下标等处理方法(mac)
我使用mac系统遇到的需求,需要在项目中显示10的6次方 用来做单位,找了很多方案,word等文本编辑工具很好实现(word是使用ctrl + shift + =)(mac 版的word是 Comm ...
- 计算2的N次方&&计算e
2的N次方 注意:这里在处理的时候并没有用循环来处理,而是用移位的做法. n<<4 就是 n*2^4 ,所以在本例中只需要写 1<<time (time是要求的 ...
随机推荐
- hue初识
Hue Web应用的架构 Hue 是一个Web应用,用来简化用户和Hadoop集群的交互.Hue技术架构,如下图所示,从总体上来讲,Hue应用采用的是B/S架构,该web应用的后台采用python编程 ...
- 前端每日实战:48# 视频演示如何用纯 CSS 创作一盘传统蚊香
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/BVpvMz 可交互视频教程 此视频 ...
- Layabox UI 笔记
1.UI List 官方文档是 https://ldc2.layabox.com/doc/?language=zh&nav=zh-js-3-3-7 通常用List的时候会出现滚动条按钮比例拉 ...
- javascript的constructor属性介绍
之前闲来了解了__proto__和prototype的区别,每个对象都有隐私属性__proto__,而prototype是javascript函数特有的属性.那么constructor属性呢?最近是遇 ...
- SpringBoot图文教程15—项目异常怎么办?「跳转404错误页面」「全局异常捕获」
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1-Spr ...
- 微信小程序之登录连接django,以及用户的信息授权认证
小结: 1 如何自定义组件 - 组件和页面一样,也是由四个文件组成,所以我们自定义组件的时候,模拟pages文件夹,把所有的所有的组件都放在一个文件夹中,每个组件又由一个文件夹包裹,方便管理,在对应目 ...
- python爬虫的数据库连接问题
1.需要导的包 import pymysql 2.# mysql连接信息(字典形式) db_config ={ 'host': '127.0.0.1',#连接的主机id(107.0.0.1是本机id) ...
- 写一个scrapy中间件--ip代理池
middleware文件 # -*- coding: utf-8 -*- # Define here the models for your spider middleware # See docum ...
- 动态创建多个button
2020-03-13 每日一例第6天 1.新建窗体windowform,修改text值: 2.找到mouseclick事件,填写代码: Random rm = new Random(); Button ...
- frida的简单实用
一.环境 1.环境 1.手机运行服务端 2. 电脑端运行客户端3.进行端口转发 adb forward tcp:27042 tcp:27042 adb forward tcp:27043 tcp:27 ...