Regionals 2015 Asia - Daejeon acmliveoj7233 - Polynomial

点击打开链接

Regionals 2015 Asia - Daejeon acmliveoj7233 - Polynomial_第1张图片

Regionals 2015 Asia - Daejeon acmliveoj7233 - Polynomial_第2张图片

Regionals 2015 Asia - Daejeon acmliveoj7233 - Polynomial_第3张图片

Regionals 2015 Asia - Daejeon acmliveoj7233 - Polynomial_第4张图片


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		new H().run();
	}
}

class H {

	void run() {
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
		int T = cin.nextInt() ;
		while(T-- > 0){
			 int t = cin.nextInt()  ;
	         for(int i = 0 ; i <= t ; i++)  c[i] = cin.nextBigInteger() ;


	         for(int d = t+1 ; d >= 1 ; d--){
	            BigInteger rightSum = BigInteger.ZERO  ;
	            for(int it_right = t ; it_right >= d-1 ; it_right--){
	                rightSum = rightSum.add(  c[it_right].multiply(C[it_right][d-1]) ) ;
	            }

	            FenShu rightFenshu = new FenShu(rightSum , BigInteger.ONE) ;
	            for(int it_left = t+1 ; it_left >= d+1 ; it_left--){
	                FenShu nowFenshu = new FenShu(a[it_left].multiply(C[it_left][d-1]) , b[it_left] )  ;
	                rightFenshu = rightFenshu.sub(nowFenshu) ;
	            }

	            FenShu abD = rightFenshu.mult(new FenShu(BigInteger.ONE  , C[d][d-1] ) ) ;
	            a[d] = abD.fenzi ;
	            b[d] = abD.fenmu ;
	         }
	         a[0] = c[0] ;

	         BigInteger sum = BigInteger.ZERO ;
	         for(int i = 0 ; i <= t+1 ; i++) sum = sum.add(a[i].abs()) ;
	         cout.println(sum) ;
		}
		cout.flush();
	}

	final int N = 55;
	BigInteger[] a = new BigInteger[N] ;
	BigInteger[] b = new BigInteger[N] ;
	BigInteger[] c = new BigInteger[N] ;
	
	
	BigInteger[][] C = new BigInteger[N][N];
	{
		C[0][0] = BigInteger.ONE;
		for (int i = 1; i < N; i++) {
			C[i][0] = C[i][i] = BigInteger.ONE;
			for (int j = 1; j < i; j++)
				C[i][j] = C[i - 1][j - 1].add( C[i - 1][j] );
		}
	}

}

class FenShu {
	BigInteger fenzi;
	BigInteger fenmu;

	FenShu(BigInteger zi, BigInteger mu) {
		BigInteger gcd = zi.gcd(mu) ;
		fenzi = zi.divide(gcd);
		fenmu = mu.divide(gcd);
	}

	FenShu sub(FenShu other) {
		BigInteger mu = fenmu.multiply(other.fenmu) ;
		BigInteger zi = (fenzi.multiply(other.fenmu)).subtract( fenmu.multiply(other.fenzi) ) ;
		return new FenShu(zi, mu);
	}

	FenShu mult(FenShu other) {
		BigInteger mu = fenmu.multiply(other.fenmu);
		BigInteger zi = fenzi.multiply(other.fenzi);
		return new FenShu(zi, mu);
	}

}





你可能感兴趣的:(Regionals 2015 Asia - Daejeon acmliveoj7233 - Polynomial)