Cod sursa(job #584927)
Utilizator | Data | 27 aprilie 2011 12:35:09 | |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.53 kb |
#include<stdio.h>
void gcd(long long &x, long long &y, int a, int b)
{
if (!b)
x = 1, y = 0;
else
{
gcd(x, y, b, a % b);
long long aux = x;
x = y;
y = aux - y * (a / b);
}
}
int main(){
freopen("inversmodular.in", "r", stdin);
freopen("inversmodular.out", "w", stdout);
int A, N;
long long inv=0, ins;
scanf("%d %d", &A, &N);
gcd(inv, ins, A, N);
while(inv<=0)inv=N+inv%N;
printf("%d\n", inv);
fclose(stdin);fclose(stdout);
}