Cod sursa(job #2366164)

Utilizator Mihai145Oprea Mihai Adrian Mihai145 Data 4 martie 2019 18:48:27
Problema Invers modular Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>

using namespace std;

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

int GetPhi(int X)
{
    int phi = X;

    for(int div = 2; div * div <= X; div++)
        if(X % div == 0)
        {
            phi = (phi / div) * (div - 1);

            while(X % div == 0)
                X /= div;
        }

    if(X != 1)
        phi = (phi / X) * (X - 1);

    return phi;
}

int RidPut(int base, int exp, int mod)
{
    int ans = 1;
    int aux = base % mod;

    for(int i = 1; i <= exp; i <<= 1)
    {
        if(i & exp)
            ans = (1LL * ans * aux) % mod;

        aux = (1LL * aux * aux) % mod;
    }

    return ans;
}

int main()
{
    int A, N;
    fin >> A >> N;

    int phi = GetPhi(N);
    fout << RidPut(A, phi - 1, N) << '\n';

    return 0;
}