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 13:45:58
2017-05-27 13:43:59
Task:Book writing
Sender:HIIT AND RUN
Submission time:2017-05-27 13:45:58
Status:READY
Result:ACCEPTED

Show test data

Code

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;


ll power(ll n, ll m) {
    
    if ( n == 0) return 1 % m;
    if (n == 1) return 3 % m;
    
    if (n % 2 == 0) {
        ll lol = power(n/2, m);
        return ((lol % m) * (lol % m)) % m;
        
    }
    ll lol = power((n-1) / 2, m);
    return (3 * ((lol % m) * (lol % m)) % m)%m;
}

int main() {
    ios_base::sync_with_stdio(0);

    ll n, m;
    cin >> n >> m;
    
    cout << (power(n-1, m)) << endl;
    
}