[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 ...
随机推荐
- spring-boot、mybatis整合
一.MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 X ...
- 追书神器API
由于自己喜欢看小说,有的时候不方便手机看的时候希望在电脑上面看,但很多网站有广告啊,于是封装了套手机版的追书神器API 目前只做了搜索 详情 书评 换源 正文 调用方式: //搜索小说 var sea ...
- hdu5698瞬间移动(杨辉三角+快速幂+逆元)
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- OSG-CompositeViewer
原文连接地址:http://www.osgchina.org/index.php?Itemid=490&id=134:usecompositiv&option=com_content& ...
- Python搭配unittest
unittest是Python的单元测试框架, 类似于Java里面的TestNG. Unittest.py: import unittest from selenium import webdrive ...
- linux系统简单命令
# uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostn ...
- JAVA基础学习之路(五)数组的定义及使用
什么是数组:就是一堆相同类型的数据放一堆(一组相关变量的集合) 定义语法: 1.声明并开辟数组 数据类型 数组名[] = new 数据类型[长度]: 2.分布完成 声明数组:数据类型 数组名 [] = ...
- NodeJs学习笔记01-你好Node
如果你对NodeJs略知一二,不禁会感叹,使用JS的语法和代码习惯就能开发一个网站的后台,实现复杂的数据交互,牛! 对于学习java和php就夹生的小码农来说,简直就是靡靡之音呐~~~ 今晚带着忐忑的 ...
- dotnetframe的清理工具
微软的产品一向不敢恭维,卸载都没有办法卸载干净,卸载又慢又不彻底,dotnet被我卸载之后还有注册表残留以至于无法重新安装. .NET Framework Cleanup Tool真的很好用,全部版本 ...
- solidity 十六进制字符串转十六进制bytes
pragma solidity ^0.4.16; contract Metadata { // 十六进制字符串转换成bytes function hexStr2bytes(string data)re ...