FZU 1084 Three powers

Three powers


Time Limit:1sMemory limit:32M
Accepted Submit:96Total 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以下的各次幂。

生成方法如下:(需要调用大数模板

 

以下是本题的代码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值