Pages

Wednesday 10 October 2012

UVA - 374 - Big Mod (C solution)

#include <stdio.h>

int mod(long b, long p, long m)
{
    if(p==0)
        return 1;
    if(p%2==0)
        return (mod(b,p/2,m)*mod(b,p/2,m))%m;
    return (mod(b,p-1,m)*(b%m))%m;
}
int main()
{
    int b,p,m;
    while(scanf("%d %d %d",&b,&p,&m)==3){
        printf("%d\n",mod(b,p,m));
    }
    return 0;
}

2 comments:

  1. i want to learn recursive function . how can i improve ?

    ReplyDelete
    Replies
    1. Just try to learn to solve more algorithm problems ... on Recursive Backtracking in the Problem Solving Paradigms .. in the book Competitive Programming II or just use uHunt

      Delete