hdu3826 Squarefree number

來源:互聯網
上載者:User

標籤:des   style   blog   http   java   color   

題目連結:

傳送門

題目:

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, 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. 
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 3822 


因為數的範圍為10的18次方,那麼它的因子必須是小於10的6次方的,則n*n*n>10的18次方,所以打一個1000000的素數表,
首先是素數表,用篩法打素數表。複雜度為O(ologn),應該是目前來說最快的吧。。
如果一個數在整除素數1000000後任然大於10的6次方的話,則將其開方後再乘。詳情參見小白178面。

有個非常詳細的講解的。

傳送門  講的非常好。。

My Code如下:

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.