素数的Miller_Rabbin测试

第一个以2为底的强伪素数为2047。第一个以2和3为底的强伪素数则大到1 373 653。

  微笑




</pre><pre name="code" class="cpp">#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
const __int64 maxn = 500005;
__int64 powe_m(__int64 a,__int64 b,__int64 c)
{
	__int64 ans=1; 
	__int64 tmp=a;
	
	while(b!=0)
	{ 	
		if (b&1) 
			ans=ans*tmp%c;    //不可以写 ans=ans*ans%c 结果会变
		
		tmp=tmp*tmp%c;
		b=b>>1;
	}
	
	
	return ans;	 
}
bool test(__int64 n,__int64 a,__int64 d)
{
	if (n==2)return true;
	if (n==a)return true;
	if  (  (n&1)==0) return false;
	while(!(d&1))  d=d>>1;
	__int64 t=powe_m(a,d,n);
	while((d!=n-1) && (t!=1)&&(t!=n-1))
	{
		t= (__int64 ) t*t%n;
		d=d<<1;
	}
	return (t==n-1 || (d&1)==1);
}
bool isprime (__int64 n)
{
	if (n<2)return false;
	__int64 a[]={2,3,61,7,13,17,19,23,29,31,37};
	for (int i=0;i<11 ;i++)
		if (!test(n,a[i],n-1))
			return false;
		return true;
}


int main()
{
	int t;
	
	int n,i;
	scanf("%d",&t);
	
	while(t--)
	{
		scanf("%d",&n);
 
		if (isprime(n))
			
		{
			printf("yes\n");
		}
		else
		{
			printf("No\n");
		} 
	}
		
		return 0;
	}


你可能感兴趣的:(素数的Miller_Rabbin测试)