F. The Sum of the k-th Powers

题目连接:

http://www.codeforces.com/contest/622/problem/F

Description

There are well-known formulas: , , . Also mathematicians found similar formulas for higher degrees.

Find the value of the sum modulo 109 + 7 (so you should find the remainder after dividing the answer by the value 109 + 7).

Input

The only line contains two integers n, k (1 ≤ n ≤ 109, 0 ≤ k ≤ 106).

Output

Print the only integer a — the remainder after dividing the value of the sum by the value 109 + 7.

Sample Input

4 1

Sample Output

10

Hint

题意

让你计算1k+2k+....+n^k

题解:

拉格朗日插值法

答案等于$${P}{x} = \sum{i}^{k+2}({P}{i}\prod{j=1,j\neq i}^{k+2}\frac{n-j}{i-j})$$

最后的答案就等于P(n)

我们预处理(n-j)的阶乘,再预处理下面的阶乘就好了

对于这样,对于每一个i,我们都能够O(logn)来计算了(logn拿来求逆元)

代码

#include<bits/stdc++.h>
using namespace std; const int mod = 1e9+7;
const int maxn = 1e6+7;
long long quickpow(long long m,long long n,long long k)//返回m^n%k
{
long long b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m)%k;
n = n >> 1 ;
m = (m*m)%k;
}
return b;
}
long long p[maxn];
long long fac[maxn];
int n,k;
int main()
{
fac[0]=1;
for(int i=1;i<maxn;i++)
fac[i]=(fac[i-1]*i)%mod;
scanf("%d%d",&n,&k);
p[0]=0;
for(int i=1;i<=k+2;i++)
p[i]=(p[i-1]+quickpow(i,k,mod))%mod;
if(n<=k+2)
{
printf("%d\n",p[n]);
return 0;
}
long long cur = 1;
for(int i=1;i<=k+2;i++)
cur=(cur*(n-i))%mod;
long long ans = 0;
for(int i=1;i<=k+2;i++)
{
long long tmp = quickpow(fac[k+2-i]%mod*fac[i-1]%mod,mod-2,mod);
long long tmp2 = quickpow(n-i,mod-2,mod);
if((k+2-i)%2)tmp=-tmp;
ans =(ans + p[i]*cur%mod*tmp%mod*tmp2)%mod;
}
cout<<ans<<endl;
}

Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法的更多相关文章

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

    The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...

  2. [Educational Codeforces Round 7]F. The Sum of the k-th Powers

    FallDream dalao找的插值练习题 题目大意:给定n,k,求Σi^k (i=1~n),对1e9+7取模.(n<=10^9,k<=10^6) 思路:令f(n)=Σi^k (i=1~ ...

  3. 【Educational Codeforces Round 37 F】SUM and REPLACE

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 那个D函数它的下降速度是很快的. 也就是说到最后他会很快的变成2或者1 而D(2)==2,D(1)=1 也就是说,几次操作过后很多数 ...

  4. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  5. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  6. Educational Codeforces Round 26 F. Prefix Sums 二分,组合数

    题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...

  7. Educational Codeforces Round 14 - F (codeforces 691F)

    题目链接:http://codeforces.com/problemset/problem/691/F 题目大意:给定n个数,再给m个询问,每个询问给一个p,求n个数中有多少对数的乘积≥p 数据范围: ...

  8. Educational Codeforces Round 1 A. Tricky Sum 暴力

    A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...

  9. Educational Codeforces Round 23 F. MEX Queries 离散化+线段树

    F. MEX Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. C++学习之路(一):const与define,结构体对齐,new/delete

    前言:针对C++ Primer和Effective C++两本书,以及技术博客与实验测试,本系列主要是针对C++进行系统化学习,记录学习中遇到的问题和经验. (一)const与define 关于con ...

  2. V4L2(二)虚拟摄像头驱动vivi深入分析【转】

    转自:http://www.cnblogs.com/tureno/articles/6694463.html 转载于: http://blog.csdn.net/lizuobin2/article/d ...

  3. 手動設定 電池溫度 mtk platform

    adb root adb shell echo "3 1 27" > ./proc/mtk_battery_cmd/battery_cmd 27 即是所要設定的溫度, 此設定 ...

  4. perl_nc.pl

    #!/usr/bin/perl use strict; use IO::Socket; use IO::Select; use Getopt::Std; my %option;getopts('lp: ...

  5. C++内存管理(转)

    C++内存管理比较好的文章,参考链接如下: C++内存管理

  6. Mui自定义时间格式:

    Mui自定义时间格式: (function($) { $.init(); $(document).on('tap','.btn',function(){ var obj = getFormJson($ ...

  7. web中的CSS、Xpath等路径定位方法学习

    今天不到八点就到公司了,来的比较早,趁着有点时间,总结下web中的CSS.Xpath等路径定位定位的方式吧! 简单的介绍下xpath和css的定位 理论知识就不罗列了 还是利用博客园的首页.直接附上代 ...

  8. java中this的用法如:this.name=name

    package com.chensi; /** * 这个是为了搞懂那个 this.name = name的. * @author ZHL * */ public class ThisTestZhl { ...

  9. POJ 1984 Navigation Nightmare(二维带权并查集)

    题目链接:http://poj.org/problem?id=1984 题目大意:有n个点,在平面上位于坐标点上,给出m关系F1  F2  L  D ,表示点F1往D方向走L距离到点F2,然后给出一系 ...

  10. Linux安装mysql.8.0.12

    1. linux安装mysql8.0.12,亲测可用. 以下是安装过程中出现的问题: 1 [root@localtest1 file]# systemctl start mysqld 2 Job fo ...