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
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 拉格朗日插值法的更多相关文章
- 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 ...
- [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~ ...
- 【Educational Codeforces Round 37 F】SUM and REPLACE
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 那个D函数它的下降速度是很快的. 也就是说到最后他会很快的变成2或者1 而D(2)==2,D(1)=1 也就是说,几次操作过后很多数 ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...
- Educational Codeforces Round 14 - F (codeforces 691F)
题目链接:http://codeforces.com/problemset/problem/691/F 题目大意:给定n个数,再给m个询问,每个询问给一个p,求n个数中有多少对数的乘积≥p 数据范围: ...
- Educational Codeforces Round 1 A. Tricky Sum 暴力
A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...
- 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 ...
随机推荐
- udp调用connect有什么作用(转)
原文链接如下: http://blog.csdn.net/wannew/article/details/18218619 整理一下.1:UDP中可以使用connect系统调用 2:UDP中connec ...
- Leetcode 之Binary Tree Postorder Traversal(44)
后序遍历,比先序和中序都要复杂.访问一个结点前,需要先判断其右孩子是否被访问过.如果是,则可以访问该结点:否则,需要先处理右子树. vector<int> postorderTravers ...
- leetcode 之Reverse Linked List II(15)
这题用需要非常细心,用头插法移动需要考虑先移动哪个,只需三个指针即可. ListNode *reverseList(ListNode *head, int m, int n) { ListNode d ...
- linux命令(38):traceroute命令
1.命令格式: traceroute[参数][主机] 2.命令功能: traceroute指令让你追踪网络数据包的路由途径,预设数据包大小是40Bytes,用户可另行设置. 具体参数格式:tracer ...
- Python Flask装饰器登录验证
from flask import Flask,render_template,redirect,request,session app = Flask(__name__) app.secret_ke ...
- hadoop3.1 ha高可用部署
1.资源角色规划
- Python图像处理库(1)
转自:http://www.ituring.com.cn/tupubarticle/2024 第 1 章 基本的图像操作和处理 本章讲解操作和处理图像的基础知识,将通过大量示例介绍处理图像所需的 Py ...
- redis 安装及安装遇到的问题解决
https://blog.csdn.net/jy0902/article/details/19248299 http://q.fireflyclub.org/?/article/24 https:// ...
- 这种文件别打开, 大小不足1KB, 却可以让你电脑瘫痪
今年6月份,抖音表白代码火了,不足1kb的txt文件,玩出了新花样.可是你知道吗,这种非常“浪漫”的表白方式,其实存在着很大的风险,甚至会让你的电脑直接瘫痪. 首先,先说一下所谓的表白代码是怎么回事. ...
- 模仿jq里的选择器和color样式
(function(){ HTMLElement.prototype.css = function () { var option; if (arguments.length > 0) { op ...