-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Exponentiation..cpp
101 lines (65 loc) · 1.4 KB
/
Binary Exponentiation..cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define mod 100000
#define for1(k,n) for(ll i=k;i<n;i++)
#define for2(k,n) for(ll j=k;j<n;j++)
#define E cout<<endl
#define max 50000
int binexpo(int a, int n) // for finding the exponent of any number
{ int res = 1;
while (n)
{
if (n % 2 != 0)
{
res = res * a;
n--;
}
else
{
a = a * a;
n = n / 2;
}
}
return res;
}
int modexpo(int a, int n, int p) //for finding the large exponent modulo number
{
int res = 1;
while (n)
{
if (n % 2 != 0)
{
res = res * a % p;
n--;
}
else
{
a = a * a % p;
n = n / 2;
}
}
return res;
}
int main()
{
//////////////////////////////////////start...............
int n, a;
cin >> a >> n;
cout << binexpo(a, n) << endl;
// suppose we want to find the large exponent modulo number
//a^n % p ;
int p = 5;
cout << modexpo(a, n, p) << endl;
//Lets find the fibonnacii series in O(log n) time
/////////////////////////////end................................... ....
return 0;
}
//c v a s selecting text or x for selecting cut
//ctrl+d after selecting text to select same type
//ctrl+shift+d for copy and paste the line below it
//ctrl+del to delete a text
//ctrl+left to jump left of line or vice versa
//ctrl+shift+"/" to comment whole block and vice versa for undo
//ctrl+"/" for commenting a line