Grand Prix
# Include <stdio. h>
Void luwei_gcd (int a, int B, int & d, int & x, int & y)
{
If (B = 0) // for ax + by = d and B = 0, there is obviously x = 1 (because gcd (a, B) = d, a = d)
{
D =;
X = 1;
Y = 0;
Return;
}
Luwei_gcd (B, a % B, d, y, x); // a * x + B * y = d can be converted to B * y + (a % B) * (y-x * (a/B) = d
Y-= x * (a/B); // when backtracking x = y, y = y-x * (a/B), update the values of x and y.
}
// Ax limit 1 (mod n) satisfies gcd (a, n) = 1. This problem can be converted to ax + bn = 1. Then, the above function can be used to obtain x
Int shuchu (int a, int n)
{
Int d, x, y;
Luwei_gcd (a, n, d, x, y );
If (d = 1) // ax + bn = 1
{
X = x % n;
If (x <0)
Return (x + n) % n; // The returned value must be a positive number.
Printf ("the reverse element of a mod B is: % dn", x );
Printf ("B mod a's reverse element: % dn", (1-a * (x-26)/n );
}
Else
Printf ("a and B are not mutually exclusive, and there is no reverse element n ");
}
Int main ()
{
Int a, B;
Printf ("enter two positive integers: (a <B) n ");
Scanf ("% d", & a, & B );
Shuchu (a, B );
Return 0;
}