题目链接:
http://acm.bnu.edu.cn/v3/problem_show.php?pid=7931
分析:
求一个数n(n<10^18)的因子中有没有完全平方数(不包括1)。
直接for找因子一个个判断的话复杂度为10^9,太慢。
一个很好的方法:
分3种情况:
1. n = k^2 (k<10^9)
2. n/i = k^2 (k<10^6&&i<10^6)
3. n/i = i*k (k<10^6&&i<10^6)
第一种情况O(1)的复杂度,第二、三种情况的复杂度为10^6
代码:
#include <cstdio>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#define LL long long
#define db double
#define pi acos(-1.0)
#define pr printf
#define sc scanf
#define mod
#define N
using namespace std;
bool IsSquare(LL n)
{
LL x = sqrt(n);
if(x*x == n)
return 1;
return 0;
}
/*
求一个数n(n<10^18)的因子中有没有完全平方数(不包括1)。
直接for找因子一个个判断的话复杂度为10^9,太慢。
一个很好的方法:
分3种情况:
1. n = k^2 (k<10^9)
2. n/i = k^2 (k<10^6&&i<10^6)
3. n/i = i*k (k<10^6&&i<10^6)
第一种情况O(1)的复杂度,第二、三种情况的复杂度为10^6
*/
int main()
{
int T,cas=0;
LL i,j,n;
bool is = 1;
sc("%d",&T);
while(T--)
{
is = 1;
sc("%I64d",&n);
if(IsSquare(n))
{
is = 0;
}
else
{
for(i=2; i*i<=n&&i<=1000000; ++i)
{
if(n%i==0)
{
LL x = n/i;
if(IsSquare(x))
{
is = 0;
break;
}
}
if(n%(i*i)==0)
{
is = 0;
break;
}
}
}
pr("Case %d: ",++cas);
puts(is?"Yes":"No");
}
return 0;
}