forked from sahilbansal17/Competitive_Coding
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpower.cpp
More file actions
47 lines (35 loc) · 1 KB
/
power.cpp
File metadata and controls
47 lines (35 loc) · 1 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//https://github.com/sahilbansal17/Competitive_Coding/
//Name: Anil Khatri (@imkaka)
//Email-Id: anil.soccer.khatri@gmail.com
// Now we understand the Algorithm in other file we need to have it in our Template to use Power()
//Whenever Necessary.
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
ll MOD = 1e9+7;
//Power Function (Recursive version) => O(log n)
//Add this to your Template
//It is in one line because it has to be added in everyday used Template.
//For logic and well idented program please refer Power.cpp , Just include it in your Template!!
ll Power(ll x, ll y){
ll temp;
if( y == 0)
return 1;
temp = Power(x, y/2)%MOD;
if (y%2 == 0)
return (temp%MOD*temp%MOD)%MOD;
else{
if(y > 0)
return (x%MOD*temp%MOD*temp%MOD)%MOD;
//Last Return when y is negative.
else return (temp*temp)/x;
}
}
int main(){
//User Input
long long base,power;
cin >> base >> power;
cout << Power(base , power);
}
//Complexity - O(log N)