標籤: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;}