Cod sursa(job #562717)

Utilizator AndrewTheGreatAndrei Alexandrescu AndrewTheGreat Data 23 martie 2011 19:12:34
Problema Invers modular Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.45 kb
// Folosind Teorema mica a lui Fermat
#include <stdio.h>

using namespace std;

long long rez, N, P, M;

int main()
{
    freopen ("inversmodular.in","r",stdin);
    freopen ("inversmodular.out","w",stdout);

    scanf("%lld %lld",&N,&P);
    M = P;
    P -= 2;
    rez = 1;
    while(P)
    {
        if(P & 1)
            rez = (rez * N) % M;
        N = (N * N) % M;
        P >>= 1;
    }
    printf("%lld",rez);
    return 0;
}