Cod sursa(job #2433772)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 28 iunie 2019 22:55:30
Problema Invers modular Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream in ("inversmodular.in");
ofstream out("inversmodular.out");
int a, n, i, diviz;
int explog(int x1, int n1)
{
    int p=1;
    int x=x1;
    while(n1>0)
    {
        if(n1&1==1)
        {
            p=(p*x)%n;
            //cout<<p<<" ";
            n1--;
        }
        x=(x*x)%n;
        n1>>=1;
    }
    return p;
}
int ptphi(int x)
{
    int temp=x;
    for(i=2; i*i<=x; i++)
    {
        if(x%i==0)
        {
            while(x%i==0) {x/=i;}
            temp=temp/i*(i-1);
        }
    }
    if(x>1) temp=temp/x*(x-1);
    return temp;
}
int main()
{
    in>>a>>n;
    out<<explog(a, ptphi(n)-1);
    return 0;
}