forked from arya2004/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
69 lines (62 loc) · 1.22 KB
/
Copy pathmain.cpp
File metadata and controls
69 lines (62 loc) · 1.22 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <unordered_set>
using namespace std;
int bruteForce(int a, int b, int m){
int res = pow(a,b);
res = res % m;
return res;
}
int fastExponentiation(int a, int b, int m){
int res = 1;
while (b > 0) {
if (b % 2 == 1) {
res = (res * a) % m;
}
a = (a * a) % m;
b /= 2;
}
cout << res << endl;
return res;
}
int main() {
//a^b % m
int a = 2;
int b = 3;
int m = 5;
int brute = bruteForce(a,b,m);
int fast = fastExponentiation(a,b,m);
cout << "Brute Force: " << brute << endl;
cout << "Fast Exponentiation: " << fast << endl;
return 0;
}