HDU3826 Squarefree number(素数筛选)

Squarefree number

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3542    Accepted Submission(s): 909


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
 

Output
For each test case, output the case number first. Then output "Yes" if N is squarefree, "No" otherwise.
 

Sample Input
 
  
23075
 

Sample Output
 
  
Case 1: YesCase 2: No
 


/*
题目大意:判断n的约数是否含有平方数

思路:
	1.素数筛选法打表
	2.看1e6内的素数是否能整除n 
	3.因为n<=10^18, 经过步骤2处理,n剩下的情况就是:
	  素数、素数的平方、素数成素数。所以步骤2后判断
	  n是否是平方数即可 
*/

#include<bits/stdc++.h>

using namespace std;
const int maxn = 1e6+10;
bool prime[maxn];
vector<int>isprime;

//1.打一张素数表 
void init(){
	for(int i=2;i*i<maxn;i+=i){
		if(!prime[i]){
			for(int j=i;j*j<maxn;j+=j){
				prime[j]=1;
			}
		}
	}
	//将素数存在vector数组中 
	for(int i=2;i<maxn;i++){
		if(!prime[i]) isprime.push_back(i);
	}
}

int main(){
	int t,f;
	long long n;
	init();
	scanf("%d",&t);
	for(int k=1;k<=t;k++){
		f=0;
		scanf("%lld",&n);
		
		//2.判断能否被素数整除 
		for(int i=0;i<isprime.size();i++){
			if(n%isprime[i]==0){
				n/=isprime[i];
				if(n%isprime[i]==0){
					n/=isprime[i];
					f=1;
					break;
				}
			}
		}
		
		if(n>1000000){
			long long kk=sqrt(n);
			if(kk*kk==n) f=1;
		}
		if(f) printf("Case %d: No\n",k);
		else printf("Case %d: Yes\n",k);
	}
} 

猜你喜欢

转载自blog.csdn.net/rvelamen/article/details/80729423