From 1928ffdc8e2016adacdaec1f229652a32a579ec3 Mon Sep 17 00:00:00 2001 From: Ranadheer Senani Dasari Date: Thu, 7 Oct 2021 19:05:40 +0530 Subject: [PATCH] added fast exponentiation --- fastexponentiation.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 fastexponentiation.cpp diff --git a/fastexponentiation.cpp b/fastexponentiation.cpp new file mode 100644 index 0000000..3201ee8 --- /dev/null +++ b/fastexponentiation.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + int t; + cin >> t; + unsigned long long a, b, c = 1e9 + 7, s, two = 2, one = 1; + while (t--) + { + s = 1; + cin >> a >> b; + a = a % c; + while (b > 0) + { + if (b % 2 == 0) + { + a = (a * a) % c; + b = b / two; + } + else + { + s = (s * a) % c; + b = b - one; + } + } + cout << s << endl; + } + return 0; +}