Cod sursa(job #1689251)

Utilizator llalexandruLungu Alexandru Ioan llalexandru Data 14 aprilie 2016 08:23:31
Problema Invers modular Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>
#include <cmath>

using namespace std;

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

int a, n, r, f, cn;

int Lgput(int a, int x)
{
    int act, sol=1;
    act=a;
    while (x>0)
    {
        if (x%2==1)
        {
            sol *= act;
            sol %= cn;
        }
        act = (act*act)%cn;
        x=x/2;
    }
    return sol%cn;
}

int main()
{
    int i;
    fin>>a>>n;
    cn=n;
    r=sqrt(n);
    f=n;
    for (i=2; i<=r; i++)
    {
        if (n%i==0)
        {
            while (n%i==0)
                n=n/i;
            f-=f/i;
        }
    }
    if (n!=1)
        f-=f/n;
    fout<<Lgput(a, f-1);
    return 0;
}