首页 > 其他 > 详细

HDU 2138

时间:2014-09-06 10:53:13      阅读:232      评论:0      收藏:0      [点我收藏+]

这题用MILLER测试应该是不可避免的。

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <time.h>
#define LL __int64
using namespace std;

LL random(LL n){
	return (LL)((double)rand()/RAND_MAX*n+0.5);
}

LL quick(LL a,LL k,LL m){
	LL ans=1;
	while(k){
		if(k&1){
			ans=ans*a%m;
		}
		k>>=1;
		a=a*a%m;
	}
	return ans;
}

bool judgep(LL p){
	for(int i=1;i<=15;i++){
		LL a=random(p-2)+1;
		if(quick(a,p,p)!=a)
		return false;
	}
	return true;
}

int main(){
	int n;
	LL p;
	srand(time(0));
	while(scanf("%d",&n)!=EOF){
		int flag=0;
		for(int i=1;i<=n;i++){
			scanf("%I64d",&p);
			if(judgep(p)){
				flag++;
			}
		}
		printf("%d\n",flag);
	}
	return 0;
}

  

HDU 2138

原文:http://www.cnblogs.com/jie-dcai/p/3959160.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!