题目链接

题意 : 就是让你求个自然数幂和、最高次可达 1e6 、求和上限是 1e9

分析 : 

题目给出了最高次 k = 1、2、3 时候的自然数幂和求和公式

可以发现求和公式的最高次都是 k+1

那么大胆猜测幂为 k 的自然数幂和肯定可以由一个最高次为 k+1 的多项式表示

不会证明,事实也的确如此

此时就变成了一个拉格朗日插值的模板题了

只要先算出 k+2 个值、然后就能确定最高次为 k+1 的多项式

套模板求值即可

当然自然数幂和不止这一种做法、例如伯努利数、斯特林数等

详细可参考 ==> Click here

#include<bits/stdc++.h>
#define LL long long
#define ULL unsigned long long

#define scs(i) scanf("%s", i)
#define sci(i) scanf("%d", &i)
#define scd(i) scanf("%lf", &i)
#define scIl(i) scanf("%I64d", &i)
#define scii(i, j) scanf("%d %d", &i, &j)
#define scdd(i, j) scanf("%lf %lf", &i, &j)
#define scIll(i, j) scanf("%I64d %I64d", &i, &j)
#define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k)
#define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k)
#define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k)
#define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l)
#define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l)
#define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l)

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define lowbit(i) (i & (-i))
#define mem(i, j) memset(i, j, sizeof(i))

#define fir first
#define sec second
#define VI vector<int>
#define ins(i) insert(i)
#define pb(i) push_back(i)
#define pii pair<int, int>
#define VL vector<long long>
#define mk(i, j) make_pair(i, j)
#define all(i) i.begin(), i.end()
#define pll pair<long long, long long>

#define _TIME 0
#define _INPUT 0
#define _OUTPUT 0
clock_t START, END;
void __stTIME();
void __enTIME();
void __IOPUT();
using namespace std;
;
;

LL pow_mod(LL a, LL b)
{
    a %= mod;
    LL ret = ;
    while(b){
        ) ret = (ret * a) % mod;
        a = ( a * a ) % mod;
        b >>= ;
    }
    return ret;
}

namespace polysum
{
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=n-1;i>=a;i--)
    ;
    LL a[D],f[D],g[D],p[D],p1[D],p2[D],b[D],h[D][],C[D];
    LL powmod(LL a,LL b){LL res=;a%=mod;assert(b>=);){)res=res*a%mod;a=a*a%mod;}return res;}
    LL calcn(int d,LL *a,LL n) { // a[0].. a[d]  a[n]
        if (n<=d) return a[n];
        p1[]=p2[]=;
        rep(i,,d+) {
            LL t=(n-i+mod)%mod;
            p1[i+]=p1[i]*t%mod;
        }
        rep(i,,d+) {
            LL t=(n-d+i+mod)%mod;
            p2[i+]=p2[i]*t%mod;
        }
        LL ans=;
        rep(i,,d+) {
            LL t=g[i]*g[d-i]%mod*p1[i]%mod*p2[d-i]%mod*a[i]%mod;
            ) ans=(ans-t+mod)%mod;
            else ans=(ans+t)%mod;
        }
        return ans;
    }
    void init(int M) {
        f[]=f[]=g[]=g[]=;
        rep(i,,M+) f[i]=f[i-]*i%mod;
        g[M+]=powmod(f[M+],mod-);
        per(i,,M+) g[i]=g[i+]*(i+)%mod;
    }
    LL polysum(LL m,LL *a,LL n) { // a[0].. a[m] \sum_{i=0}^{n-1} a[i]

        ;i<=m;i++) b[i]=a[i];
        b[m+]=calcn(m,b,m+);
        rep(i,,m+) b[i]=(b[i-]+b[i])%mod;
        ,b,n-);
    }
    LL qpolysum(LL R,LL n,LL *a,LL m) { // a[0].. a[m] \sum_{i=0}^{n-1} a[i]*R^i
        ) return polysum(n,a,m);
        a[m+]=calcn(m,a,m+);
        LL r=powmod(R,mod-),p3=,p4=,c,ans;
        h[][]=;h[][]=;
        rep(i,,m+) {
            h[i][]=(h[i-][]+a[i-])*r%mod;
            h[i][]=h[i-][]*r%mod;
        }
        rep(i,,m+) {
            LL t=g[i]*g[m+-i]%mod;
            ) p3=((p3-h[i][]*t)%mod+mod)%mod,p4=((p4-h[i][]*t)%mod+mod)%mod;
            ]*t)%mod,p4=(p4+h[i][]*t)%mod;
        }
        c=powmod(p4,mod-)*(mod-p3)%mod;
        rep(i,,m+) h[i][]=(h[i][]+h[i][]*c)%mod;
        rep(i,,m+) C[i]=h[i][];
        ans=(calcn(m,C,n)*powmod(R,n)-c)%mod;
        ) ans+=mod;
        return ans;
    }

}
LL arr[maxn];
int main(void){__stTIME();__IOPUT();

    int n, k;

    scii(n, k);

    ) return !printf("%d\n", n);

    polysum::init(k+);

    ; i<=k+; i++)
        arr[i] = pow_mod((LL)i, (LL)k);

    LL res = polysum::polysum(k+, arr, n+) - arr[];
    printf("%I64d\n", res);

