POJ 3307

题意:一个数,如果能写成某个数各位数的乘积,那么它就是XX数,XX数从1开始算起,然后问你第n个XX数是多少。

题解:考虑某个数的各位乘积得到的x,即所有1~10的数相乘,那么x分解质因数后必定由2、3、5、7组成,所以,所谓第几个XX数就是找第几个只有2,3,5,7的幂次构成的数而已,先打表预处理,直接得答案完事。

View Code
 1 #include<cstdio>

 2 #include<cstring>

 3 #include<algorithm>

 4 using namespace std;

 5 typedef long long LL;

 6 LL ans[100000];

 7 LL MAXN=1000000000000000000LL;

 8 int main()

 9 {

10     int p2,p3,p5,p7,top=2;

11     LL s2,s3,s5,s7;

12     ans[p2=p3=p5=p7=1]=1;

13     s2=2,s3=3,s5=5,s7=7;

14     while(ans[top-1]<=MAXN)

15     {

16         if(s2<s3&&s2<s5&&s2<s7)

17         {

18             if(s2>ans[top-1])

19                 ans[top++]=s2;

20             s2=ans[p2++]*2;

21         }

22         else if(s3<s5&&s3<s7)

23         {

24             if(s3>ans[top-1])

25                 ans[top++]=s3;

26             s3=ans[p3++]*3;

27         }

28         else if(s5<s7)

29         {

30             if(s5>ans[top-1])

31                 ans[top++]=s5;

32             s5=ans[p5++]*5;

33         }

34         else

35         {

36             if(s7>ans[top-1])

37                 ans[top++]=s7;

38             s7=ans[p7++]*7;

39         }

40     }

41     int T;

42     for(scanf("%d",&T);T;T--)

43     {

44         int n;

45         scanf("%d",&n);

46         printf("%lld\n",ans[n]);

47     }

48     return 0;

49 }

你可能感兴趣的:(poj)