[CF622F]The Sum of the k-th Powers
题目大意:给你$n,k(n\leqslant10^9,k\leqslant10^6)$,求:
$$
\sum\limits_{i=1}^ni^k\pmod{10^9+7}
$$
题解:可以猜测是一个$k+1$次的多项式,可以求出$x$在$0,1,2,3,\dots,k+1$时的值,设为$s_0,s_1,\dots,s_{k+1}$,根据拉格朗日插值公式:
$$
\begin{align*}
f_n&=\sum\limits_{i=0}^{k+1}y_i\prod\limits_{j=0,j\not=i}^{k+1}\dfrac{n-x_j}{x_i-x_j}\\
&=\sum\limits_{i=0}^{k+1}(-1)^{k-i+1}s_i\dfrac{n(n-1)\cdots(n-k-1)}{(n-i)i!(k-i+1)!}\\
\end{align*}
$$
然后预处理出阶乘就可以了。注意,因为取了$0$这个点,若$k=0$会答案出错,可以选择特判或取$1\sim k+2$几个点,还有,当$k\leqslant n-1$时,式子为零,直接输出即可。
卡点:无
C++ Code:
#include <cstdio>
#define maxn 1000010
const int mod = 1e9 + 7;
inline int pw(int base, int p) {
static int res;
for (res = 1; p; p >>= 1, base = static_cast<long long> (base) * base % mod) if (p & 1) res = static_cast<long long> (res) * base % mod;
return res;
}
inline int inv(int x) {return pw(x, mod - 2);}
inline void reduce(int &x) {x += x >> 31 & mod;} int n, k, ans;
int fac[maxn], s[maxn], prod = 1;
int main() {
scanf("%d%d", &n, &k);
if (k == 0) {
std::printf("%d\n", n);
return 0;
}
for (int i = 0; i <= k + 1; i++) {
prod = static_cast<long long> (n - i) * prod % mod;
s[i] = pw(i, k);
}
fac[0] = 1;
for (int i = 1; i <= k + 1; i++) {
fac[i] = static_cast<long long> (fac[i - 1]) * i % mod;
reduce(s[i] += s[i - 1] - mod);
if (n == i) {
std::printf("%d\n", s[i]);
return 0;
}
}
for (int i = 1; i <= k + 1; i++) {
reduce(ans += s[i] * static_cast<long long> (prod) % mod * inv(n - i) % mod * inv(fac[i]) % mod * inv(fac[k - i + 1]) * (k - i + 1 & 1 ? -1 : 1) % mod - mod);
reduce(ans);
}
printf("%d\n", ans);
return 0;
}
[CF622F]The Sum of the k-th Powers的更多相关文章
- [题解] CF622F The Sum of the k-th Powers
CF622F The Sum of the k-th Powers 题意:给\(n\)和\(k\),让你求\(\sum\limits_{i = 1} ^ n i^k \ mod \ 10^9 + 7\ ...
- 解题:CF622F The Sum of the k-th Powers
题面 TJOI2018出CF原题弱化版是不是有点太过分了?对,就是 TJOI2018 教科书般的亵渎 然而我这个问题只会那个题的范围的m^3做法 回忆一下1到n求和是二次的,平方求和公式是三次的,立方 ...
- [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 ...
- LeetCode862. 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 ...
- leetcode 862 shorest subarray with sum at least K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...
- 862. 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 ...
- [LeetCode] 862. Shortest Subarray with Sum at Least K 和至少为K的最短子数组
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
随机推荐
- JS学习 函数的理解
ECMAScript 的函数实际上是功能完整的对象. 函数的理解 用 Function 类直接创建函数,格式如下.可理解为Function构造器. var function_name = new Fu ...
- iOS UIWebView加载时添加进度条01
标注:此框架仅适合UIWebView 对iOS8后新出的WKWebView不适用,当然,你可以尝试修改框架里的几个代理方法. 框架是:NJKWebViewProgress 导入头文件 #import ...
- 学会了vim中的自动补全功能
好开心,再也不用再多个工具之间切换了,哈哈 擦,功能太弱
- 2019年猪年海报PSD模板-第三部分
14套精美猪年海报,免费猪年海报,下载地址:百度网盘,https://pan.baidu.com/s/15m6sWTdDzuBfdmHYxJVvbA
- Qt-QML-C++交互实现文件IO系统-后继-具体文件IO的实现
在上一篇文章中,我大致将这个QML中的文件IO类搭出了大致的框架,那么,今天抽时间写了一点文件的读写,这里我使用的文件流来读写文件. 文件结构如图 在QML中调用这个类了,就见简单的读取了一个JSON ...
- Qt 计算两个日前间隔天数
某一个大神写的 改写了一点 请无视注释 //时间计算法则 /********************************************************************** ...
- word record 3
word record 3 tabloid : a half size page of a newspaper, or a newspaper or magazine with short, exci ...
- [JSON].getObj( keyPath )
语法:[JSON].getObj( keyPath ) 返回:[JSON] 说明:返回指定键名路径的JSON对象,指定键名路径不存在时返回空的toJson对象(强烈建议使用 [JSON].exists ...
- spark-submit配置说明
<Spark 官方文档>Spark配置 spark-1.6.0 原文地址 Spark配置 Spark有以下三种方式修改配置: Spark properties (Spark属性)可以控制绝 ...
- 如何使用AEditor制作一个简单的H5交互页demo
转载自:http://www.alloyteam.com/2015/06/h5-jiao-hu-ye-bian-ji-qi-aeditor-jie-shao/ 本教程演示如何使用AEditor制作一个 ...