广义欧几里得(列表法):
#include<iostream> #include<cstdio> #include<cmath> #include<map> #include<cstring> #include<algorithm> using namespace std; int main() { int a,b,c,d,e,f,g; while(~scanf("%d%d",&a,&b)) { c=0,d=1;e=1,f=0,g=1; printf(" %d %d %d %d\n",a,b,e,f); while(b) { int n=a/b; int h=b; int i=a%b; int j=e-n*c; int k=f-n*d; g=n,e=c;f=d;c=j,d=k,a=h,b=i; printf("%d %d %d %d %d\n",g,a,b,e,f); } } return 0; }
模重复平方法:
#include<iostream> #include<cstdio> #include<cmath> #include<map> #include<cstring> #include<algorithm> using namespace std; int main()//2 251 503 { int c,k,m,d; cout<<"模重复平方法:分别输入底数a,幂k,模m"<<endl; while(~scanf("%d%d%d",&c,&k,&m)) { int i=0,a=1,b=c; printf("设a=%d b0=%d\n",a,b); while(k>=(1<<i)) { if(k&(1<<i))a=a*b%m; b=(b*b)%m; if(i&&k&(1<<i))printf("(%d)x%d=%d\n\ta%d=a%d*b%d=%d,b%d=sqrt(b%d)=%d\n",i+1,i,k&(1<<i),i,i-1,i,a,i+1,i,b); else if(i)printf("(%d)x%d=%d\n\ta%d=a%d=%d,b%d=sqrt(b%d)=%d\n",i+1,i,k&(1<<i)>0,i,i-1,a,i+1,i,b); else if(k&(1<<i))printf("(%d)x%d=%d\n\ta%d=a*b%d=%d,b%d=sqrt(b%d)=%d\n",i+1,i,k&(1<<i),i,i,a,i+1,i,b); else printf("(%d)x%d=%d\n\ta%d=a=%d,b%d=sqrt(b%d)=%d\n",i+1,i,k&(1<<i)>0,i,a,i+1,i,b); i++; } } return 0; }