codeforces 622f
来源:互联网 发布:行知小学 程洁 编辑:程序博客网 时间:2024/05/18 20:32
第一道拉格朗日插值
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int maxn = 1000006;const LL mod = 1e9 + 7;int n, k;LL po[maxn];//prod_i(1~maxn)LL ne[maxn];//prod_i (-1~-maxn)LL c[maxn];//系数 po[i-1]*ne[i-k-2]LL S, Y[maxn];LL x, y;LL ex_gcd(LL num1, LL num2){if (!num2){x = 1;y = 0;return num1;}LL ans = ex_gcd(num2, num1%num2);LL tmp = x;x = y;y = tmp - (num1 / num2)*x;return ans;}LL inv(LL xx){ex_gcd(xx, mod);return (x % mod + mod) % mod;}void pre(){po[0] = ne[0] = 1;for (int i = 1; i <= k + 2; i++){int x = mod - i;po[i] = (po[i - 1] * i) % mod;ne[i] = (ne[i - 1] * x) % mod;}for (int i = 1; i <= k + 2; i++){c[i] = (inv(po[i - 1]) * inv(ne[k + 2 - i])) % mod;}S = 1;for (int i = n - k - 2; i <= n - 1; i++){S = (S*i) % mod;}}LL pow(LL a, int k){LL ret = 1;while (k){if (k & 1) ret = (ret * a) % mod;a = (a * a) % mod;k >>= 1;}return ret;}int main(){cin >> n >> k;if (!k){cout << n << endl;return 0;}for (int i = 1; i <= min(k + 2, n); i++){Y[i] = (Y[i - 1] + pow(i*1LL, k)) % mod;}if (n <= k + 2){printf("%d\n", Y[n]);return 0;}pre();LL ret = 0;for (int i = 1; i <= k + 2; i++){LL mid = (((c[i] * S) % mod) * Y[i])%mod;ret = (ret + mid * inv(n - i)) % mod;}cout << ret << endl;return 0;}
0 0
- codeforces 622f
- CodeForces 622F
- Codeforces 659F F
- CodeForces 622A--F - Infinite Sequence
- Codeforces 622F (拉格朗日插值 费马小定理)
- Codeforces 234 F. Fence
- 【Codeforces 500F】Dp
- codeforces 234F - Fence
- F-Logo Turtle codeforces
- 【CODEFORCES】 F. Ant colony
- Codeforces 567F
- CodeForces 241F Race
- Codeforces 611F 思维
- Codeforces 542F 构造
- codeforces 626f
- codeforces 628F
- codeforces 652F
- codeforces 632F
- vim快捷键操作
- java内存分配和String类型的深度解析
- RAC 环境 下 抓取ID 大于 应用ID
- poj1009——编码题
- PAT (Advanced Level) Practise 1004 Counting Leaves (30)
- codeforces 622f
- Java中内存分配及堆栈比较
- POJ1723----SOLDIERS
- Python练习(4):牛顿拉复生算法求解根
- Objective c手动写代码块
- DISPLAY environment variable not set!
- 树莓派系统终端中让文件和文件夹显示不同颜色的设置
- Codeforces 593A 2Char
- s2sh