扩展gcd的模板题,代码如下
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define lowbit(a) ((a)&(-a)) 7 #define max(a, b) ((a)>(b)?(a):(b)) 8 #define min(a, b) ((a)<(b)?(a):(b)) 9 #define MAXN 100010 10 #define PI 3.1415926 11 #define E 2.718281828 12 #define INF 0x777777f 13 typedef long long LL; 14 15 inline int exGcd(int m,int n,int &x,int &y){ 16 int x0=1,y0=0,x1=0,y1=1,r=m%n,q=(m-r)/n;x=0;y=1; 17 while(r){x=x0-q*x1;y=y0-q*y1;x0=x1;y0=y1;x1=x;y1=y;m=n;n=r;r=m%n;q=(m-r)/n;} return n; 18 } 19 int main(){ 20 int a, b, x, y; 21 scanf("%d%d", &a, &b); 22 exGcd(a, b, x, y); 23 printf("%d", (x+b)%b); 24 return 0; 25 }
时间: 2024-10-11 12:50:53