不定方程(Exgcd)
1 #include<cstdio> 2 using namespace std; 3 int x,y; 4 inline int abs(int a){return a>0?a:-a;} 5 int exgcd(int a,int b){ 6 if(!b){x=1,y=0;return a;} 7 int d=exgcd(b,a%b),t=x; 8 x=y,y=t-(a/b)*y; 9 return d; 10 } 11 int main(){ 12 int a,b; 13 scanf("%d%d",&a,&b); 14 int d=exgcd(a,b); 15 if(c%d==0){ 16 x*=c/d,t=abs(b/d); 17 x=(x%t+t)%t; 18 printf("%d",x); 19 } 20 return 0; 21 }
来自一只刚参加过NOIP的蒟蒻