Cod sursa(job #1819930)

Utilizator GeoeyMexicanuBadita George GeoeyMexicanu Data 30 noiembrie 2016 23:30:55
Problema Invers modular Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
long i,j,A,N,r=0;
int prim(long x)
{
    int t,ok=1;
    for(t=2;t<=N/2;t++)
        if(N%t==0)
        ok=0;
    return ok;
}
long pew(long x,long y)
{
    long p=1;
    while(y>0)
    {
        if(y%2!=0)
        {
            p=p*x;
        }
        x=x*x;
        y=y/2;
    }
    return p;
}
int main()
{
    f>>A>>N;
    if(prim(N)==1)
    {
        r=N-2;
    }
    else
    {
        for(i=2;i<N;i++)
        {
            if(N%i!=0)
                r++;
        }
    }
    g<<pew(A,r)%N;
}