Skip to content

Commit

Permalink
Merge pull request #662 from bhavyammodi/main
Browse files Browse the repository at this point in the history
Create fermats_little_theorem.cpp
  • Loading branch information
hhhrrrttt222111 authored Oct 20, 2021
2 parents 435e037 + ea18dfb commit 3649787
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ALGORITHMS/Mathematical Algorithms/fermats_little_theorem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
#define ll long long int
// Compute (A^B)%mod
using namespace std;
ll power(ll a, ll b, ll mod)
{
if (b == 0)
return 1;
ll ans = power(a, b / 2, mod);
ans %= mod;
ans = (ans * ans) % mod;
if ((b & 1) == 1)
return (ans * a) % mod;
return ans % mod;
}
ll stoi(string a, ll mod)
{
ll ans = 0;
for (size_t i = 0; i < a.length(); i++)
ans = ((ans * 10) % mod + a[i] - '0') % mod;
return ans;
}
int main()
{
ll n, m, mod = 1000000007;
ll t;
cin >> t;
string a, b;
while (t--)
{
cin >> a >> b;
n = stoi(a, mod);
m = stoi(b, mod - 1); // using fermats theorem
cout << power(n, m, mod) << endl;
}
return 0;
}

0 comments on commit 3649787

Please sign in to comment.