Sunday, November 22, 2015

374 - Big Mod (UVa)

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int bigMod(int b, int p, int m)
{
    int a;
    if(p==0) return 1;
    if(p%2==0)
    {
       a = bigMod(b, p/2, m);
       return (a*a)%m;
    }
    else
    {
       a = bigMod(b, p-1, m)*(b%m);
       return a%m;
    }
}

int main()
{
    int b, p ,m ;
    while(scanf("%d %d %d", &b, &p, &m)!=EOF)
    {
        printf("%d\n", bigMod(b, p, m));
    }
    return 0;
}

No comments:

Post a Comment

Compare equality of two string in C

#include <stdio.h> #include<string.h> int main() {     char* country = "Bangladesh";     char* country2;     ...