Squarefree number HDU - 3826(数论)

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

题意就是判断一个数是否有平方因子数,只要先在1000000的范围内的素质去判断是否某个素数的因子大于等于两个,有就直接返回false,并且出掉这个素数,如果发现最后原来的数等于1,说明满足要求返回true,如过大于1,那必然含有大于1000000的素因子,这个的素因子不会超过两个,如果大于两个就超long long了,所以只要看这个数是不是一个平方数即可

#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
bool p[1000006];
vector<int> prime;
void getPrime()
{
    for(int i=2;i<=1000000;i++)
        if(!p[i])
        {
            prime.push_back(i);
            for(int j=i+i;j<1000000;j+=i)
                p[j]=true;
        }
}
bool check(long long n)
{
    for(int i=0;i<prime.size();i++)
    {
        if(n%prime[i]==0)
        {
            n/=prime[i];
            if(n%prime[i]==0)
                return false;
        }
    }
    if(n==1)
        return true;
    long long k=(long long)sqrt(n);
    if(k*k==n)
    return false;
    return true;
}
int main()
{
    getPrime();
    int t;
    scanf("%d",&t);
    int ca=1;
    long long n;
    while(t--)
    {
        scanf("%lld",&n);
        printf("Case %d: ",ca++);
        if(check(n))

        printf("Yes\n");
        else
        printf("No\n");
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值