srm#397_div1_500pt 矩阵乘法+快速模幂
2014-03-16 20:17
351 查看
题目地址:srm#397_div1_500
题目描述:
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
构造一个k+2阶矩阵;
这里n比较大 显然我们接受不了O(n)的复杂度
最好的方法就是像求Fibonacci数列第n项一样用矩阵乘法+快速幂做
要实现求和长度在k左右的递推式
首选(n+1)^k-n^k=sigma(c[k][i]*n^i);
直接看代码里面的矩阵构造吧
wa了几次
1 组合数是要求到c[52][i]的
2 因为mod 10^9+7 所以存在的数都是可能接近int上限的 要进行+运算 所以要用long long 存储
代码:
tc上提交没有main()
题目描述:
Problem Statement | ||||||||||||
NOTE: This problem statement contains superscripts that may not display properly if viewed outside of the applet. You are given ints n and k. Return the value of the sum 1k + 2k + 3k + ... + nk modulo 1000000007. | ||||||||||||
Definition | ||||||||||||
| ||||||||||||
Limits | ||||||||||||
| ||||||||||||
Constraints | ||||||||||||
- | n will be between 1 and 109, inclusive. | |||||||||||
- | k will be between 1 and 50, inclusive. | |||||||||||
Examples | ||||||||||||
0) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
|
构造一个k+2阶矩阵;
这里n比较大 显然我们接受不了O(n)的复杂度
最好的方法就是像求Fibonacci数列第n项一样用矩阵乘法+快速幂做
要实现求和长度在k左右的递推式
首选(n+1)^k-n^k=sigma(c[k][i]*n^i);
直接看代码里面的矩阵构造吧
wa了几次
1 组合数是要求到c[52][i]的
2 因为mod 10^9+7 所以存在的数都是可能接近int上限的 要进行+运算 所以要用long long 存储
代码:
#include<iostream> typedef long long inta ; using namespace std; struct Matrix { inta m[60][60]; }; inta n; // 用来表示维度 inta c[60][60]; //组合数 const inta mod=1000000007; void init() { for(inta i=0;i<=55;i++) c[i][0]=1; for(inta i=0;i<55;i++) for(inta j=0;j<=i;j++) c[i+1][j+1]=c[i][j+1]+c[i][j]; } Matrix multi(Matrix a,Matrix b) { Matrix ans; for(inta i=0;i<n;i++) for(inta j=0;j<n;j++) { inta c=0; for(inta k=0;k<n;k++) c=(c+a.m[i][k]*b.m[k][j])%mod; ans.m[i][j]=c; } return ans; } Matrix quick_mod(Matrix a,inta b) { Matrix ans; Matrix p=a; for(inta i=0;i<n;i++) for(inta j=0;j<n;j++) ans.m[i][j]=(i==j?1:0); while(b) { if(b&1) { ans=multi(ans, p); b--; } b>>=1; p=multi(p, p); } return ans; } class SumOfPowers { public : inta value(inta nn,inta k) { init(); n=k+2; Matrix A; for(inta i=0;i<n;i++) // so important for(inta j=0;j<n;j++) A.m[i][j]=0; for(inta i=0;i<k+1;i++) for(inta j=0;j<=i;j++) A.m[i][j]=c[i][j]%mod; for(inta i=0;i<k+1;i++) A.m[k+1][i]=c[k][i]%mod; A.m[k+1][k+1]=1; A=quick_mod(A, nn-1); inta ans=0; for(inta i=0;i<k+2;i++) ans=(ans+A.m[k+1][i])%mod; return ans; } }; int main() { inta nn,k; cin>>nn>>k; SumOfPowers obj; cout<<obj.value(nn, k)<<endl; }
tc上提交没有main()
相关文章推荐
- srm#397_div1_500pt 矩阵乘法+快速模幂
- 继递归递推求斐波那契后的另一种奇妙方法---矩阵乘法+快速幂运算
- Codeforces Round #226 (Div. 2) E(矩阵快速幂)
- Codeforces Round #373 (Div. 1)C(线段树维护矩阵,矩阵快速幂)
- POJ 3233 Matrix Power Series (矩阵乘法+快速幂+等比二分求和) -
- hdu4549 斐波那契数列(乘法递推) + 欧拉定理 (快速幂,模幂)
- BestCoder Round #68 (div.1) B 矩阵乘法优化DP
- 【矩阵乘法+快速幂】数学序列
- BZOJ-2875 随机数生成器 矩阵乘法快速幂+快速乘
- 那啥,,矩阵乘法,矩阵快速幂模板
- CDOJ 1280 772002画马尾 每周一题 div1 矩阵快速幂
- codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数
- hdu 1575 Tr A(矩阵快速幂乘法优化算法)
- TJU-3848 Game(矩阵乘法快速幂)
- HDOJ-1757A Simple Math Problem(矩阵乘法快速幂)
- 矩阵乘法快速幂 cojs 1717. 数学序列
- Xn数列(矩阵乘法+快速幂+慢速乘法)
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo [矩阵快速幂]
- codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数
- hdu 4602 Partition(矩阵快速幂乘法)