Squarefree number

Squarefree number

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1125    Accepted Submission(s): 274
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

题意很简单,就是然你判断一个数能不能被一个数的平方整除(注意这个数的范围是1~10^18)

以下转载于:

http://hi.baidu.com/304467594/blog/item/f02407ea9eeecac62f2e2136.html

/* 要判断给定的n(2<=n<=10^18)是不是squarefree number; 因为给定的数最大达到10^18,所以直接暴力肯定是杯具的TLE; 10^18=10^6^2*10^6; */

下面是神龙的解题思路,我跳过了第三步; /* 1. 计算1000000以下 素数,存于 prime[ ]

2. 计算N的 小于1000000的因子p,如果有平方因子,则No,转 5     否则 N=N/p 3.  消除N的小于 1000000的因子p后,若N=1 , 则Yes,转 5 4.  N必为 3种之一: 素数,素数的平方,两个不同素数之积。    因此判断 N是否平方数,是平方数则No,不是则Yes 5. 结束  */

自己的体会:一个数可以写成n个素数的乘积的形式如n=p1^k1*p2^k2*p3^k3....*pn^kn当ki>=2时就是no了;对于pi<10^6所对应的ki是否>=2很容易判断;如果n存在大于10^6的素数的话,最多存在两个这样的素数,当这两个素数相等的时候就no了,其他情况都是yes;

 

#include<stdio.h>
 #include<string.h>
 #include<math.h>
 #define nmax 1000001
 int prime[nmax], plen;
 void init() {
     memset(prime, -1, sizeof(prime));
     int i, j;
     for (i = 2; i < nmax; i++) {
         if (prime[i]) {
             for (j = i + i; j < nmax; j += i) {
                 prime[j] = 0;
             }
         }
     }
     for (i = 2, plen = 0; i < nmax; i++) {
         if (prime[i]) {
             prime[plen++] = i;
         }
     }
 }
 int solve(long long n) {
     int i;
     for (i = 0; (i < plen) && (prime[i] < n); i++) {
         if (n % prime[i] == 0) {
             n /= prime[i];
             if (n % prime[i] == 0) {
                 return 0;
             }
         }
     }
     return 1;
 }
 int main() {
 
     init();
     int i, t, te, flag;
     long long n;
     scanf("%d", &t);
         for (i = 1; i <= t; i++) {
             scanf("%I64d", &n);
             flag = solve(n);
             printf("Case %d: ", i);
             if (flag) {
                 te = (int) (sqrt(n * 1.0));
                 if (((long long) (te)) * te == n) {
                     printf("No\n");
                 } else {
                     printf("Yes\n");
                 }
             } else {
                 printf("No\n");
             }
         }
 
     return 0;
 }

转载于:https://www.cnblogs.com/Ac007/archive/2012/04/27/2473976.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值