Codeforces 622F. The Sum of the k-th Powers Lagrange interpolation method

Source: Internet
Author: User



Topic links



Ask Sigma (I:1 to N) i^k.






In order to do this question these two days really fill up a lot of number theory, before even multiplication inverse yuan do not know ...



About Lagrange interpolation, I'm looking at the http://www.guokr.com/post/456777/here, and it's kinda fun ...



According to the example given in the topic, we can find that the highest order of the K-th formula is k+1 times, and according to the Lagrange interpolation method, it is necessary to construct a k+1 equation k+2 term.



Then the formula is, for this problem, P[i] is i^k+ (i-1) ^k+ (i-2) ^k+.....+1^k, this part can be preprocessed out. I will not make the formula, stolen from the http://www.cnblogs.com/qscqesze/p/5207132.html here (fog



We find that above is (n-1) * (n-2) ... * (n-k-2)/(N-i), the upper part of the preprocessing, except (n-i) equivalent multiplication (n-i) inverse.



The following part is (i-1) * (i-2) * ... (i-i+1) * (i-i-1) * (i-i-2) * ... (I-k-2), which is equivalent to two factorial multiplication, factorial preprocessing, and then pay attention to the following sign on it.


#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const ll mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e6+5;
ll f[maxn], fac[maxn];
ll pow(ll a, ll b) {
    ll ret = 1;
    while(b) {
        if(b&1)
            ret = (ret*a)%mod;
        a = (a*a)%mod;
        b>>=1;
    }
    return ret;
}
int main()
{
    ll n, k;
    cin>>n>>k;
    for(int i = 1; i<=k+2; i++) {
        f[i] = (f[i-1]+pow(i*1LL, k))%mod;
    }
    fac[0] = 1;
    for(int i = 1; i<maxn; i++) {
        fac[i] = (fac[i-1]*i)%mod;
    }
    if(n<=k+2) {
        cout<<f[n]<<endl;
        return 0;
    }
    ll cur = 1, ans = 0;
    for(int i = 1; i<=k+2; i++) {
        cur = (cur*(n-i))%mod;
    }
    for(int i = 1; i<=k+2; i++) {
        ll tmp = pow(n-i, mod-2)%mod;
        ll tmp1 = pow(fac[i-1]%mod*fac[k+2-i]%mod, mod-2)%mod;
        int sign = (k+2-i)%2?-1:1;
        ans = (ans + sign*tmp*tmp1%mod*f[i]%mod*cur%mod)%mod;
    }
    ans = (ans+mod)%mod;
    cout<<ans<<endl;
    return 0;
}





Codeforces 622F. The Sum of the k-th Powers Lagrange interpolation method


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.