极简代码,时间复杂度低

凌艺樽  •  4个月前


#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int N=1e7+10;
const int INF=0x3f3f3f3f;
long long n;
bool f(long long x)
{
	for(int i=2;i<=sqrt(x);++i)
	{
		if(x%i==0 || x==1)
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	//IOS;
	while(cin>>n)
	{
		if((int)sqrt(n)*(int)sqrt(n)==n && f(sqrt(n)))
		{
			cout<<"YES\n";
		}
		else
		{
			cout<<"NO\n";
		}
	} 
    return 0;
}

由题意推导出三质数必须满足:

  1. 完全平方数;
  2. sqrt(n)必须为质数

根据推导完成题目。


Comments: