poj1906

组合数学

View Code
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
static void work(BigInteger n) {
BigInteger x
= new BigInteger("2");
int i = 0;
while (x.subtract(BigInteger.valueOf(1)).compareTo(n)< 0)
{
x
= x.multiply(BigInteger.valueOf(2));
i
++;
}
x
= x.divide(new BigInteger("2"));
if (n.equals(x))
{
System.out.print(BigInteger.valueOf(
3).pow(i));
return;
}
work(n.subtract(x));
System.out.print(
", " + BigInteger.valueOf(3).pow(i));
}

public static void main(String[] args) {
Scanner cin
= new Scanner(new BufferedInputStream(System.in));
while (cin.hasNext()) {
BigInteger a
= cin.nextBigInteger();
if (a.equals(new BigInteger("0")))
break;
System.out.print(
"{ ");
if (a.equals(new BigInteger("1"))) {
System.out.println(
"}");
continue;
}
work(a.subtract(
new BigInteger("1")));
System.out.println(
" }");
}
}
}
posted @ 2011-08-13 23:13  金海峰  阅读(166)  评论(0编辑  收藏  举报