Young

effective power function 본문

코딩 테스트 대비 추천 문제

effective power function

yyjjang9 2019. 4. 5. 19:22
728x90
반응형

거듭제곱을 조금 더 효율적으로 하는 recursive 함수입니다.

시간 복잡도는 O(logN) 입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
 
#define endl    '\n'
#define FastIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
using namespace std;
typedef long long ll;
 
ll power(int a, int b) {
    if (b == 0return 1;
    if (b == 1return a;
    ll ret = power(a, b >> 1);
    if (b & 1return ret * ret * a;
    return ret * ret;
}
 
int main() {
    FastIO;
 
    cout << power(27<< endl;
    
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 
728x90
반응형

'코딩 테스트 대비 추천 문제' 카테고리의 다른 글

정사각 배열 90도 회전  (0) 2019.04.06
BOJ - 배열 돌리기 2 (16927)  (0) 2019.04.06
소문난 칠공주  (0) 2019.03.12
전공책  (0) 2019.03.12
문자 메시지  (0) 2019.03.11