HDU-3826-Squarefree number

HDU-3826-Squarefree number

            Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

题目链接:HDU 3826

题目大意:判断n是不是完全平方数,既约数是否含有平方数。

题目思路:定理:一个
1.素数筛选法打表出1e6以内的素数
2.1e6以内暴力,并且做一个处理,把n除以能整除的素数。
3.因为数字很大到10^18,但是因为已经处理了10^6,剩下的n的情况可能是:素数,素数的平方,素数x素数。所以只需要判断n是不是平方数即可。

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;

long long prm[1000010];
const int MAXV = 1e7; 
bool isPrime[MAXV+1]; 
int size=0; 
void getPrime()  
{  
    memset(isPrime, true, sizeof(isPrime));
    int sq = sqrt((double)MAXV) + 1; 
    int i,j,k;  
    for(i = 2;i <= sq; i++)  
        if(isPrime[i])  
    for(j = 2,k = MAXV/i+1;j < k;j++)  
        isPrime[i*j] = false;  
    for( i = 2 ; i <= MAXV; i++)  
        if(isPrime[i])    
            prm[size++] = i;
    isPrime[0] = isPrime[1] = false;
}  

int main(){
    getPrime();
    int t;
    scanf("%d",&t);
    int sjy = 1;
    while(t--)
    {
        long long n;
        scanf("%lld",&n);
        int ok = 0,ans = 1;
        for (int i = 0; i < size && prm[i] < n; i++)
        {   
            if (n % prm[i] == 0)
            {
                n /= prm[i];
                if (n % prm[i] == 0)
                {
                    ans = 0;
                    break;
                }           
            }
        }
        if (ans)
        {
            double temp = sqrt(n * 1.0);
            if ((int)(temp + 0.5) == temp) ans = 0;
            else ans = 1;
        }   
        if (!ans) printf("Case %d: No\n",sjy++);
        else printf("Case %d: Yes\n",sjy++);
    }
    return 0;
}

你可能感兴趣的:(HDU,3826)