51Nod 1256 求乘法逆元--扩展欧几里德
#include<stdio.h> int exgcd(int a,int b,int &x,int &y) { if(b==0) { x=1; y=0; return a; } int r=exgcd(b,a%b,x,y); int t=x;x=y;y=t-(a/b)*y; return r; } int main(){ int n,m,x,y; while(~scanf("%d%d",&m,&n)){ exgcd(m,n,x,y); while(x<0) x+=n; printf("%d\n",x); } }