题目链接

题意 : 就是让你求个自然数幂和、最高次可达 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. [xpath] text()和string()区别

    质区别 text()是一个node test,而string()是一个函数,data()是一个函数且可以保留数据类型.此外,还有点号(.)表示当前节点. 使用要点 XML例子: <book> ...

  2. [转帖]linux下使用 du查看某个文件或目录占用磁盘空间的大小

    linux下使用 du查看某个文件或目录占用磁盘空间的大小 du -ah --max-depth= 去年用过一次 后来忘记了.. 命令这个东西 熟能生巧.. https://www.cnblogs.c ...

  3. 2. zookeeper介绍及集群搭建

    ZooKeeper 概述 Zookeeper 是一个分布式协调服务的开源框架. 主要用来解决分布式集群中 应用系统的一致性问题,例如怎样避免同时操作同一数据造成脏读的问题. ZooKeeper 本质上 ...

  4. Storm本地启动拓扑报错:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/storm/topology/IRichSpout

    问题描述: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/storm/topology ...

  5. mweb发布文章为什么默认TinyMCE编辑器?

    如果是通过 metaweblog api 发布的,需要在网站分类中添加 [Markdown] 标记

  6. Mac OS 下定制终端颜色

    方法 有五种方法, 参考网站,我使用的是 Oh My Zsh 方案一:(通过 .bash_profile 文件自定制) 方案二:(也是修改 ~/.bash_profile) 方案三:(三方插件 Oh ...

  7. @RequestMapping-标准映射和Ant风格的映射

    4.@RequestMapping 如果value不以“/”开头,SpringMVC会自动添加“/” 4.1.@RequestMapping映射 4.1.1.标准URL映射 4.1.2.Ant风格的U ...

  8. 10 Scrapy框架持久化存储

    一.基于终端指令的持久化存储 保证parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. 执行输出指定格式进行存储:将爬取到的 ...

  9. git ignore 如何忽略已经提交的文件修改

    git ignore git ignore的作用很简单,本地仓库忽略一些文件的修改. ignore的规格可以按文件匹配,按后缀匹配或者按文件夹匹配. 如果在项目开发过程中,需要忽略某一个文件已经提交的 ...

  10. 人脸识别之Python DLib库进行人脸关键点识别

    一.首先安装DLib模块 这里只介绍linux安装的过程,windows安装过程请自行百度 1.首先,安装dlib.skimage前:先安装libboost sudo apt-get install ...