Uva136 (priority queue)

Source: Internet
Author: User

Uva136 (priority queue)

Question:

The number that cannot be divided by prime numbers other than 2, 3, and 5, which is called the ugly number. Find 1,500th ugly numbers;

 

Ideas:

Use priority queue and map to determine the weight;

If x is ugly, 2x, 3x, and 5x are ugly;

Keep releasing the priority queue;

And retrieve the header (minimum number) x;

Determine whether the number has been accessed;

Find 1,500th outputs;

 

 

# Include

 
  
# Include

  
   
# Include

   
    
# Include

    
     
# Include

     # Define ll long longusing namespace std; priority_queue

      
        , Greater 
       
         > Q; map 
        
          M; int main () {q. push (1); m [1] = 1; int count = 0; while (1) {ll t = q. top (); q. pop (); count ++; if (count = 1500) {printf ("The 1500 'th uugly number is % lld. \ n ", t); break;} if (! M [t * 2]) {m [t * 2] = 1; q. push (t * 2);} if (! M [t * 3]) {m [t * 3] = 1; q. push (t * 3);} if (! M [t * 5]) {m [t * 5] = 1; q. push (t * 5) ;}} return 0 ;} 
         
        
      

    

   

  

 


 

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.