Cod sursa(job #2870816)

Utilizator cdenisCovei Denis cdenis Data 12 martie 2022 16:29:18
Problema Invers modular Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int a,n,ind;

int euler(int n)
{
    int d=2,tot=n;
    while(d*d<=n)
    {
        if(n%d==0)
        {
            while(n%d==0)
                n/=d;
            tot/=d;
            tot*=d-1;
        }
        d++;
    }
    if(n>1)
    {
        tot/=n;
        tot*=n-1;
    }
    return tot;
}

int pow(int x, int p, int MOD)
{
    int rez=1;
    while(p)
    {
        if(p&1)
            rez=(rez*x)%MOD;
        x=(x*x)%MOD;
        p>>=1;
    }
    return rez;
}

int main()
{
    fin >> a >> n;
    ind=euler(n);
    fout << pow(a,ind-1,n);
    return 0;
}