Bernuoli number and natural power sum

Source: Internet
Author: User

Today we are discussing how to effectively calculate the Power Sum of natural numbers. Next3For example.

 

Question:Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 1864

 

Analysis:In fact, there are many ways to calculate the power and methods of natural numbers. Let's first look at the ordinary recursive method.

 

 

Then, all the values are accumulated.

 

 

For more information, see

 

 

We can see that this is a recursive formula.

 

 

The following recursive formula is obtained:

 

 

Recursive exit is

 

 

In order to improve efficiency, we need to memorize it during recursion. Because large numbers are usedJava.

 

Code:

import java.math.*;import java.util.*;public class Main {public static final int N = 105;public static final BigInteger FLAG = (BigInteger.ZERO).subtract(BigInteger.ONE);public static BigInteger[][] C = new BigInteger[N][N];public static BigInteger[] ans = new BigInteger[N];public static void Init(){for(int i=0; i<N; i++){C[i][0] = C[i][i] = BigInteger.ONE;if(i == 0) continue;for(int j=1; j<i; j++)C[i][j] = C[i-1][j].add(C[i-1][j-1]);}}public static BigInteger Solve(BigInteger n, int k){if(ans[k].compareTo(FLAG) != 0){return ans[k];}if(k == 1){ans[k] = ((n.add(BigInteger.ONE)).multiply(n)).divide(BigInteger.valueOf(2));return ans[k];}BigInteger tmp = BigInteger.ONE;for(int i=0; i<k+1; i++){tmp = tmp.multiply(n.add(BigInteger.ONE));}tmp = tmp.subtract(n.add(BigInteger.ONE));BigInteger sum = BigInteger.ZERO;for(int i=1; i<k; i++){BigInteger t = C[k+1][i+1].multiply(Solve(n, k-i));sum = sum.add(t);}ans[k] = (tmp.subtract(sum)).divide(BigInteger.valueOf(k+1));return ans[k];}public static void main(String[] args){Init();Scanner cin = new Scanner(System.in);while(cin.hasNext()){BigInteger n = cin.nextBigInteger();int k = cin.nextInt();for(int i=0; i<N; i++){ans[i] = FLAG;}System.out.println(Solve(n, k));}}}


 

Title: http://www.51nod.com/onlineJudge/questionCode.html! Problemid = 1228

 

Analysis:This question is to askPower Sum of Natural NumbersBut itsCaseThere are many. The time complexity required for power and itself.

Continue to use the above method to calculate the Power Sum of natural numbers,5000ItemsCaseYesTleNext, we will introduce another method to calculate the Power Sum of natural numbers, which is based on

The formula of the nuoli number is described as follows:

 

 

We can see that as long as we pre-process each item, we can obtain the power sum of natural numbers in linear time. You can use the forward method to calculate the inverse element.

Preprocessing, the number of combinations can also be preprocessed, or pre-processed first, now the key is how to pre-processBernuoli count.

 

Bernuoli countMeet the conditions and have

 

 

Then continue to get

 

 

This is the recursive formula of the bernuoli number. The reverse element can also be preprocessed.

 

Code:

# Include <iostream> # include <string. h> # include <stdio. h> using namespace STD; typedef long ll; const ll mod = 1000000007; const int n = 2005; ll C [N] [N]; LL B [N], inv [N]; ll TMP [N]; ll N; void Init () {// Number of preprocessing combinations for (INT I = 0; I <n; I ++) {c [I] [0] = C [I] [I] = 1; if (I = 0) continue; For (Int J = 1; j <I; j ++) C [I] [J] = (C [I-1] [J] % mod + C [I-1] [J-1] % mod) % MOD ;} // pre-processing inverse element inv [1] = 1; for (INT I = 2; I <n; I ++) inv [I] = (mod-mod/I) * inv [mod % I] % MOD; // The number of pre-processed bernuoli B [0] = 1; for (INT I = 1; I <n; I ++) {ll ans = 0; if (I = n-1) break; For (Int J = 0; j <I; j ++) {ans + = C [I + 1] [J] * B [J]; ans % = MOD;} ans * =-inv [I + 1]; ans = (ANS % mod + mod) % MOD; B [I] = ans ;}} ll work (int K) {ll ans = inv [k + 1]; ll sum = 0; For (INT I = 1; I <= k + 1; I ++) {sum + = C [k + 1] [I] * TMP [I] % mod * B [K + 1-I] % MOD; sum % = MOD ;} ans * = sum; ans % = MOD; return ans;} int main () {int t; Init (); scanf ("% d", & T ); while (t --) {int K; scanf ("% i64d % d", & N, & K); N % = MOD; TMP [0] = 1; for (INT I = 1; I <n; I ++) TMP [I] = TMP [I-1] * (n + 1) % MOD; printf ("% i64d \ n", work (k);} return 0 ;}



Title: http://www.51nod.com/onlineJudge/questionCode.html! Problemid = 1258

 

Analysis:The difference between this question and the previous question is that the value is relatively large50000If the same method is usedTle. Therefore, optimization is required.

 

Continue to simplify the above expression to obtain

 

 

We can see that the back part is in the convolution form, so we can useFFT.

 

 

Bernuoli number and natural power sum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.