6071: Squarefree number 
时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte
总提交: 49 测试通过:24
View Code
总提交: 49 测试通过:24
描述
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.
输入
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, output the case number first. Then output "Yes" if N is squarefree, "No" otherwise.
样例输入
样例输出
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 }