Regionals 2015 Asia - Daejeon acmliveoj7233 - Polynomial

来源:互联网 发布:学php去哪 编辑:程序博客网 时间:2024/05/21 02:53

点击打开链接




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