Cod sursa(job #2112792)

Utilizator circeanubogdanCirceanu Bogdan circeanubogdan Data 23 ianuarie 2018 20:56:47
Problema Invers modular Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#define ll long long

using namespace std;

ifstream f("inversmodular.in");
ofstream g("inversmodular.out");

long long n, a, phi, inv;

ll get_phi(ll n){
    ll x = n;
    ll d = 2;
    ll p = 0;
    while(n > 1){
        if(!(n % d)){
            x /= d;
            x *= (d - 1);
        }

        while(!(n % d))
            n /= d;
        ++ d;
    }
    return x;
}

ll log_pow(ll x, ll pow){
    ll nr = 1;
    while(pow > 1){
        if(pow % 2){
            nr = (nr * x) % n;
            -- pow;
        }
        x = (x * x) % n;
        pow /= 2;
    }
    return (x * nr) % n;
}

int main()
{
    f>>a>>n;
    phi = get_phi(n);
    inv = log_pow(a, phi - 1);
    g<<inv;
    return 0;
}