hdu3826 打表《借鉴思路》

Squarefree number
Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3005    Accepted Submission(s): 757


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
The 6th Central China Invitational Programming Contest and 9th Wuhan University Programming Contest Final


Recommend
lcy
    /*《借鉴》 思路是这样的,10^6不存在因子的话,那么10^6以上最多2个因子,只要判断是否能被开根号就可以了*/
#include <math.h>
#include <stdio.h>
#define MAX 1000000
#define Yes 1
#define No  0
int A[MAX+1]={}; //10^6
void Search(void)
{
    for(int i=2;i<=MAX;i++)
        if(!A[i])
            for(int j=2*i;j<=MAX;j+=i) A[j]=1;
}
int Cal(long long N)
{
    for(int i=2;i<=MAX&&N>1;i++)
    {
        if(A[i]) continue;        //如果不是素数,跳过
        if (!(N%i))
            if (!((N/=i)%i)) return 0;
    }
    if(N==1)     return Yes; //N是10^6内的话按照算数基本定律一定被分解完毕
    //如果不为1只能有一`这里写代码片`种情况 那就是N的因子大于10^6 那么肯定不会存在3个素因子了
    long long n=(long long)sqrt(N+0.5);
    if(n*n==N)   return No;
    else         return Yes;//可优化为 return n*n!=N;
}
int main()
{
    Search();
    int T;scanf("%d",&T);
    for(int i=1;i<=T;i++)
    {
        long long N;scanf("%I64d",&N);
        printf("Case %d: %s\n",i,Cal(N)?"Yes":"No");
    }
    return 0;
}`这里写代码片`
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值