Cod sursa(job #427617)

Utilizator hendrikHendrik Lai hendrik Data 28 martie 2010 06:30:32
Problema Invers modular Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

void open(){
    freopen("inversmodular.in","r",stdin);
    freopen("inversmodular.out","w",stdout);
}
typedef long long ll;
int a,n;

ll bigmod(int a,int b){
    if (b==0) return 1;
    if (b==1) return a%n;
    if (b%2==0){
        ll x=bigmod(a,b/2);
        return (x*x)%(ll)n;
    }
    return ((ll)a*bigmod(a,b-1))%(ll)n;
}

int main(){
    open();
    scanf("%d%d",&a,&n);
    /*
    *   a*x=1(mod n)
    *   x=1/a(mod n)
    *   modular inverse= (1 * a^n-2)%n
    */
    printf("%lld\n",bigmod(a,n-2));
    //system("pause");
    return 0;
}