Cod sursa(job #1819923)

Utilizator GeoeyMexicanuBadita George GeoeyMexicanu Data 30 noiembrie 2016 23:17:24
Problema Invers modular Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 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;
long pew(long x,long y)
{
    long p=1;
    while(y!=0)
    {
        if(y%2!=0)
        {
            p=p*x;
            p=p%N;
        }
        x=x*x;
        y=y/2;
        x=x%N;
    }
    return p;
}
int main()
{
    f>>A>>N;
    if(N==1 || N==2 || N==3 || N==5 || N==7 || (N%2!=0 && N%3!=0 && N%5!=0 && N%7!=0 && floor(sqrt(N))!=sqrt(N)))
    {
        r=N-2;
    }
    else
    {
        for(i=2;i<N;i++)
        {
            if(N%i!=0)
                r++;
        }
    }
    g<<pew(A,r);
}