標籤:integer name 方便 stat [] valueof content 實用 else
/*對於本題題意非常easy關鍵在於求楊輝三角時的二項式是沒實用到最佳化,導致逾時。對於第n行的二項式的第i個可有第i-1個乘於一個數處於一個數得到,要用到大數。java比較方便。假如n=6,i=4;C(n,i)=C(n,i-1)*(n-i+1)/i;*/import java.io.*;import java.math.*;import java.util.*;import java.text.*;public class Main{ public static void main(String[] args) { int i,j, e,ee,n,s,t; BigInteger f[]=new BigInteger[3100],h[]=new BigInteger[3100],ans,ff,flag,p; Scanner cin = new Scanner (System.in); int k=cin.nextInt(); boolean d=true; while(d) { ans=BigInteger.valueOf(0); n= cin.nextInt(); for(i=1; i<=n; i++) f[i]=cin.nextBigInteger(); h[1]=BigInteger.valueOf(1); for(i=2; i<=n; i++) { e=i-1; ee=n-i+1; flag=BigInteger.valueOf(e); p=BigInteger.valueOf(ee); h[i]=h[i-1].multiply(p); h[i]=h[i].divide(flag); } for(i=1; i<=n; i++) { if(i%2!=n%2) { p=BigInteger.valueOf(-1); p=h[i].multiply(p); f[i]=f[i].multiply(p); ans=ans.add(f[i]); } else { f[i]=f[i].multiply(h[i]); ans=ans.add(f[i]); } } System.out.println(ans); k=k-1; if(k==0) d=false; } }}
hdu 4927 java程式