Modular exponentiation

//modular exponentiation – my code cant pass over 4 test cases, please rectify it
#include
#define ll long long

using namespace std;

ll power_optimised(ll a, int n) {
ll ans = 1;

while (n > 0) {
	int last_bit = (n & 1);
	if (last_bit) {
		ans = ans * a;
	}
	a = a * a; //Square up
	n = n >> 1; //Discard the last bit of N
}
return ans;

}

int main() {
ll n1,n2,n3;
cin>>n1>>n2>>n3;
ll n4= power_optimised(n1, n2);
cout<<(n4 % n3)<<endl;
return 0;
}

hi @sy7176442_ca8a8991a7ddc322
refer this -->

#include<iostream>
using namespace std;

int main(){
	int a,b,c;
	cin>>a>>b>>c;
	
	long long ans = 1;
	for(int i=1;i<=b;i++){
		ans = (ans * a) % c;
	}
	
	cout<<(ans%c)<<endl;

	return 0;
}

@sy7176442_ca8a8991a7ddc322
I hope it’s clear now??

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.