__enTIME();;}

void __stTIME()
{
    #if _TIME
        START = clock();
    #endif
}

void __enTIME()
{
    #if _TIME
        END = clock();
        cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl;
    #endif
}

void __IOPUT()
{
    #if _INPUT
        freopen("in.txt", "r", stdin);
    #endif
    #if _OUTPUT
        freopen("out.txt", "w", stdout);
    #endif
}

Codeforces 622F The Sum of the k-th Powers ( 自然数幂和、拉格朗日插值法 )的更多相关文章

  1. codeforces 622F. The Sum of the k-th Powers 拉格朗日插值法

    题目链接 求sigma(i : 1 to n)i^k. 为了做这个题这两天真是补了不少数论, 之前连乘法逆元都不知道... 关于拉格朗日插值法, 我是看的这里http://www.guokr.com/ ...

  2. Codeforces 622F The Sum of the k-th Powers

    Discription There are well-known formulas: , , . Also mathematicians found similar formulas for high ...

  3. Codeforces 622F The Sum of the k-th Powers(数论)

    题目链接 The Sum of the k-th Powers 其实我也不懂为什么这么做的……看了无数题解觉得好厉害哇…… #include <bits/stdc++.h> using n ...

  4. The Sum of the k-th Powers(Educational Codeforces Round 7F+拉格朗日插值法)

    题目链接 传送门 题面 题意 给你\(n,k\),要你求\(\sum\limits_{i=1}^{n}i^k\)的值. 思路 根据数学知识或者说题目提示可知\(\sum\limits_{i=1}^{n ...

  5. Codeforces 396B On Sum of Fractions 数论

    题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...

  6. codeforces 963A Alternating Sum

    codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...

  7. Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法

    F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...

  8. Codeforces D. The Sum of the k-th Powers(拉格朗日插值)

    题目描述: The Sum of the k-th Powers time limit per test 2 seconds memory limit per test 256 megabytes i ...

  9. [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

随机推荐

  1. [Luogu5686] 和积和

    Description 给定两个下标从\(1\)到\(n\)编号的序列 \(a_i,b_i\),定义函数\(S(l,r)(1\le l\le r\le n)\)为: \[\sum_{i=l}^r a_ ...

  2. AC自动机模版

    我以前一直觉得AC自动机就是我打一个代码,然后可以帮我自动AC题目,现在才知道原来是处理字符串的一个有意思的东西,然后我们现在就来看一下这个东西 1464: [视频][AC自动机]统计单词出现个数 时 ...

  3. 从入门到自闭之Python字典如何使用

    字典: 定义:dict dict = {"key":"value"} -- 键值对 作用:存储大量数据,数据和数据起到关联作用 所有的操作都是通过键来完成 键: ...

  4. Windows 2008 R2阿里云安全基线检查

    设置密码使用期限策略在管理工具打开本地安全策略,打开路径:安全设置\帐户策略\密码策略,将密码最长使用期限设置为30-180之间,建议值为90,将密码最短使用期限设置为1-14之间,建议值为7. 风险 ...

  5. O012、Linux如何实现VLAN

    参考https://www.cnblogs.com/CloudMan6/p/5313994.html   LAN 表示 Local Area Network ,本地局域网,通常使用 Hub 或者 Sw ...

  6. 关于redis的几件小事(一)redis的使用目的与问题

    1.redis是用来干嘛的? Redis is an open source (BSD licensed), in-memory data structure store, used as a dat ...

  7. jquery简单实现表格隔行变色

    小知识点:odd的过滤选择器大的使用 html代码: <table> <tr> <td>用户名</td> <td>年龄</td> ...

  8. jq选择CheckBox进行排序

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name=& ...

  9. lambda中FirstOrDefault和First

    First()表示取集合中的第一个元素,如果集合为空,则抛异常. FirstOrDefault()表示取集合的第一个元素. 如果集合为空,且集合元素是引用类型,则返回null. 如果集合为空,且集合元 ...

  10. mysql数据库备份与恢复命令

    mysqldump -h主机名  -P端口 -u用户名 -p密码 [--databases] 数据库名(可以是多个,用空格分割) > 文件名.sql 备份MySQL数据库的命令(备份脚本中不包含 ...