题目大意:给你$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:

  1. #include <cstdio>
  2. #define maxn 1000010
  3. const int mod = 1e9 + 7;
  4. inline int pw(int base, int p) {
  5. static int res;
  6. 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;
  7. return res;
  8. }
  9. inline int inv(int x) {return pw(x, mod - 2);}
  10. inline void reduce(int &x) {x += x >> 31 & mod;}
  11.  
  12. int n, k, ans;
  13. int fac[maxn], s[maxn], prod = 1;
  14. int main() {
  15. scanf("%d%d", &n, &k);
  16. if (k == 0) {
  17. std::printf("%d\n", n);
  18. return 0;
  19. }
  20. for (int i = 0; i <= k + 1; i++) {
  21. prod = static_cast<long long> (n - i) * prod % mod;
  22. s[i] = pw(i, k);
  23. }
  24. fac[0] = 1;
  25. for (int i = 1; i <= k + 1; i++) {
  26. fac[i] = static_cast<long long> (fac[i - 1]) * i % mod;
  27. reduce(s[i] += s[i - 1] - mod);
  28. if (n == i) {
  29. std::printf("%d\n", s[i]);
  30. return 0;
  31. }
  32. }
  33. for (int i = 1; i <= k + 1; i++) {
  34. 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);
  35. reduce(ans);
  36. }
  37. printf("%d\n", ans);
  38. return 0;
  39. }

  

[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. HDU 4418 Time travel

    Time travel http://acm.hdu.edu.cn/showproblem.php?pid=4418 分析: 因为走到最后在折返,可以将区间复制一份,就变成了只往右走,01234321 ...

  2. Entity Framework Core 导航属性 加载数据

    Loading Related Data https://docs.microsoft.com/en-us/ef/core/querying/related-data Eager loading me ...

  3. hdu1058Humble Numbers(动态规划)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. python—IDLE的shell上下翻看历史

    Alt+p和Alt+n,分别向上(history previous)和向下(history next)调出使用过的历史命令.

  5. 蓝桥杯算法训练 区间k大数查询

    算法训练 区间k大数查询   问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二行包含n个正整数,表示给定的序列. 第三个 ...

  6. 自测之Lesson8:进程操作

    题目:请解释wait是如何同步父子进程的. 程序代码: #include <stdio.h> #include <unistd.h> #include <sys/type ...

  7. 软件工程 作业part1

    自我介绍 老师您好,我叫宋雨,本科在长春理工大学,专业是计算机科学与技术. 1.回想一下你曾经对计算机专业的畅想:当初你是如何做出选择计算机专业的决定?你认为过去接触的课程是否符合你对计算机专业的期待 ...

  8. Notes of the scrum meeting before publishing(12.19)

    meeting time:18:30~20:30p.m.,December 19th,2013 meeting place:3号公寓一层 attendees: 顾育豪                  ...

  9. 第二十一次ScrumMeeting会议

    第二十一次Scrum Meeting 时间:2017/12/11 地点:SPR咖啡馆 人员:王子铭 游心 解小锐 王辰昱 李金奇 杨森 陈鑫 赵晓宇 照片: 目前工作进展 名字 今日 明天的工作 蔡帜 ...

  10. 20145214 《Java程序设计》第4周学习总结

    20145214 <Java程序设计>第4周学习总结 教材学习内容总结 继承 继承基本上就是避免多个类间重复定义共同行为.要避免在程序设计上出现重复,可以把相同的程序代码提升为父类. 关键 ...