https://www.acwing.com/problem/content/880/
#include <bits/stdc++.h>
using namespace std;
int ex_gcd(int a,int b,int &x,int &y)
{
if (!b)
{
x=1,y=0;
return a;
}
int d=ex_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int a,b,m;
int x,y;
cin>>a>>b>>m;
int d=ex_gcd(a,m,x,y);
if(b%d)
puts("impossible");
else
printf("%lld\n",(long long)b/d*x%m);
}
return 0;
}
来源:https://blog.csdn.net/qq_43716912/article/details/99684587