HDU 3826 Squarefree number 唯一分解定理
2017-04-16 23:32
387 查看
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
2 30 75
Sample Output
Case 1: YesCase 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;
}
相关文章推荐
- HDU 3826 Squarefree number 唯一分解定理
- HDU 3826 Squarefree number 唯一分解定理
- HDU 3826 Squarefree number 唯一分解定理
- HDU 3826 Squarefree number 唯一分解定理
- HDU 3826 Squarefree number 唯一分解定理
- HDU 3826 Squarefree number 唯一分解定理
- HDU 5428 质因数分解(唯一分解定理)
- GCD and LCM HDU - 4497 (唯一分解定理)
- HDU 3398 String(整数唯一分解定理)
- HDU 1452 Happy 2004(唯一分解定理)
- HDU GT and numbers (整数的唯一分解定理)
- hdu 5428 The Factor(唯一分解定理)
- HDU 1299分解素因子--唯一分解定理
- HDU 1215 七夕节 【约数和定理&&唯一分解定理(模板)】
- hdu 1215 求约数和 唯一分解定理的基本运用
- HDU 2053 Switch Game(开灯问题,唯一分解定理)
- HDU 1452 Happy 2004 (唯一分解定理 + 求等比数列前n项和)
- [ACM] HDU 3398 String (从坐标0,0走到m,n且不能与y=x-1相交的方法数,整数唯一分解定理)
- HDU 2608 - 0 or 1 (数论 + 唯一分解定理)
- HDU-1215 七夕节 数论 唯一分解定理 求约数之和