Cod sursa(job #1959294)

Utilizator MaligMamaliga cu smantana Malig Data 9 aprilie 2017 12:34:51
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <sstream>

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

typedef long long ll;

ll A,N;

ll getPhi(ll);
ll pw(ll,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 x) {
    ll ans = x;
    for (ll i=2;i*i <= x;++i) {
        if (x % i != 0) {
            continue;
        }

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

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

    return ans;
}

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

    return ans;
}