Cod sursa(job #852953)

Utilizator Luncasu_VictorVictor Luncasu Luncasu_Victor Data 11 ianuarie 2013 22:25:51
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <cassert>
using namespace std;

#define PRO "inversmodular"
void OpenFiles(int EVAL)
{
	if(EVAL)
	{
		char input[100] = PRO, output[100] = PRO;
		freopen(strcat(input, ".in"),"r",stdin);
		freopen(strcat(output,".out"),"w",stdout);
	} else
	{
		freopen("test.in","r",stdin);
		//freopen("test.out","w",stdout);
	}
}

#define MAX  1000001
#define INF 0xffffff

void Euclid(int a,int b,int &d,int &x,int &y)
{
    if(b==0)
    {
        d = a;
        x = 1;
        y = 0;
    } else
    {
        int x0,y0;
        Euclid(b,a%b,d,x0,y0);
        x = y0;
        y = x0 - (a/b)*y0;
    }
}

int main(int argv,char *args[])
{
	OpenFiles(argv==0);
	// start
	int a,b,d,x,y;
		scanf("%d %d",&a,&b);
		Euclid(a,b,d,x,y);
		if(x<0)x += (x/b+1)*b;
		printf("%d\n",x);

	return 0;
}