差分序列
1 差分序列 2 3 0阶差分序列(就是序列本生): h0,h1,h2,···,hn,··· 4 1阶差分(△hn = hn+1 - hn): △h0,△h1,△h2,···,△hn,··· 5 ······ 6 7 定理一: 8 设序列的通项公式是n的p次多项式,即 9 hn = a(p)*n^p + a(p-1)*n^p-1 + ··· + a1*n + a0 10 则对所有△(p+1)hn = 0. 11 12 定理二: 13 差分表的第0条对角线等于 14 c0,c1,c2,···,cp,0,0,0,···, 其中cp != 0 15 的序列的通项公式满足 16 hn = c0(n,0) + c1(n,1)+c2(n,2)+···+cp(n,p) 17 18 定理三: 19 差分表的第0条对角线等于 20 c0,c1,c2,···,cp,0,0,0,···, 其中cp != 0 21 则 22 k(0-n)∑hk = c0(n+1,1) + c1(n+2,2) + ··· + cp(n+1,p+1) 23 24 25 26 27 1.给出了一个数列的前若干项,要求推测后面的项 28 29 int h[MAXN][MAXN]; // 存放差分序列,h[i][MAXN]为i阶差分 30 int n; // 前n个数 31 int m; // 要得到的后n个数 32 33 //计算差分序列 34 void CalDifferentSequence() 35 { 36 for(int i = 1; i < n; ++i) 37 for(int j = 0; i+j < n; ++j) 38 h[i][j] = h[i-1][j+1] - h[i-1][j]; 39 for(int i = 1; i <= m; ++i) 40 h[n-1][i] = h[n-1][0]; 41 for(int i = n-2; i >= 0; --i) 42 for(int j = 0; j < m; ++j) 43 h[i][n-i+j] = h[i][n-i+j-1] + h[i+1][n-i+j-1]; 44 }
题意:给定m和n,求∑(i^m) (1<= i <= n),1 <= n <= 10 ^ 41 and 3 <= m <= 100。
思路:n很大m很小可以直接利用定理三求解。
code:
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 public class Main { 5 6 public final int MAXN = 105; 7 8 Scanner cin = new Scanner(System.in); 9 public BigInteger c[] = new BigInteger[MAXN]; 10 BigInteger h[][] = new BigInteger[MAXN][MAXN]; 11 BigInteger N; 12 int M; 13 void init() { 14 c[1] = N; 15 for(int i = 2; i <= M + 1; ++i) { 16 c[i] = c[i-1].multiply(N.subtract(BigInteger.valueOf(i-1))).divide(BigInteger.valueOf(i)); 17 } 18 } 19 20 void solve() { 21 int T = cin.nextInt(); 22 while (T-- > 0) { 23 N = cin.nextBigInteger(); 24 N = N.add(BigInteger.ONE); 25 M = cin.nextInt(); 26 init(); 27 for (int i = 0; i <= M; ++i) { 28 h[0][i] = BigInteger.valueOf(i).pow(M); 29 } 30 for (int i = 1; i <= M; ++i) { 31 for (int j = 0; j <= M - i; ++j) { 32 h[i][j] = h[i - 1][j + 1].subtract(h[i - 1][j]); 33 } 34 } 35 BigInteger ans = BigInteger.ZERO; 36 for (int i = 0; i <= M; ++i) { 37 ans = ans.add(c[i+1].multiply(h[i][0])); 38 } 39 System.out.println(ans); 40 } 41 cin.close(); 42 } 43 44 public static void main(String[] args) { 45 new Main().solve(); 46 } 47 48 }