Regionals 2015 Asia - Daejeon acmliveoj7233 - Polynomial

点击打开链接




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);
	}

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值