http://acm.nyist.net/JudgeOnline/problem.php?pid=520

來源:互聯網
上載者:User
最大素因子時間限制:1000 ms | 記憶體限制:65535 KB難度:2
描述

i c e最近正在學習數論中的素數,但是現在他遇到了一個難題:給定一個整數n,要求我們求出n的最大素因子的序數,例如:2的序數是1,3的序數是2,5的序數是3,以此類推. 研究數論是需要很大的耐心的,為了懲罰那些沒有耐心讀完題目的童鞋,我們規定:1的最大素因子序數是0.

輸入
有多組測試資料,每一行輸入一個數字n.(0<n<=1000000)
輸出
在接下來的一行,輸出結果.
範例輸入
12345
範例輸出
01213
 #include<iostream>#include<string.h>#include<algorithm>#include<cstdio>#include<cmath>#define N 1000005using namespace std;int prim[N]={1,1,0};int a[N];int b[N];int tot;void init(){           memset(b,0,sizeof(b));      for(int i=2;i<=N/2;++i)        for(int j=2*i;j<=N;j+=i)         {     b[j]=max(i,b[j]);               prim[j]=1;         }            tot=1;         for(int i=2;i<=N;++i)              if(!prim[i])               a[i]=tot++;}int main(){    int n;    init();    while(~scanf("%d",&n))    {      if(n==1) printf("0\n");      else if(n==2) printf("1\n");      else      {          if(!prim[n]) printf("%d\n",a[n]);          else printf("%d\n",a[b[n]]);      }    }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.