How+Many+Pieces+of+Land

Problem G How Many Pieces of Land? Input: Standard Input Output: Standard Output Time Limit: 3 seconds

You are given an elliptical shaped land and you are asked to choose n arbitrary points on its boundary. Then you connect all these points with one another with straight lines (that’s n*(n-1)/2 connections for n points). What is the maximum number of pieces of land you will get by choosing the points on the boundary carefully?

Fig: When the value of n is 6.

Input

The first line of the input file contains one integer S (0 < S < 3500), which indicates how many sets of input are there. The next S lines contain S sets of input. Each input contains one integer N (0<=N<2^31).

Output

For each set of input you should output in a single line the maximum number pieces of land possible to get for the value of N.

Sample Input: 4 1 2 3 4

Sample Output: 1 2 4 8

Source Code code format="java5" import java.util.Scanner;

public class HowManyPiecesofLand { public static void main(String[] args) { Scanner scan = new Scanner(System.in);

int S = scan.nextInt; // 테스트 케이스 개수

for (int i = 0; i < S; i++) { int n = scan.nextInt; // 점의 개수

System.out.println(1 + combination(n, 2) + combination(n, 4)); }   }

/**    * 팩토리얼을 구한다. n! = n * (n-1) * (n-2) * ... * 2 * 1 ex) 4! = 4 * 3 * 2 * 1 =    * 24     *     * @param n     *            정수     * @return 팩토리얼값     */    public static long factorial(long n) {        if (n <= 1) {            return 1;        }        return n * factorial(n - 1);    }

/**    * 조합을 구한다. nCr = nPr / r! = n! / ((n-r)! * r!) ex) 4개 중에 2개를 고르는 경우의 수 =    * 6가지     *     * @param n     *            전체 개수     * @param r     *            고를 개수     * @return 경우의 수     */    public static long combination(long n, long r) {        return factorial(n) / (factorial(n - r) * factorial(r));    } } code

Comment