http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010
http://poj.org/problem?id=1338
First POJ this question to find out the nth ugly number, think of to hit the table, because each ugly number is from the previous ugly number * * or * * or to get, each take 3 kinds of results of the smaller one into the array.
Be careful to keep the array in order when you hit the table.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> -#include <map> the#include <queue> - //#pragma COMMENT (linker, "/stack:102400000,102400000") - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) - + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) - - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineE (x) (1 << (x)) to #defineIabs (x) (x) < 0? -(x): (x) + #defineOut (x) printf ("%i64d\n", X) - #defineLowbit (x) (x) & (-X) the #defineRead () freopen ("A.txt", "R", stdin) * #defineWrite () freopen ("B.txt", "w", stdout); $ #defineMAXN 1010Panax Notoginseng #defineMAXV 1010 - #defineMoD 1000000000 the using namespacestd; +ll p[1555]; A intMain () the { + //freopen ("A.txt", "R", stdin); - intI=1, j=1, k=1; $p[1]=1; $ for(intL=2; l<= the; l++) - { -P[l]=min (p[i]*2, Min (p[j]*3, p[k]*5)); the if(p[l]==p[i]*2) i++; - if(p[l]==p[j]*3) J + +;Wuyi if(p[l]==p[k]*5) k++; the //printf ("%lld\n", P[l]); - } Wu intN; - while(~SCANF ("%d", &n) &&N) About { $printf"%lld\n", P[n]); - } - return 0; -}
51nod This question lets you beg >=n's ugly number, because n is very big, therefore needs two points to look after the table, hits the table to pay attention until is bigger than 10^18, the upper limit can try several times by oneself.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> -#include <map> the#include <queue> - //#pragma COMMENT (linker, "/stack:102400000,102400000") - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) - + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) - - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineE (x) (1 << (x)) to #defineIabs (x) (x) < 0? -(x): (x) + #defineOut (x) printf ("%i64d\n", X) - #defineLowbit (x) (x) & (-X) the #defineRead () freopen ("A.txt", "R", stdin) * #defineWrite () freopen ("B.txt", "w", stdout); $ #defineMAXN 1010Panax Notoginseng #defineMAXV 1010 - #defineMoD 1000000000 the using namespacestd; +ll p[10999]; A the ll Erfen (ll N) + { - intL=1, r=10999; $ while(l<=R) $ { - intMid= (L+R)/2; - if(p[mid]==n)returnP[mid]; the Else if(p[mid]>n) r=mid-1; - ElseL=mid+1;Wuyi } the if(p[l]<n) l++; - returnP[l]; Wu } - intMain () About { $Freopen ("a.txt","R", stdin); - intI=1, j=1, k=1; -p[1]=1; - for(intL=2; l<=10999; l++) A { +P[l]=min (p[i]*2, Min (p[j]*3, p[k]*5)); the if(p[l]==p[i]*2) i++; - if(p[l]==p[j]*3) J + +; $ if(p[l]==p[k]*5) k++; the //printf ("%lld\n", P[l]); the } the //printf ("%lld\n", p[10000]); the intT; - ll N; inscanf"%d",&t); the while(t--) the { Aboutscanf"%lld",&n); the { the if(n==1) printf ("2\n"); the Else +printf"%lld\n", Erfen (n)); - } the }Bayi return 0; the}
51nod 1010 contains only the number of factors 2 3 5 && poj-1338 Ugly Numbers (play table)