Untitled

half=(half*half)%m; how half recognize b is the power of a and m is mod.
 avatar
unknown
plain_text
a month ago
428 B
2
Indexable
#include <iostream>
#include<stdio.h>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<bitset>
#include<numeric>
using namespace std;


long long bigmod(long long a,long long b,long long m){

if(b==0)return 1;
long long half= bigmod(a,b/2,m);
half=(half*half)%m;
if(b%2!=0)half=(half*a)%m;
return half;

}
int main(){
    int a,b,m;
    cin>>a>>b>>m;
    cout<<bigmod(a,b,m)<<endl;
    return 0;
}
Editor is loading...
Leave a Comment