Squarefree number (Euler sieve)

6071: Squarefree number QQ space to share

Time limit (Normal / Java): 1000MS / 3000MS Memory Limit: 65536KByte
total submission: 49 test passes: 24

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.

 

Entry

 

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

 

Export

 

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

 

 

Sample input

Sample Output

 

Case 1: Yes
Case 2: No

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 
 5 int t,num;
 6 ll n;
 7 const int maxn=1e6+6;
 8 int primer[maxn];
 9 int check[maxn];
10 
11 void getprime(){
12     check[0]=check[1]=1;
13     for(int i=2;i<=maxn;i++){
14         if(!check[i]) primer[num++]=i;
15         for(int j=0;j<num;j++){
16             if(i*primer[j]>maxn) break;
17             check[i*primer[j]]=1;
18             if(i%primer[j]==0) break;
19         }
20     }
21 }
22 
23 int main(){
24     ios::sync_with_stdio(false);
25     int jishu=0;
26     getprime();
27     cin>>t;
28     while(t--){
29         cin>>n;
30         bool flag=false;
31         for(int i=0;i<num;i++){
32             int ee=0;
33             while(n%primer[i]==0){
34                 n/=primer[i];
35                 ee++;
36             }
37             if(ee>=2)
38                 flag=true;
39             if(n==1) break;
40         }
41         if(flag) cout << "Case " << ++jishu << ": No" << endl;
42         else{
43             if(n!=1){
44                 ll shu=sqrt(n);
45                 if(shu*shu==n) cout << "Case " << ++jishu << ": No" << endl;
46                 else cout << "Case " << ++jishu << ": Yes" << endl;
47             }
48             else cout << "Case " << ++jishu << ": Yes" << endl;
49         }
50     }
51     return 0;
52 
53 
54 
55 }
View Code

 

Guess you like

Origin www.cnblogs.com/qq-1585047819/p/11748908.html