UVA136 to find the 1500th ugly number

Source: Internet
Author: User

Enumerate large-range data: Conditions for the examination of violence

#include <iostream>#include<cstdio>#include<vector>#include<algorithm>#include<map>using namespaceStd;vector<int>Prime;typedefLong Longll;BOOLIsPrime (ll x) {ll i; if(x==1)return false;  for(i=2; i*i<=x;++i) {        if(x%i==0)return false; }    return true;} Vector<int>Set;map<int,int>MP;intpri[]={2,3,5,6, the,Ten, -};BOOLJud (ll x) {ll temp=x;  while(temp!=0&&temp%2==0) {Temp/=2; }         while(temp!=0&&temp%3==0) {Temp/=3; }         while(temp!=0&&temp%5==0) {Temp/=5; }        if(temp!=0){            if(IsPrime (temp)) {//printf ("T%d temp%d\n", set[i],temp);                return false; }            Else{                if(temp==1)return true;  for(LL j=2; j*j<=temp;++j) {                    if(temp%j==0){                        //if (temp<100)//printf ("Temp%i64d j%i64d\n", temp,j);                        if(IsPrime (j))return false; }                }                return true; }        }        return true;}intMain () {inti,j; Set.push_back (1);  for(i=2;i<10000000;++i) {        if(Jud (i)) {set.push_back (i);    }} sort (Set.begin (), Set.end ());  for(i=0;i< the;++i) {printf ("%d", Set[i]); } printf ("\ n"); printf ("The "th ugly number is%d"",180625); return 0;}

It turns out that the right answer is 8e9 ... And this enumeration 1e7 is going to run very long.

Gg

In fact, there's no proof that the test is correct.

And then we found out. The number of ugly numbers can be *2,3,5 by the number of ugly numbers and the number of new ugly numbers.

But if you are violent multiply direct map to record the number of haunted occurrences. Direct push without appearing.

And then we found this. Will miss some of the middle numbers.

But the next round of operation I found a set of data in front of

1 2 3 4 5 6 8 9 10 15 25 next round will add 12

Young to think that as long as you get enough to be able to fill the whole.

In fact.. I later took great pains to approximate the maximum value of long long.

The answer is still wrong.

Then we can understand that. There are no more numbers to fill (if the answer output format is not a problem)

So what's the safest way to do it?

For the first number special case we are in the set S push 1

2,3,5, take it again.

2,3,5

Take the smallest push2 that ever happened.

The sequence becomes

2,3,5 multiply it over again

2:2,4

3:3,6

5:5,10

We take the smallest of these six numbers.

Repeat the steps above:

The last sequence we get is the smallest.

When I meet the middle missing item later. We can take only the smallest 1 at a time! To complete the complete sequential sequence.

Actually, I thought so, too.

But my writing and thinking are not consistent. This must be checked more in the future. Is your statement doing what you expect?

Paste the following code:

PS Q giant today said for PE return WA. Just check the blanks and the carriage return.

For 64-bit integers: Some evaluation machine does not support%i64d words. 64 spaces may be returned

But now most of the testing machine should support%LLD.

It's best to test the game ...

#include <iostream>#include<cstdio>#include<vector>#include<map>#include<algorithm>using namespaceStd;typedefLong LongLl;vector<ll>S;map<ll,ll>MP;intp[]={2,3,5};intN;intMain () {scanf ("%d",&N); S.push_back (1); inti,j,flag=0;  while(S.size () <n&&!flag) {        intsz=s.size (); Vector<ll>K;  for(i=0; i<sz&&!flag;++i) {ll T=S[i];  for(j=0;j<3;++j) {                if(mp[p[j]*t]==0) {k.push_back (P[j]*t);        }}} sort (K.begin (), K.end ()); S.push_back (k[0]); mp[k[0]]++;    } sort (S.begin (), S.end ()); printf ("The "th ugly number is 859963392."); return 0;}

The final output is a well-calculated 1500th result. The input is the size of the ugly number.

There is a wrong idea at first to do this question.

My answer is to use 2,3,5,6,20,15,30 as the base to multiply the number of not prime numbers to get the answer.

But.. Numbers that are not prime numbers may still contain a mass factor other than 2,3,5.

So we're going to get rid of this number. So there is the simulation of the large-force test.

8e9.. You're a retard.

UVA136 to find the 1500th ugly number

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.