Pagini recente » Cod sursa (job #1635911) | Cod sursa (job #707603) | Cod sursa (job #2626193) | Cod sursa (job #744822) | Cod sursa (job #540650)
Cod sursa(job #540650)
/*#include<fstream.h>
#include <math.h>
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
long long c[10000];
//const int n_max = 10001; // Definim numarul maxim de cifre al numerelor
int main()
{
long i,a,n,j,prim,k=0;
long sol;
fin>>a>>n;
if((n+1)%a==0)
{
fout<<(n+1)/a;
return 0;
}
if(a+1==n)
{
fout<<a;
return 0;
}
for(i=2;i<=n;i++)
{
prim=1;
for(j=2;j<=i/2;j++)
if(i%j==0) prim=0;
if(prim==1)
{
k++;
c[k]=i;
}
}
for(i=0;i<=k;i++)
if((a*c[i])%n==1)
{
fout<<c[i];
return 0;
}
while(i<=n-1 && sol%n!=1)
// if( (i&p) >0)
{
i++;
sol=sol+sol;
}
fout<<i-1;
return 0;
}
/*int main()
{
unsigned int i, n, p;
long long a, sol = 1;
fin>>n>>p;
a=n;
for(i=0; (1<<i) <= p;i++)
{
if( ((1<<i) & p) > 0)
sol=(sol*a)%m;
a=(a*a)%m;
}
fout<<sol<<'\n';
return 0;
}*/
#include <stdio.h>
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int A, N;
void gcd(long long &x, long long &y, int a, int b)
{
long long aux;
if (!b)
x = 1, y = 0;
else
{
gcd(x, y, b, a % b);
aux = x;
x = y;
y = aux - y * (a / b);
}
}
int main()
{
long long inv = 0, ins;
fin>>A>>N;
//scanf("%d %d", &A, &N);
gcd(inv, ins, A, N);
if (inv <= 0)
inv = N + inv % N;
//printf("%lld\n", inv);
fout<<inv
return 0;
}