Pagini recente » Cod sursa (job #1214139) | Cod sursa (job #1666997) | Cod sursa (job #2335976) | Cod sursa (job #1185800) | Cod sursa (job #1837240)
#include <fstream>
using namespace std;
ifstream cin("inversmodular.in");
ofstream cout("inversmodular.out");
int gcd(int x, int y)
{
if (!y) return x;
return gcd(y,x % y);
}
long long something(long long x, long long y)
{
if (gcd(x,y)!=1) return -1;
if (x==1 || x==y-1) return x;
long long q=(x-1)*y+1;
while (q % x!=0) q-=y;
return q/x;
}
int main()
{
long long a,n,x;
cin >> a >> n;
for (int c,i=1; i<a; i++)
{
c=something(i,n);
if (c==a)
{
cout << i ;
return 0;
}
}
x=(a-1)*n+1;
while (x % a !=0) x-=n;
cout << something(a,n);
return 0;
}