Number Theory Introduction to Chapter 8 congruence
We discuss how to solve the congruence $ ax \ equiv c (mod m) $
Obviously this is determined whether or not solvable congruence of determining a linear equation equivalent to \ (ax-my = c \ ) whether solvable
We respect \ (ax + by = c \ ) type of linear equations if and only if \ (gcd (a, b) | c \) when the solvability
Can be solved by way of the following codes to solve (expand Euclid)
//ax+by = gcd(a,b)
long long extend_gcd(long long a,long long b,long long &x,long long &y)
{
if(a==0&&b==0) return -1;
if(b==0) {x = 1; y = 0; return a;}
long long d = extend_gcd(b,a%b,y,x);
y -= a/b*x;
return d;
}
Set \ (g = gcd (a, m) \) then
- If not divisible by g c This equation has no solution
- If \ (g \ mid c \) is exactly the formula it modified with different solutions of g
NOTE: The most important case is the linear congruence equations \ (gcd (a, m) = 1 \) In this case, the same below exactly one solution of formula I
At this time, and c = 1 is referred to a x m of the inverse element
Seeking inverse follows:
long long extend_gcd(long long a,long long b,long long &x,long long &y)
{
if(a==0&&b==0) return -1;
if(b==0) {x = 1; y = 0; return a;}
long long d = extend_gcd(b,a%b,y,x);
y -= a/b*x;
return d;
}
ll getInv(int a,int mod)
{
ll x,y;
ll d=extend_gcd(a,mod,x,y);
return d==1?(x%mod+mod)%mod:-1;
}