Cod sursa(job #2050838)

Utilizator MaligMamaliga cu smantana Malig Data 28 octombrie 2017 11:35:28
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_map>

using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");

#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
const int NMax = 1e5 + 5;
const ll inf = 1e18 + 5;

ll A,N;

ll pw(ll,ll);
ll getPhi(ll);

int main() {
    in>>A>>N;

    ll phi = getPhi(N);
    out<<pw(A,phi-1)<<'\n';

    in.close();out.close();
    return 0;
}

ll getPhi(ll nr) {
    ll ans = nr;
    for (ll i=2;i*i <= nr;++i) {
        if (nr % i != 0) {
            continue;
        }

        while (nr % i == 0) {
            nr /= i;
        }

        ans = ans * (i-1) / i;
    }
    if (nr != 1) {
        ans = ans * (nr - 1) / nr;
    }

    return ans;
}

ll pw(ll base,ll exp) {
    ll ans = 1;
    while (exp) {
        if (exp & 1) {
            ans = (ans * base) % N;
        }
        base = (base * base) % N;
        exp >>= 1;
    }

    return ans;
}