#include <iostream>
#define LIMIT 5842
#define INTH (long long)1e100
int main() {
long long num[5]={2,3,5,7}, deg[5]={0};
long long ugly[LIMIT]={1}, i, j, cur;
char numq[10], print[10]; int numenq;
for (i=0 ; i<=LIMIT ; i++) ugly[i]=1;
for (i=0 ; i<4 ; i++) deg[i]=0;
for (i=1 ; i<LIMIT ; i++) {
cur=INTH;
for (j=0 ; j<4 ; j++) {
if (cur>ugly[deg[j]]*num[j])
cur=ugly[deg[j]]*num[j];
}
ugly[i]=cur;
for (j=0 ; j<4 ; j++) {
if (cur == (ugly[deg[j]]*num[j]))
deg[j]++;
}
}
while (scanf("%s",numq)==1) {
numenq=atoi(numq);
if (numenq==0) break;
if (numq[strlen(numq)-1]=='1'&&numq[strlen(numq)-2]!='1') strcpy(print,"st");
else if (numq[strlen(numq)-1]=='2'&&numq[strlen(numq)-2]!='1') strcpy(print,"nd");
else if (numq[strlen(numq)-1]=='3'&&numq[strlen(numq)-2]!='1') strcpy(print,"rd");
else strcpy(print,"th");
printf("The %d%s humble number is %lld.\n",numenq,print,ugly[numenq-1]);
}
return 0;
}
Thursday, September 02, 2010
[UVa] 443 - Humble Numbers
This solution uses a simple DP. Keeps a mark on the numbers that are generating the smooths. Then checks for the highest and sets it is the next. A slight modification will solve the 136th Problem, namely ugly numbers.
Subscribe to:
Post Comments (Atom)
Connect Rapoo MT750S with Linux (Tested on Manjaro)
I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...
-
I like coding a lot, keeps me glued to the PC for hours. For that reason it's a need to edit the Syntax Highlighter to suit my eyes for...
-
I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...
-
Install MinGW GCC Port on Windows. 1. Just go to this address [ http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/ ]...
No comments:
Post a Comment
Post your comment here. If you want to say something about programming problems, scripts, software etc, please try to be as descriptive as possible.