Skip to content

Instantly share code, notes, and snippets.

@Remonhasan
Last active April 25, 2020 13:09
Show Gist options
  • Save Remonhasan/7cf76ddaf538b10a044aaa9f67f51b05 to your computer and use it in GitHub Desktop.
Save Remonhasan/7cf76ddaf538b10a044aaa9f67f51b05 to your computer and use it in GitHub Desktop.
Big Mod [ Find Mod Answer ]
/* Author: Remon Hasan
Algorithm: BigMod */
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll BigMod(ll base, ll power, ll mod)
{
if(power==0)
return 1;
else if(power%2==1) // if power is odd ( ধরি , 2^3 % Mod) বের করব-> (((2^2)^2)%mod . (2^1)%mod)%mod এভাবে করলে উত্তর পাব
{
int a = base % mod; // a হলো (2^1)%mod)
int b = (BigMod(base,power-1,mod))%mod; // b হলো (((2^2)^2)%mod
return (a*b)%mod; // উত্তর হলো (((2^2)^2)%mod . (2^1)%mod)%mod
}
else if(power%2==0) // if power is even
{
int a = (BigMod(base,power/2,mod))%mod;
return (a*a)%mod;
}
}
int main ()
{
ll base,power,mod;
cin>>base>>power>>mod;
cout<<BigMod(base,power,mod)<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment