Cod sursa(job #1384633)

Utilizator Narcys01Ciovnicu Narcis Narcys01 Data 11 martie 2015 11:40:18
Problema Invers modular Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.46 kb
#include <iostream>
#include <fstream>
#define LL long long

using namespace std;

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

LL A, N;

LL PPP(LL n, LL p)
{
    if (p == 0) return 1;
    if (p == 1) return n;
    if (p % 2 == 0) return (PPP((n * n) % N, p / 2)) % N;
    return (n % N * PPP(n * n, p / 2)) % N;
}

int main()
{
    LL p;
    fin >> A >> N;
    p = N - 2;
    fout << PPP(A, p) % N;
    return 0;
}