Cod sursa(job #2236161)

Utilizator teodorgTeodor G teodorg Data 28 august 2018 13:55:29
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
int a,n,m,e,p;
int putere(int e)
{
    if(e==0)
        return 1;
    int r=putere(e/2);
    r=1LL*r*r%m;
    if(e%2==1)
        r=1LL*r*a%m;
    return r;
}
int main()
{
    f>>a>>n;
    m=n;
    e=1;
    if(n%2==0)
    {
        n/=2;
        while(n%2==0)
        {
            n/=2;
            e*=2;
        }
    }
    for(p=3;p*p<=n;p+=2)
        if(n%p==0)
        {
            n/=p;
            e*=p-1;
            while(n%p==0)
            {
                n/=p;
                e*=p;
            }
        }
    if(n>1)
    {
        e*=n-1;
    }
    g<<putere(e-1);
    return 0;
}