日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

hdu1058(dp||优先队列)

發(fā)布時間:2025/3/15 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu1058(dp||优先队列) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

丑數(shù):一個數(shù)的素因子只有2,3,5。

Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29812????Accepted Submission(s): 13053

Problem Description

A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers.?

Write a program to find and print the nth element in this sequence

Input

The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.

Output

For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output。

Sample Input

1 2 3 4 11 12 13 21 22 23 100 1000 5842 0

?

Sample Output

The 1st humble number is 1. The 2nd humble number is 2. The 3rd humble number is 3. The 4th humble number is 4. The 11th humble number is 12. The 12th humble number is 14. The 13th humble number is 15. The 21st humble number is 28. The 22nd humble number is 30. The 23rd humble number is 32. The 100th humble number is 450. The 1000th humble number is 385875. The 5842nd humble number is 2000000000.

解析:主要思想:humble number肯定是前面的humble number的*2,*3,*5,*7.就如? x*2,x*3,x*5,x*7,每次取dp最小的數(shù)

#include<bits/stdc++.h> using namespace std;#define e exp(1) #define pi acos(-1) #define mod 1000000007 #define inf 0x3f3f3f3f #define ll long long #define ull unsigned long long #define mem(a,b) memset(a,b,sizeof(a)) int gcd(int a,int b){return b?gcd(b,a%b):a;}ll dp[6000]; int a=1,b=1,c=1,d=1; ll f() {ll t=min(min(2*dp[a],3*dp[b]),min(5*dp[c],7*dp[d]));if(t==2*dp[a])a++;if(t==3*dp[b])b++;if(t==5*dp[c])c++;if(t==7*dp[d])d++;return t; } int main() {dp[1]=1;for(int i=2; i<=5842; ++i){dp[i]=f();}int n;while(scanf("%d",&n),n){printf("The %d",n);if(n % 10 == 1 && n % 100 != 11)printf("st");else if(n % 10 == 2 && n % 100 != 12)printf("nd");else if(n % 10 == 3 && n % 100 != 13)printf("rd");elseprintf("th");printf(" humble number is %d.\n",dp[n]);}return 0; }

priority_queue

#include<bits/stdc++.h> using namespace std;#define e exp(1) #define pi acos(-1) #define mod 1000000007 #define inf 0x3f3f3f3f #define ll long long #define ull unsigned long long #define mem(a,b) memset(a,b,sizeof(a)) int gcd(int a,int b){return b?gcd(b,a%b):a;}ll a[6000]; const int c[4]={2,3,5,7}; int main() {set<ll> s;priority_queue<ll, vector<ll>, greater<ll> >q;s.insert(1);a[1]=1;q.push(1);for(int i=1; i<=5842; i++){ll x=q.top();a[i]=x;q.pop();for(int j=0; j<4; j++){ll y=x*c[j];if(!s.count(y)){s.insert(y);q.push(y);}}}int n;while(scanf("%d",&n),n){printf("The %d",n);if(n % 10 == 1 && n % 100 != 11)printf("st");else if(n % 10 == 2 && n % 100 != 12)printf("nd");else if(n % 10 == 3 && n % 100 != 13)printf("rd");elseprintf("th");printf(" humble number is %d.\n",a[n]);}return 0; }

?

?

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的hdu1058(dp||优先队列)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。