intexgcd(int a,int b,int&x,int&y){if(!b){
x=1,y=0;return a;}int d =exgcd(b,a%b,x,y);int t = x;
x = y;
y = t - a/b * y ;return d;}
线性同余方程
#include<iostream>usingnamespace std;intexgcd(int a,int b,int&x,int&y){if(!b){
x=1,y=0;return a;}int d =exgcd(b,a%b,x,y);int t = x;
x = y;
y = t - a/b * y ;return d;}intmain(){int n;
cin>>n;while(n--){int a,b,x,y,m;
cin>>a>>b>>m;int d =exgcd(a,m,x,y);if(b%d) cout<<"impossible"<<endl;else cout<<(longlong) x * b / d % m<<endl;}return0;}