HDU 1058 humble numbers (Dynamic Regulation + looking for ugly numbers)

Source: Internet
Author: User
Humble numbers Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 16742 accepted submission (s): 7280



Problem descriptiona 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
 
Inputthe 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.
 
Outputfor 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
1234111213212223100100058420
 
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.
 
Sourceuniversity of Ulm Local contest 1996
Recommendjgshining | we have carefully selected several similar problems for you: 1176 1421 1024 1025

Look for patterns. I can only look at other people's code for this kind of weak dish .. Orz.
Multiply by 2, multiply by 3, multiply by 5, multiply by 7, and then change the P2, P3, P5, and P7 values... It is actually a process of searching, and a question.

Each number can be divided into a finite product of 2 3 5 7. The DP equation is DP [I] = f [I] = min (F [a] * 2, min (F [B] * 3, min (F [c] * 5, F [d] * 7 )))

Find the smallest number that is larger than F [I-1] and use a rolling look here;

The following is a detailed explanation:

A indicates the number of subscripts of A in the f [] array. * 2 may obtain the current F [I ].

B indicates that in the f [] array, the number of subscript B * 3 may obtain the current F [I]; if it is, ++

C indicates that in the f [] array, if the subscript is the number of B * 5, the current F [I] may be obtained; if it is, ++

D indicates that in the f [] array, if the subscript is the number of B * 7, the current F [I] may be obtained. If it is, ++

Find the min in them, then f [I];


If the prime factor of a number is 2, 3, 5, or 7, the number is called humble numbers (difference ). Sort positive integers in positive order (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27 ,...) you will find the first 20 humble numbers (difference ). Now, your task is to write a program to find and print the nth humble numbers (difference. Input specification: there are many groups of inputs, where n meets 1 <= n <= 5842. If 0 is entered, it indicates termination. Output specification: for each group of numbers, output "the nth humble number is number. ", where nth must be written as" 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th... ".
English is also used... Orz.
In general, the single digit is 1 to the st, the single digit is 2 to the nd, and the single digit is 3 to the RD. However, except for exceptions, the values 11, 12, and 13 are both th and 111,112,113 are th, all others are th;
Code:
#include <iostream>#include <algorithm>using namespace std;#define M 50000__int64 min(__int64 a,__int64 b,__int64 c,__int64 d){      __int64 x=a<b?a:b;      __int64 y=c<d?c:d;      return x<y?x:y;  }  __int64 vis[M];int main(__int64 p2,__int64 p3,__int64 p5,__int64 p7){       int i=1,n,cur;    p2=p3=p5=p7=1;    memset(vis,0,sizeof(vis)); vis[1]=1;    while(vis[i]<=2000000000)    {        vis[++i]=min(vis[p2]*2,vis[p3]*3,vis[p5]*5,vis[p7]*7);        if(vis[i]==vis[p2]*2) p2++;          if(vis[i]==vis[p3]*3) p3++;          if(vis[i]==vis[p5]*5) p5++;          if(vis[i]==vis[p7]*7) p7++;      }    while(scanf("%d",&n)!=EOF,n)    {        string ss;          if(n%10==1&&n%100!=11)              printf("The %dst humble number is %I64d.\n",n,vis[n]);          else if(n%10==2&&n%100!=12)              printf("The %dnd humble number is %I64d.\n",n,vis[n]);          else if(n%10==3&&n%100!=13)              printf("The %drd humble number is %I64d.\n",n,vis[n]);          else               printf("The %dth humble number is %I64d.\n",n,vis[n]);              }  return 0;}

HDU 1058 humble numbers (Dynamic Regulation + looking for ugly numbers)

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.