Code Submission Evaluation System Login

CSES - HIIT Open 2017

HIIT Open 2017

Contest start:2017-05-27 11:00:00
Contest end:2017-05-27 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard


History
2017-05-27 12:46:03
2017-05-27 12:43:34
Task:Book writing
Sender:KnowYourArchitecture
Submission time:2017-05-27 12:46:03
Status:READY
Result:ACCEPTED

Show test data

Code

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long int ull;

ull modpow(ull a, ull p, ull m) {
    if (p == 0) return 1;
    if (p%2 == 0) {
        a = modpow(a, p/2, m);
        return (a*a)%m;
    }
    return (a*modpow(a, p-1, m))%m;
}

int main() {
    ull n, m;
    cin >> n >> m;
    
    if (m == 1)
        cout << "0" << endl;
    else
        cout << modpow(3, n-1, m) << endl;
    return 0;
}