当前位置:网站首页>Fast power code
Fast power code
2022-06-13 05:17:00 【Python's path to becoming a God】
Basic questions : Ask for numbers
a
a
a Of
n
n
n Power .
The basic idea , It's a one-time multiplication , The time complexity is
O
(
n
)
O(n)
O(n).
The code is as follows :
using ll = long long;
ll pow(ll a, ll n)
{
ll ret = 1;
for (ll i = 0; i < n; i++) {
ret *= a;
}
return ret;
}
A little thought will reveal , To calculate a number
n
n
n Power does not need to be multiplied
n
n
n Time . If we knew
a
a
a Of
n
/
2
n/2
n/2 Power , You can use two
a
n
/
2
a^{n/2}
an/2 Multiply , get
a
n
a^n
an. Follow this thought , Think about it
n
n
n Split into binary form , In this way, the answer can be obtained by multiplying the powers obtained from the corresponding binary digits . give an example : When
n
n
n by 7 when , We want to
6
7
6^7
67.
7
7
7 According to binary, it can be divided into
00000111
00000111
00000111. Then we just need to know
6
1
6^1
61,
6
2
6^2
62 and
6
4
6^4
64, Then multiply them .
The code is as follows :
ll qpow(ll a, ll n)
{
if (n == 0) {
return 1;
} else if (n % 2 == 1) {
return qpow(a, n - 1) * a;
} else {
ll ret = qpow(a, n / 2);
return ret * ret;
}
}
ll qpow2(ll a, ll n)
{
ll ret = 1;
while (n) {
if (n & 1) {
ret *= a;
}
a *= a;
n >>= 1;
}
return ret;
}
One is recursion , The second is the way of iteration . Consider call stack switching , Mode 2 will be faster . This method of splitting powers into binary numbers , Is called a fast power .
The complete code is as follows :
#include <iostream>
#include <time.h>
using namespace std;
using ll = long long;
ll pow(ll a, ll n)
{
ll ret = 1;
for (ll i = 0; i < n; i++) {
ret *= a;
}
return ret;
}
ll qpow(ll a, ll n)
{
if (n == 0) {
return 1;
} else if (n % 2 == 1) {
return qpow(a, n - 1) * a;
} else {
ll ret = qpow(a, n / 2);
return ret * ret;
}
}
ll qpow2(ll a, ll n)
{
ll ret = 1;
while (n) {
if (n & 1) {
ret *= a;
}
a *= a;
n >>= 1;
}
return ret;
}
int main()
{
ll a = 7, n = 16;
clock_t startTime, endTime;
startTime = clock(); // cpu clock time
cout << pow(a, n) << endl;
endTime = clock();
cout << (endTime - startTime) << endl;
startTime = clock(); // cpu clock time
cout << qpow(a, n) << endl;
endTime = clock();
cout << (endTime - startTime) << endl;
startTime = clock(); // cpu clock time
cout << qpow2(a, n) << endl;
endTime = clock();
cout << (endTime - startTime) << endl;
}
You can see the latter two cpu The period is generally lower than the previous one .
边栏推荐
- C language learning log 2.19
- C language learning log 10.4
- Clause 27: alternatives to overloading with familiar universal reference types
- Interpretation of QT keypressevent
- Mysql database crud operation
- File descriptorfile description
- OpenCV中的saturate操作(饱和操作)究竟是怎么回事
- C language learning log 10.5
- What is the saturate operation in opencv
- C language learning log 10.19
猜你喜欢
Case - simulated landlords (upgraded version)
Install harbor (online offline)
Recursion and recursion
RTSP streaming using easydarwin+ffmpeg
Problems encountered in the use of PgSQL
Small project - household income and expenditure software (1)
Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis
Use the browser to cut the entire page (take chrome as an example)
C language learning log 1.22
QT signal is automatically associated with the slot
随机推荐
基于 Docker Compose 搭建 Nacos 2(使用 MySQL 进行持久化)
std::condition_ variable::wait_ for
Mysql database crud operation
[multi thread programming] the future interface obtains thread execution result data
Building Nacos 2 based on docker compose (using MySQL for persistence)
QT using layout manager is invalid or abnormal
行情绘图课程大纲1-基础知识
QT interface rendering style
Automatic database backup (using Navicat)
Wampserver (MySQL) installation
Pycharm错误解决:Process finished with exit code -1073741819 (0xC0000005)
Dynamic programming - longest common substring
Luogu p1036 number selection
Modification and analysis of libcoap source code by Hongmeng device discovery module
Deleted the jupyter notebook in the jupyter interface by mistake
Clause 30: be familiar with the failure of perfect forwarding
Solutions to conflicts between xampp and VMware port 443
C language learning log 12.5
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
Redis plus instructions