Hdu3826 squarefree number

Source: Internet
Author: User

Question link:

Portal

Question:

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 descriptionin mathematics, a squarefree number is one which is divisible by no perfect squares, limit T 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.
Inputthe 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
Outputfor each test case, output the case number first. Then output "yes" if n is squarefree, "no" otherwise.
Sample Input
23075
 
Sample output
Case 1: YesCase 2: No
 
Authorhanshuai
Sourcethe 6th Central China Invitational programming contest and 9th Wuhan University programming contest final
Recommendlcy | we have carefully selected several similar problems for you: 3823 3818 3819 1060


Because the range of a number is 10 to the power of 18, the factor must be less than 10 to the power of 6, then n * n> 10 to the power of 18, therefore, create a 1000000 prime number table,
The first is the prime number table, which is used to screen the prime number table. The complexity is O (ologn), which is the fastest at present ..
If a number is equal to or greater than 10 to the power of 6 after the total division of prime number 1000000, it is then multiplied after the square is opened. For more information, see section 178.

There is a very detailed explanation.

The portal is very good ..

My code is as follows:

#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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.