Cod sursa(job #1401844)
Utilizator | Data | 26 martie 2015 10:12:46 | |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.53 kb |
#include <stdio.h>
using namespace std;
void inv(int a, int b, long long &x, long long &y){
if(!b){
x = 1;
y = 0;
}else{
inv(b, a%b, x, y);
long long aux = x;
x = y;
y = aux-(a/b)*y;
}
}
int main(){
freopen("inversmodular.in", "r", stdin);
freopen("inversmodular.out", "w", stdout);
int A, N;
long long x = 0, y;
scanf("%d %d", &A, &N);
inv(A, N, x, y);
if(x < 0)
x = N + x % N;
printf("%d\n", x);
return 0;
}