HDU 1058 Humble Numbers (dp+打表)

來源:互聯網
上載者:User

標籤:hdu 1058   humble numbers   dp   a number whose   only prime factors   


先是想篩法素數表啊,然後1~2000000000枚舉打表啊,結果越想越不對。

後來想到唯一分解定理,可是怎麼實現呢。。果然還是需要努力啊。。

研究了discuss代碼,碼之~

~~~~

dp的思想,若dp[i]是Humble Numbers,那麼dp[i]*2,dp[i]*3,dp[i]*5,dp[i]*7都將是Humble Numbers。

所以只需要注意連續性便好了。

#include<cstdio>#include<algorithm>#include<cmath>#define LL __int64#define N 6000using namespace std;LL f[N]={0,1};void dp(){    int x,y,p,q;    x=y=p=q=1;    for(int i=2;i<=5842;i++)    {        //取最小值以保持連續性        f[i]=min(f[x]*2,min(f[y]*3,min(f[p]*5,f[q]*7)));          if(f[i]==f[x]*2) x++;   //取下一個作為因子        if(f[i]==f[y]*3) y++;        if(f[i]==f[p]*5) p++;        if(f[i]==f[q]*7) q++;    }}int main(){    dp();    int n;    while(~scanf("%d",&n),n)    {        //輸出注意11,12,13,113,···的情況。        if(n%10==1 && n%100!=11)            printf("The %dst humble number is %I64d.\n",n,f[n]);        else if(n%10==2 && n%100!=12)            printf("The %dnd humble number is %I64d.\n",n,f[n]);        else if(n%10==3 && n%100!=13)            printf("The %drd humble number is %I64d.\n",n,f[n]);        else            printf("The %dth humble number is %I64d.\n",n,f[n]);    }    return 0;}

~~~~

看到個漂亮的暴力代碼。如下~

#include<stdio.h>#include<math.h>#include<algorithm>using namespace std;const int maxn = 2000000000;int main(){    __int64 i, j, k, r, g = 1, a[6000];    for(i=0; i<31 && pow(2,i)<=maxn; i++)        for(j=0; j<20 && pow(2,i)*pow(3,j)<=maxn; j++)            for(k=0; k<14 && pow(2,i)*pow(3,j)*pow(5,k)<=maxn; k++)                for(r=0; r<12 && pow(2,i)*pow(3,j)*pow(5,k)*pow(7,r)<=maxn; r++)                    a[g++] = pow(2,i)*pow(3,j)*pow(5,k)*pow(7,r);    sort(a+1,a+g+1);    int t;    while(~scanf("%d", &t), t)    {         printf("The %d", t);         if(t%10 == 1 && t%100 != 11) printf("st ");         else if(t%10 == 2 && t%100 != 12) printf("nd ");         else if(t%10 == 3 && t%100 != 13) printf("rd ");         else printf("th ");         printf("humble number is %d.\n", a[t]);    }    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.