HDU 3826 Squarefree number 唯一分解定理

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: 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;
}


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值