Squarefree number
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1713 Accepted Submission(s): 453
Problem Description
In mathematics, a squarefree number is one which is divisible by no perfect squares, except 1. For example, 10 is square-free but 18 is not, as it is divisible by 9 = 3^2. Now you need to determine whether an integer is squarefree or not.
Input
The first line contains an integer T indicating the number of test cases.
For each test case, there is a single line contains an integer N.
Technical Specification
1. 1 <= T <= 20
2. 2 <= N <= 10^18
For each test case, there is a single line contains an integer N.
Technical Specification
1. 1 <= T <= 20
2. 2 <= N <= 10^18
Output
For each test case, output the case number first. Then output "Yes" if N is squarefree, "No" otherwise.
Sample Input
2 30 75
Sample Output
Case 1: Yes Case 2: No
/*
由欧拉定理:任何一个数可以表示为 sum = a^a1*b*^b1*...*d*^d1 ;(其中a,b,c..d为质数)
思路:2-10^18之内的数先判断能否有两个以上的质数相乘,
如果是则输出"No";相除得到sum == 1输出“Yes”。
要么就是10^6和10^18之间的数相乘,只要判断是否能开根号即可
在判断
*/
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 1000000
int sieve[N + 1];
int prime[N+1],k=0;
//质数表
void Cprime(){
int i;
for( i = 2; i <= N; i++) sieve[i] = 1;
for(i = 2; i <= N / 2; i++) sieve[i * 2] = 0;
int p = 2;
while(p * p <= N)
{
p = p + 1;
while(sieve[p] == 0)
p++;
int t = p * p;
int s = 2 * p;
while(t <= N)
{
sieve[t] = 0;
t = t + s;
}
}
for(i=2;i<=N;i++)
if(sieve[i]!=0)
prime[k++]=i;
return ;
}
int main(void)
{
int t , count = 0 ;
scanf("%d",&t);
Cprime();
while( t--)
{
int i , flag = 0 ;
__int64 sum ;
scanf("%I64d",&sum);
__int64 n = sum ;
for( i = 0 ; i < k && prime[i] <= sqrt(sum*1.0) ;i++)
{
int cnt = 0 ;
while(sum % prime[i] == 0)
{
cnt ++ ;
sum /= prime[i] ;
}
if(cnt >= 2){
flag = 1;
break;
}
}
cout<<"Case "<<++count<<": ";
if(flag)
{
cout<<"No"<<endl;
continue ;
}
if(sum == 1)
{
cout<<"Yes"<<endl;
continue ;
}
double s = sqrt(n*1.0) ;
if( (s - floor(s))<0.00001)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
}
return 0 ;
}