Cod sursa(job #2363042)

Utilizator GhSamuelGherasim Teodor-Samuel GhSamuel Data 3 martie 2019 12:37:48
Problema Invers modular Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
long long A, N;

void read()
{
    f >> A >> N;
}

long long expLg(int x, int p)
{
    int y = 1;
    while (p > 1) {
        if (p & 1)
            y = (x * y) % N;
        x = (x * x) % N;
        p >>= 1;
    }
    return (x * y) % N;
}

long long phi(long long x)
{
    long long res = x;
    for (int i = 2; i * i <= x; ++i)
        if (x % i == 0) {
            while (x % i == 0)
                x /= i;
            res = (res / i) * (i - 1);
        }
    if (x > 1)
        res = (res / x) * (x - 1);
    return res;
}

void show()
{
    g << expLg(A, phi(N) - 1);
}

int main()
{
    read();
    show();
    return 0;
}