java高精度hdu4043

可以推出公式

f[n]=f[n-1]+f[n-1]*2*(n-1)

f[1]=1;

数据量很大,最后又要进行gcd操作,java里竟然自带了一个gcd的函数,为了避免求大数取余和大数除法操作,还是用java比较快,而且这题对时间复杂度要求不是很高。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.math.BigInteger;
import java.util.Scanner;

/**
 *
 * @author chen
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner cin=new Scanner(System.in);
        //System.out.println("How many number do you need to draw"); 
        int T;
        T=cin.nextInt();
        while(T!=0)
        {
            T--;
            int n;
            n=cin.nextInt();
            BigInteger up= BigInteger.valueOf(1);
            BigInteger down= BigInteger.valueOf(2);
            BigInteger tmp= BigInteger.valueOf(1);
            for(int i=2;i<=n;i++)
            {
                tmp=up;
                BigInteger tmp1= BigInteger.valueOf( 2*(i-1) );
                //System.out.println(tmp1);
                tmp=tmp.multiply(tmp1);
                up=up.add(tmp);
                //System.out.println(up);
                tmp1=BigInteger.valueOf(2*i);
               // System.out.println(tmp1);
                down=down.multiply(tmp1);
                //System.out.println(down);
            }
            //System.out.println(up);
            //System.out.println(down);
            tmp=up.gcd(down);
            up=up.divide(tmp);
            down=down.divide(tmp);
            System.out.println(up+"/"+down);
        }
    }
}

 

还有就是Netbeans IDE 简洁又好用! 

 

posted @ 2013-07-19 15:17  chenhuan001  阅读(288)  评论(0编辑  收藏  举报