Three powers
Time Limit:1s | Memory limit:32M |
Accepted Submit:96 | Total Submit:217 |
Consider the set of all non-negative integer powers of 3. S = { 1, 3, 9, 27, 81, ... } Consider the sequence of all subsets of S ordered by the value of the sum of their elements. The question is simple: find the set at the n-th position in the sequence and print it in increasing order of its elements. Input Each line of input contains a number n, which is a positive integer with no more than 19 digits. The last line of input contains 0 and it should not be processed.Output For each line of input, output a single line displaying the n-th set as described above, in the format used in the sample output.Sample input 1 7 14 783 1125900981634049 0 Output for sample input { } { 3, 9 } { 1, 9, 27 } { 3, 9, 27, 6561, 19683 } { 59049, 3486784401, 205891132094649, 717897987691852588770249 } Original: Waterloo |
解题:
题目的意思是,3的次幂所组成的集合S,按从小到大排列组合成一些数,最小的为空,比如:{},{1},{3},{1,3},{9}···刚开始还是没发现规律,查了下资料发现二进制关系。如前几项组合成的数为0,1,3,4,9,10,12····相对应的项数为0,1,2,3,4,5,6··化为二进制0,1,10,11,100,101··于是可以发现二进制上为1的,S集合中相对应的项则有出现,但是项数是从0开始的,所以程序取的n要减去1。题目要求n是最多19位的数,预计3的次幂可能会很大,采用预先生成3的64以下的各次幂。
生成方法如下:(需要调用大数模板)
以下是本题的代码: