2014-08-28 解題區/解題區 - UVa UVa 10694 - Combinatorial Summation contents 1. Problem2. Sample Input3. Sample Output4. Solution Problem對於組合公式,輸出其結果。 Sample Input123234 Sample Output12714 Solution暴力法將前幾項找出來,發現其相鄰兩項之差恰好有規律。 120 1 3 7 14 26 46 79 133 0 2 4 7 12 20 33 54 其規律是前兩項總和再加一。 123456789101112131415161718192021222324252627282930import java.util.Scanner;import java.math.BigInteger;public class Main { public static void main(String[] args) { BigInteger[] f1 = new BigInteger[1024]; BigInteger[] f2 = new BigInteger[1024]; f1[1] = BigInteger.valueOf(2); f1[2] = BigInteger.valueOf(4); f2[0] = BigInteger.valueOf(0); f2[1] = BigInteger.valueOf(1); for (int i = 3; i < f1.length; i++) f1[i] = f1[i - 2].add(f1[i - 1]).add(BigInteger.ONE); for (int i = 2; i < f2.length; i++) f2[i] = f2[i - 1].add(f1[i - 1]); Scanner cin = new Scanner(System.in); int testcase = cin.nextInt(); while (testcase-- != 0) { int n = cin.nextInt(); if (n < 0) n = 0; System.out.println(f2[n]); } }}/* * 0 1 3 7 14 26 46 79 133 0 2 4 7 12 20 33 54 */ Newer UVa 11071 - Permutation Representation Older UVa 1635 - Irrelevant Elements