-paozi-

导航

exgcd求乘法逆元

#include<cstdio>
#include<iostream>
using namespace std;
int n,p,x,y;
int exgcd(int a,int b,int &x,int &y){
	int d=a;
	if(b==0){
		x=1;
		y=0;
		return d;
	}
	else{
		exgcd(b,a%b,y,x);
		y-=(a/b)*x;	
	}
	return d;
}

int main(){
	scanf("%d%d",&n,&p);
	for(int i=1;i<=n;i++){
		x=0;y=0;
		exgcd(i,p,x,y);
		while(x<0){
			x+=p;
		}
		printf("%d\n",x);
	}
	return 0;
}

  

posted on 2017-10-31 15:06  -paozi-  阅读(227)  评论(0编辑  收藏  举报