Dev Skill Another Bigmod Problem

//Next Codeforces Round #354 (Div. 2)
//#include<bits/stdc++.h>

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<cmath>
#include<map>

using namespace std;

#define fast ios_base::sync_with_stdio(false)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")

typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;

LL multiple(LL a, LL b, LL c)
{
    if(b==0) return 0;

    LL x = multiple(a , b/2, c)%c;

    if(b%2==0)
    {
        return (x%c+x%c)%c;
    }

    else
    {
        return ((a%c + (x+x)%c))%c;
    }
}

LL bigmod(LL a, LL b, LL c)
{
    if(b==0) return 1;

    LL x = bigmod(a , b/2, c)%c;

    if(b%2==0)
    {
        return multiple(x , x , c)%c;
    }

    else
    {
        return (multiple(a%c , multiple(x , x , c)%c , c))%c;
    }
}

int main()
{
    int t , tc=0;
    LL a, b , c;
    sf("%d", &t);
    for(tc=1; tc<=t; tc++)
    {
        sf("%lld %lld %lld", &a , &b , &c);
        pf("Case %d: %lld\n" , tc, bigmod(a , b , c));
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number