Question Link
The number of N is changed to n-1 at each operation, and finally to a number. The operation refers to the number written by the last number minus the number obtained by the previous number.
Train of Thought: Find a few numbers. Do not calculate them first, instead of using formulas, for example:
1 2 3 4 5 6
(2-1) (3-2) (4-3) (5-4) (6-5)
(3-2-2 + 1) (4-3-3 + 2) (5-4-4 + 3) (6-5-5 + 4)
(4-3-3 + 2-3 + 2 + 2-1) (5-4-4 + 3-4 + 3 + 3-2) (6-5-5 + 4-5 + 4 + 4-3)
(5-4-4 + 3-4 + 3 + 3-2-4 + 3 + 3-2 + 3-2-2 + 1) (6-5-5 + 4-5 + 4 + 4-3-5 + 4 + 4-3 + 4-3-3 + 2)
(6-5-5 + 4-5 + 4 + 4-3-5 + 4 + 4-3 + 4-3 + 2-5 + 4 + 4-3) + 4-3-3 + 2 + 4-3-3 + 2-3 + 2 + 2-1)
Offset the positive and negative values in the number to get the final formula:
1*6-5*5 + 10*4-10*3 + 5*2-1*1
In fact, the coefficient of each number is the Yang Hui triangle. if you write a few more, you can see it. Or you can infer that the above formula is like the Yang Hui triangle.
Then, obtain the number of combinations. Do not create tables or call functions in advance, but directly remove the values in the loop. Otherwise, the timeout will exceed the limit ....
1 import java.io.*; 2 import java.math.*; 3 import java.text.*; 4 import java.util.*; 5 6 public class Main { 7 8 public static void main(String[] args) { 9 Scanner cin = new Scanner(System.in) ;10 BigInteger[] ch = new BigInteger[3100] ;11 BigInteger ans,a,b;12 int T ,n;13 T = cin.nextInt() ;14 while(T-- > 0)15 {16 n = cin.nextInt();17 ans = BigInteger.ZERO ;18 b = BigInteger.ONE ;19 a = BigInteger.valueOf(n-1) ;20 for(int i = 1 ; i <= n ; i++)21 ch[i] = cin.nextBigInteger();22 for(int i = 0 ; i < n ; i++)23 {24 if(i % 2 == 0)25 {26 ans = ans.add(b.multiply(ch[n-i])) ;27 }28 else 29 {30 ans = ans.subtract(b.multiply(ch[n-i])) ;31 }32 b = b.multiply(a).divide(BigInteger.valueOf(i+1)) ;33 a = a.subtract(BigInteger.ONE) ;34 }35 System.out.println(ans);36 }37 }38 39 }
View code