2013年1月1日

UVa 10375 - Choose and divide

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner cin = new Scanner(System.in);
    BigDecimal fac[] = new BigDecimal[10001];
    fac[0] = BigDecimal.valueOf(1);
    for (int i = 1; i <= 10000; i++)
      fac[i] = fac[i - 1].multiply(BigDecimal.valueOf(i));
    while (cin.hasNext()) {
      int p = cin.nextInt(), q = cin.nextInt(), r = cin.nextInt(), s = cin.nextInt();
      BigDecimal ans = BigDecimal.ONE;
      ans = ans.multiply(fac[p]);
      ans = ans.divide(fac[q], 1000, BigDecimal.ROUND_HALF_UP);
      ans = ans.multiply(fac[s]);
      ans = ans.divide(fac[p - q], 1000, BigDecimal.ROUND_HALF_UP);
      ans = ans.multiply(fac[r - s]);
      ans = ans.divide(fac[r], 1000, BigDecimal.ROUND_HALF_UP);
      System.out.println(ans.setScale(5, BigDecimal.ROUND_HALF_UP));
    }
  }
}

沒有留言:

張貼留言