LS 14 Square free(枚举)

来源:互联网 发布:急用淘宝赚钱 编辑:程序博客网 时间:2024/06/07 09:05

Square free

Test whether nis square free. nis square free if and only if for all p>1,p2is not divisors of n.

Input

The first line contains an integer t, the number of test cases.

The following nlines, each contains an integer n.

(1t102,1n1018)

Output

Print "Yes" if nis square free, or "No" otherwise.

Sample input

21020

Sample output 1

YesNo

思路: 只检查i^3<=n的数,那么最多检查10^6

            如果这个数是a*b*b 如果a>n那么b<n所以可以检查

           如果a<n 那么就把a剔除检查是否剔除完的数是平方数

           这样所有的情况就都包括在内了。

#include<iostream>#include<cstring>#include<cmath>using namespace std;const int mm=1000210;int n;bool p[mm];int f[mm],pos;int main(){  while(cin>>n)  {    while(n--)    {      long long m;      cin>>m;      bool yes=1;      long long i;      for(i=1;i*i*i<=m&&yes;i++)        if(m%i==0)      {        long long z=m/i;        long long y=(long long)sqrt(double(z)+0.01);        if(y*y==z)yes=0;        m/=i;        if(i>1&&m%i==0)yes=0;      }      if(yes)cout<<"Yes\n";      else cout<<"No\n";    }  }}