Pagini recente » Cod sursa (job #2359985) | Cod sursa (job #916620) | Cod sursa (job #1888653) | Cod sursa (job #2528749) | Cod sursa (job #852953)
Cod sursa(job #852953)
#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;
}