int exgcd(int a, int b, int &x, int &y)//求x,y使ax+by=gcd(a,b)
{
if (!b)
{
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
线性同余方程求解
int a, b, x, y, m;//求x使ax%m=b%m
cin >> a >> b >> m;
int d = exgcd(a, m, x, y);
if (b % d)
{
cout << "impossible" << endl;
}
else
{
x = b / d * x;
int t = abs(m / d);
cout << (x % t + t) % t << endl; //求最小正整数解
//cout << (ll)x * (b / d) % m << endl; //求出其中一个解
}