XOJ Square free
Square free
Test whether n is square free. n is square free if and only if for all p>1, p2 is not divisors of n.
Input
The first line contains an integer t, the number of test cases.
The following n lines, each contains an integer n.
(1≤t≤10^2,1≤n≤10^18)
Output
Print "Yes" if n is square free, or "No" otherwise.
Sample input
2
10
20
Sample output 1
Yes
No
直接枚举,但是枚举有个技巧,知道枚举到sqrt3(n)就可以,不然会超时。。
证明:
把那个数开三次方然后枚举就可以输得所有平方因子了。
UUZ的证明:设i是i^3<=n的最大数,对于任意一个数x,如果n%x==0并且x是完全平方数,有x<=i或n/x<=i。
叉姐的证明:有两个case。1. n本身是完全平方数2. 否则,n至少有3个因子,其中至少有一个<= \sqrt[3]{n}
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 int main() 6 { 7 int t; 8 while(~scanf("%d",&t)) 9 { 10 while(t--) 11 { 12 long long n; 13 scanf("%lld",&n); 14 int f=1,f2=0; 15 for(long long i=2;i*i*i<=n;i++) 16 { 17 if(f2==0&&(double)sqrt(n)==(int)sqrt(n)) 18 { 19 f=0; 20 break; 21 } 22 else 23 f2=1; 24 if(n%i==0) 25 { 26 n=n/i; 27 if(n%i==0) 28 { 29 f=0; 30 break; 31 } 32 f2=0; 33 } 34 } 35 if(f) 36 puts("Yes"); 37 else 38 puts("No"); 39 } 40 } 41 return 0; 42 }
posted on 2013-02-05 20:34 acoderworld 阅读(73) 评论(0) 收藏 举报
浙公网安备 33010602011771号