链接:
https://codeforces.com/problemset/problem/230/B
题意:
给定n个数,只且仅有三个因数的输出YES,其他输出NO;
输入
3
4 5 6
输出量
YES
NO
NO
解:
三个数,其中1和数字本身占一个,另一个只能是该数开根号,所以平方数为其中一个条件,同时发现计算平方数的因数个数也会超时,发现如果根号数为素数时,有且仅有三个因数,因为素数不能拆(乘法)
实际代码:
#include<iostream>
#include<cmath>
using namespace std;
typedef long long int ll;
bool ss(int a)
{
//cout<<"a"<<a<<endl;
int sq=sqrt(a);
if(a>=2)
{
for(int i=2;i<=sq;i++)
{
if(a%i==0)
{
return false;
}
}
//cout<<"duia"<<a<<endl;
}
else return false;
return true;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
ll t;
cin>>t;
ll a=sqrt(t);
if(a*a==t)
{
if(ss(a)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
else cout<<"NO"<<endl;
}
}
限制:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output