天天看点

数论:逆元

#include <cstdio>
#include <cstring>
#include <vector>
#include<queue>
#include<string>
#include<iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int inv[3000010];
/*void exgcd(ll a,ll b,ll &x,ll &y) {
	if(!b){
		x=1,y=0;
		return ;
	}
	exgcd(b,a%b,x,y);
	int temp=x;
	x=y;
	y=temp-a/b*x;
}//扩展欧几里得 
ll inv1(ll a,ll m){//逆元  即 (ax-1)%m==0 则x是a在模m意义下的逆元 
	ll x,y;
	exgcd(a,m,x,y);
	return  (x%m+m)%m ;//x可能是负数需要处理 
}
ll inv2(ll a,ll pow,ll mod){//x=pow(i,m-2)%m;
	a%=mod;//求单个结果用inv1快于inv2 
	long long base=a;
	long long ans=1;
	while(pow){
		if(pow&1){
			ans=(ans*base)%mod;
		}
		base=(base*base)%mod;
		pow>>=1;
	}
	return ans;
}*/
int main() {
   	int n,m;
   	scanf("%d%d",&n,&m);
   	//求1-n内每个数的逆元用这种方法较快 ,复杂度为O(n)
   	inv[1]=1;
	for(int i=2;i<=n;++i){
        inv[i]=(ll)(m-m/i)*inv[m%i]%m;
    }
   	for(int i=1;i<=n;++i){
   		printf("%d\n",inv[i]); 
   	}
   	return 0;
}
           
上一篇: ZOJ-1569
下一篇: τ-p变换