sdnu 1246 prime 大素数问题

1246.prime

Time Limit: 3000 MS    Memory Limit: 131072 KB
Total Submission(s): 36    Accepted Submission(s): 18

Description

 This is a verrrrrrrrrry easy problem, when Lulichuan was a freshman, he can ease to judge whether a number is a prime, there some TestCase, you just need to judge if the number is a prime, if it’s a prime output  “N”, else output “Y

Input

The first line is T(0 < T < 100000)

Then follow T line, every line is a number n(0 < n <100000000)

Output

As stated in the title

Sample Input

 5 1 2 3 4 5

Sample Output

 Y N N Y N

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long int 
ll gcd(ll x, ll y)
{
	if (!y)
	{
	//	cout << "Asd" << endl;
		return x;
	}
	return (y, x%y);
}
ll pow(ll a, ll x, ll mod)//要用到快速幂
{
	ll ans = 1;
	while (x)
	{
		if (x & 1)
		{
			(ans *= a) %= mod;
		}	
		(a *= a) %= mod;
		x = x / 2;
	}
	return ans;
}
bool mr(ll x)
{
	if (x == 2)
	{
		return true;
	}
	if (x == 1)
	{
		return 0;
	}
	for (ll s = 1; s <= 40; s++)//40次判断就行了
	{
	//	cout << s << endl;
		ll now = rand() % (x - 2) + 2;
		if (pow(now, x - 1, x) != 1)
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	int te;
	scanf("%d", &te);
	ll x;
	while (te--)
	{
		scanf("%I64d", &x);
		if (mr(x))
		{
			printf("N\n");
		}
		else
		{
			printf("Y\n");
		}
	}
	return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值