hdu3826 Squarefree number

题目链接:

传送门

题目:

Squarefree number

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2047    Accepted Submission(s): 540


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
 

Author
hanshuai
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3823  3818  3819  1060  3822 


因为数的范围为10的18次方,所以我们考虑打多大的素数表,答案是10的6次方,为什么????
当用10的6次方内的数整除N后,得到的数N如果任然大于10的6次方,说明存在大于10的6次方的因子,此时这个数如果有平方因子,那么最多只可能出现2次,出现3次则大于10的18次方。。。详情参见小白178面。

有个非常详细的讲解的。

传送门  讲的非常好。。

我的代码如下:

#include<cstdio>
#include<cstring>
#include<cmath>
const int maxn=80000+10;
const int  n=1000000;
int prime[maxn],vis[n];
__int64 N;
int  pos;
int init()
{
    int c=0;
    int m;
    memset(vis,0,sizeof(vis));
    m=sqrt(n+0.5);
    for(int i=2;i<=m;i++)
    {
       if(!vis[i])
       {
           for(int j=i*i;j<=n;j+=i)
              vis[j]=1;
       }
    }
    for(int i=2;i<=n;i++)
    {
        if(!vis[i])
            prime[c++]=i;
    }
    return c;
}

int judge()
{
   for(int i=0;i<pos;i++)
      {
        while(N%prime[i]==0)
        {
            N=N/prime[i];
            if(N%prime[i]==0)
              return 0;
        }
      }
      return 1;
}
int main()
{
    __int64 x;
    int i,t,cas=1,ok;
    pos=init();
    scanf("%d",&t);
    while(t--)
    {
        ok=1;
        scanf("%I64d",&N);
        if(!judge())
            ok=0;
        if(N>1000000)
        {
            x=(int)sqrt(double(N));
            if(x*x==N)
                ok=0;
        }
        if(ok)
            printf("Case %d: Yes\n",cas++);
        else
            printf("Case %d: No\n",cas++);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值