题目大意:给你$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的更多相关文章

  1. [题解] 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\ ...

  2. 解题:CF622F The Sum of the k-th Powers

    题面 TJOI2018出CF原题弱化版是不是有点太过分了?对,就是 TJOI2018 教科书般的亵渎 然而我这个问题只会那个题的范围的m^3做法 回忆一下1到n求和是二次的,平方求和公式是三次的,立方 ...

  3. [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 ...

  4. 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 ...

  5. leetcode 862 shorest subarray with sum at least K

    https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...

  6. 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 ...

  7. [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 ...

  8. 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...

  9. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

随机推荐

  1. netty之心跳机制

    1.心跳机制,在netty3和netty5上面都有.但是写法有些不一样. 2.心跳机制在服务端和客户端的作用也是不一样的.对于服务端来说:就是定时清除那些因为某种原因在一定时间段内没有做指定操作的客户 ...

  2. dsp5509的中断系统

    1. DSP5509有32个中断,中断分为软件中断和硬件中断,同时软件中断不可以屏蔽.软件中断由指令触发.55x在中断时DSP会自动保存ST0_55.ST1_55.ST2_55三个寄存器. 2. 其中 ...

  3. 安卓app连接CC2541-手机休眠后唤醒,通信不再成功

    1. 现在遇到的问题,手机进入休眠状态后唤醒,APP软件和CC2541的通信不正常了,但是CC2541依然检测到时连接状态.如何解决这个问题?手机唤醒之后会重新创建活动? 2.Wakelock 锁机制 ...

  4. LWM2M简介-学习记录

    1. Lightweight M2M 基础,谁搞出来的 OMA是一家国际组织,因为物联网的兴起, OMA在传统的OMA-DM协议基础之上,提出了LWM2M协议.这个协议基于COAP协议,COAP协议基 ...

  5. == vs === in Javascript

    本文来自网易云社区 作者:魏文庆 如果你只想知道==与===的区别,请直接看总结,当然我更希望您能耐心看完全文.Javascript中用于相等比较的操作符有两个==和===.==我们通常称为" ...

  6. iOS 开发库相关(持续更新)

    01-给任意view添加毛玻璃效果 https://github.com/JagCesar/iOS-blur   02-浮动式的textfield输入框(可用于登录界面) https://github ...

  7. cost加上了

  8. unity3d 角色头顶信息3D&2D遮挡解决方案(一)

    先上效果图,只凭文字描述,脑补应该有些困难- - 如图:有三个角色(我们暂且从左到右叫它们A.B.C),一个2D UI(中间动作选择的框框),一个cube(右边的方块) cube挡住了角色C的头顶信息 ...

  9. 教你一招,提升你Python代码的可读性,小技巧

    Python的初学者,开发者都应该知道的代码可读性提高技巧,本篇主要介绍了如下内容: PEP 8是什么以及它存在的原因 为什么你应该编写符合PEP 8标准的代码 如何编写符合PEP 8的代码 为什么我 ...

  10. (查找函数+atoi)判断与(注册函数+strcmp函数)判断两种方法

    loadrunner中接口判断的2中方法    如下: 1. ●查找函数web_reg_find() ● atoi():将字符串转换为整型值 作比较  > 0 Action() { //检查点函 ...