日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hdu1058(dp||优先队列)

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

丑數(shù):一個(gè)數(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位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

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

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