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
题目大意:算一个数中是否含有平方,例如,10是无平方数输出Yes,但18是可以被9 = 3 ^ 2整除,含有平方数,输出No
思路:10的18次方,寻找是否含有平方,直接暴力枚举,肯定超时,所以仔细思考,利用唯一分解定理,每次都除去可以除去的数不断缩小n,所以10^18,如果存在平方,最大的因子10^6左右,如果再乘一个大于10^6的数则会大于10^18,所以因子只需要讨论到对数开根号或者10^6
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
#define N 1000000
using namespace std;
typedef long long ll;
int main()
{
int t;
ll n;
scanf("%d",&t);
for(int k=1; k<=t; k++)
{
scanf("%lld",&n);
int flag=0;
for(ll i=2;i*i<=n&&i<=N;i++)
{
int cnt=0;
while(n%i==0)
{
cnt++;
n/=i;
}
double a=sqrt(double(n));
ll b=a;
a=a-b;//计算开根号是否正确,可以解决一些大数的问题
if(a==0||cnt>=2)
{
flag=1;
break;
}
}
printf("Case %d: %s\n",k,flag==1?"No":"Yes");
}
return 0;
}