hdu3826-Squarefree number-(欧拉筛+唯一分解定理)

Squarefree number

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

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
 
翻译:输入一个n,如果可以被一个平方数整除,则不是平方自由数,输出no,否则输出yes
分析:显然要分解质因数,根据唯一分解定理分解。
n<=10^18,不能用暴力求到10^9的素数,欧拉筛一般只是找10^6内的素数。显然需要优化。
如果n>10^6,巨大,筛完了10^6内的质因子后,n还是大于10^6,则
1.如果n不是平方自由数,则因子中包含质因数的平方,则n=p*p,p是素数,p>10^6,除此之外没有别的大于10^6的因子了,否则n>10^18
2.如果n是平方自由数,则因子中不包含质因数的平方
(1)n是素数,n接近10^18,temp=sqrt(n),temp接近10^9,但是通过小数点开根号截取后,开根号的数就会变成合数,如果开根号后的数是合数,则可以说明这个n是平方自由数
(2)n不是素数,n=p1*p2,p1,p2>10^6,开根号后temp=sqrt(n)也会变成合数
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
#define ll long long
const int maxx=1e6+5;
bool vis[maxx];
int prime[maxx];
int cnt;
void init()//欧拉筛
{
    cnt=0;
    memset(vis,true,sizeof(vis));
    vis[0]=vis[1]=false;
    for(int i=2;i<=maxx;i++)
    {
        if(vis[i])
            prime[cnt++]=i;
        for(int j=0;j<cnt && prime[j]*i<=maxx;j++)
        {
            vis[ prime[j]*i ] = false;
            if(i%prime[j]==0) break;
        }
    }
}

int main()///hdu3826
{
    init();
    int t;
    ll n;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        bool flag=true;
        scanf("%lld",&n);
        for(int j=0;j<cnt && flag;j++)
        {
            int num=0;///素因子有几次方
            while(n%prime[j]==0)
            {
                num++;
                n=n/prime[j];
                if(num>=2) {flag=false;break;}//可被 平方数 除尽 的数不是平方自由数
            }

        }
        ///出来后的这个n
        ///第一种情况,是一个 大于10^6 的素数, 开根号后必然截取小数点后变成合数
        ///第二种情况,是两个 大于10^6 的素数 的乘积,不可能还有第三个大于10^6的质因子,开根号后必然截取小数点后变成合数
        ///第三种情况,是一个 大于10^6 的素数 的平方,不可能还有第三个大于10^6的质因子,开根号后还是素数
        if(n>1000000)
        {
            ll temp=sqrt(n);///如果是第二种情况,temp是素数,可以判定
            ll sqtemp=sqrt(temp);
            bool flag2=true;
            for(int k=2;k<=sqtemp;k++)
            {
                if(temp%k==0)//temp是合数,表明原来的n是素数,则是平方自由数
                {
                    flag2=false;
                    break;
                }
            }
            if(flag2) //n是素数的平方,则不是平方自由数
                flag=false;
        }
        if(flag)
            printf("Case %d: Yes\n",i);
        else
            printf("Case %d: No\n",i);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shoulinniao/p/10357420.html
今日推荐