Nyoj1032--save Princess —————— "Set Application"

Source: Internet
Author: User

Save Princess time limit:MS | Memory limit:65535 KB Difficulty:2
Describe
yesterday, the princess is kidnapped by a devil. The Prince have to rescue our pretty princess. "OK, if you want to save the beautiful princess, you must answer my questions correctly." The Devil says. "No problem!". "I'll ask you t questions." For each question, I'll tell you a integer n, you must tell me the I th beatuiful number. If your answer is wrong, the princess and you'll all die ". "What's the characteristic of the beautiful number?" Pince asks. "Beautiful numbers is numbers whose only prime factors is 2, 3 or 5.  The sequence 1, 2, 3, 4, 5, 6, 8, 9, ten, ... shows the first 9 beautiful numbers. By convention, 1 is included. Can you help the Prince to save the princess?
Input
The input for each case was an
integer n (1≤n≤5000) and it was terminated by a negative integer.
Output
for each test case, you should print an integer which represents the i th beautiful number.
Sample input
23-1
Sample output
 Nine 


solve the problem: with the priority queue to store the beautiful number, each time from the queue to take the smallest number of beautiful, it expands out three beautiful numbers, with the set of the de-weight function to determine whether to put the set and priority queue. The number of beautiful numbers from small to large comes out of the priority queue in turn.

 #include <stdio.h> #include <string.h> #include < set> #include <queue> #include <vector> #include <algorithm>using namespace std;typedef long Long LL ; const int f[4]={2,3,5};    LL a[5500];void prin (int n) {priority_queue<ll,vector<ll>,greater<ll> >pq;    set<ll>s;    Pq.push (1);    S.insert (1); for (int i=1;;        i++) {LL tm=pq.top ();        A[i]=tm;        Pq.pop ();        if (i>n) break;            for (int i=0;i<3;i++) {LL x=tm*f[i];                if (!s.count (x)) {//returns the number of element x S.insert (x);            Pq.push (x);    }}}}int Main () {int n;    Prin (5010);    while (scanf ("%d", &n)!=eof&&n>0) {printf ("%lld\n", A[n]);
 } return 0;} 

Nyoj1032--save Princess —————— "set app"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.