51_1228 Sequence Summation (Bernoulli number)

Source: Internet
Author: User

Transfer from: http://blog.csdn.net/acdreamers/article/details/38929067 (acdreamers)

Analysis: The subject test instructions is to seek the power of the natural number of the sum , but its case is more. The time complexity required for exponentiation and itself, if the following

Continue to use the above method to find the power of the natural number of the sum, the four case will TLE, next introduce another method to find the power of the natural number, it is based on the

The formula is described as follows

It can be seen that as long as we preprocess each item, we can calculate the power sum of natural numbers in linear time. The inverse of the front can be calculated by recursive method

preprocessing, the combination number can also be pre-processing, can be pre-processing, now the key is how to preprocess Bernoulli number .

Bernoulli number satisfies the condition and has

Then continue to get

This is the recurrence of the Bernoulli number, and the inverse element can also be preprocessed.

In addition, in this problem I also learned an O (n) recursive method to find the first n inverse of the method, see the next chapter of the blog.

1228 Sequence summation

Source: Hackerrank Base time limit: 3 seconds Space limit: 131072 KB score: 160 Difficulty: 6-level algorithm problem collection attention T (n) = N^k,s (n) = t (1) + t (2) + ... T (n). Given N and K, ask for S (n). For example k = 2,n = 5,s (n) = 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55. As a result, output s (n) Mod 1000000007 results. Input
Line 1th: A number t that represents the number of numbers that are later used as input tests. (1 <= T <= 5000) 2-t + 1 lines: 2 numbers per line, N, K divided by a space. (1 <= N <= 10^18, 1 <= K <= 2000)
Output
A total of T-lines corresponding to the results of the S (n) Mod 1000000007.
Input example
35 34 24 1
Output example
2253010

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cmath>5#include <iomanip>6     using namespacestd;7typedefLong LongLL;8     ConstLL N =2050;9     ConstLL MAXN =2050;Ten     ConstLL mod =1000000007; One LL N, K; A      - LL COMB[MAXN][MAXN], BER[MAXN]; -      the     voidEX_GCD (ll A, ll B, ll& D, ll& X, LL &y) { -         if(b==0){ -x =1; y =0; D =A; -             return ; +         } -EX_GCD (b, a%B, D, y, x); +Y-= x* (A/b); A     } at      - LL INV (ll A, ll p) { - LL x, y, D; - EX_GCD (A, p, D, X, y); -         if(d==1)return(x%p + p)%p; -         Else return 0; in     } -      to     voidInit_comb () { +comb[0][0] =1; -          for(inti =1; i<maxn; ++i) { thecomb[i][0] = Comb[i][i] =1; *              for(intj =1; j<i; ++j) { $COMB[I][J] = (comb[i-1][j-1]%mod + comb[i-1][J]%MOD)%MoD;Panax Notoginseng             } -         } the     } +Ask for Bernoulli number A     voidInit_ber () { theber[0] =1; +          for(inti =1; i<maxn; ++i) { -LL ans =0; $              for(intj =0; J<i; ++j) $Ans = (ans + comb[i+1][J]*BER[J])%MoD; -Ans =-ANS*INV (i+1, MoD)%MoD; -Ber[i] = (ans%mod + MoD)%MoD; the         } -     }Wuyi  the     //LL INV[MAXN]; -     /*void Bi () { Wu inv[1] = 1;  - A for (int i=2; i<n; i++) About Inv[i] = (mod-mod/i) * inv[mod% i]% mod;  $ //Pre-processing Bernoulli number - for (int i= 1; i<100; ++i) cout<<inv[i]<< ""; - cout<<endl; - for (int j= 1; j<100; ++j) COUT<<INV (j,mod) << ""; A     }*/ +     intMain () { the         intT; -Cin>>T; $ Init_comb (); the Init_ber (); the          while(t--){ thescanf"%lld%lld", &n, &k); theN%=MoD; -LL ans =0; inLL POW = (n+1)%MoD; the              for(inti =1; i<=k+1; ++i) { theAns = (ans + comb[k+1][i]*ber[k+1-I]%MOD*POW%MOD)%MoD;  AboutPOW = (pow* (n+1))%MoD; the             } theAns = ANS*INV (k +1, MoD)%MoD; theAns = (ans%mod + MoD)%MoD; +printf"%lld\n", ans);  -         } the}

  

51_1228 Sequence Summation (Bernoulli number) (RPM)

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.