UVA 374 Big mod Solution


Solve in C:

#include<stdio.h>
#include<math.h>
long long int big_mod(long long int base,long long int power,long long int mod)
{
    if(power==0)
        return 1;
    else if(power%2==1)
    {
        long long int p1=(base%mod);
        long long int p2=(big_mod(base,power-1,mod))%mod;
        return (p1*p2)%mod;
    }
    else if(power%2==0)
    {
        long long int p1=(big_mod(base,power/2,mod))%mod;
        return (p1*p1)%mod;
    }
}
int main()
{
    long long int b,p,m,r,q1,q2,power,base,mod;
    while(scanf("%lld%lld%lld",&b,&p,&m)!=EOF)

    {
        long long r=big_mod(b,p,m);
        printf("%lld\n",r);

    }

}

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.