ICPC Live Archive Uva 4104 - MODEX




Problem Link

Solution in c++

#include<bits/stdc++.h>

using namespace std;

/// Typedef
typedef long long ll;

#define sc1(a) scanf("%lld",&a)
#define sc2(a, b) scanf("%lld %lld",&a,&b)

#define pf1(a) printf("%lld\n", a)
#define pf2(a, b) printf("%lld %lld\n",a,b)

#define mx 1000000
#define PI acos(-1.0)
#define Accepted 0


int bigMod(int base, int power, int mod) {
    if (power == 0)
        return 1 % mod;
    int num = bigMod(base, power / 2, mod);

    num = (num * num) % mod;

    if (power % 2 == 1)
        num = (num * (base % mod)) % mod;

    return num;

}

int main() {

    int testCase;

    cin >> testCase;

    while(testCase--) {
        int x, y, z;

        cin >> x >> y >> z;

        cout << bigMod(x, y, z) << endl;
    }
    return Accepted;
}






No comments

Theme images by Jason Morrow. Powered by Blogger.