miller_rabin
2025-08-27 13:26:57
发布于:广东
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll qpow(ll a,ll b,int mod){
	ll ans=1;
	while(b){
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
bool witness(ll a,ll n){//true n->合数 
	ll u=n-1;
	int t=0;
	while((u&1)==0) u>>=1,++t;
	ll x1,x2;
	x1=qpow(a,u,n);
	for(int i=1;i<=t;++i){
		x2=qpow(x1,2,n);
		if(x2==1 and x1!=1 and x1!=n-1) return true;
		x1=x2;
	}
	if(x1!=1) return true;
	else return false;
}
int miller_rabin(ll n,int s){
	if(n<2) return 0;
	if(n==2) return 1;
	if(!(n&1)) return 0;
	for(int i=0;i<s and i<n;++i){
		ll a=rand()%(n-1)+1;
		if(witness(a,n)) return 0;
	}
	return 1;//n is a prime
}
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	cout.tie(0);
	
} 
全部评论 1
%%%学MR了
2025-08-27 来自 广东
0其实早就学了,扔个模板上来而已
2025-08-27 来自 广东
0红温了不知道错哪了有道题换成mr WA on#17
2025-08-27 来自 广东
0选靠谱点的质数吧
2025-08-27 来自 广东
0















有帮助,赞一个