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;
}