Cod sursa(job #2985017)

Utilizator PalcPalcu Stefan Palc Data 25 februarie 2023 15:04:23
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");

long long n, a;

int MakePhi(long long x)
{
    int e, f, phi = x;
    for (f = 2; f * f <= x && x > 1; f++)
    {
        e = 0;
        while (x % f == 0)
        {
            e++;
            x /= f;
        }
        if (e > 0) phi = (phi / f) * (f - 1);
    }
    if (x > 1) phi = (phi / x) * (x - 1);
    return phi;
}

int main()
{
    long long p, sol;
    fin >> a >> n;
    p = MakePhi(n) - 1;
    sol = 1;
    while (p > 0)
    {
        if (p % 2 == 1)
        {
            sol = sol * a % n;
        }
        p /= 2;
        a = a * a % n;
    }
    fout << sol << "\n";
    fin.close();
    fout.close();
    return 0;
}