51nod oj 1137 1242 <结构体内重载运算符求矩阵乘法>
2016-08-07 22:59
253 查看
链接:1137
重载运算符--重新定义运算符的作用--使返回自己想要的结果-.-
代码:
题目链接:1242
与这题一样:矩阵快速幂【模板】求Fibonacci数列
原理请看链接的题--
下面这个代码进行了乘法优化--
代码:
重载运算符--重新定义运算符的作用--使返回自己想要的结果-.-
代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct node{ int n; int a[102][102]; void clear(){ n=0; memset(a,0,sizeof(a)); } node operator * (const node &b)const{ node c;c.clear(); c.n=n; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) for (int k=1;k<=n;k++) c.a[i][j]+=a[i][k]*b.a[k][j]; return c; } }a,b,c; int main() { scanf("%d",&a.n); int nn=a.n; for (int i=1;i<=nn;i++) for (int j=1;j<=nn;j++) scanf("%d",&a.a[i][j]); b=a; for (int i=1;i<=nn;i++) for (int j=1;j<=nn;j++) scanf("%d",&b.a[i][j]); c=a*b; for (int i=1;i<=nn;i++) { for (int j=1;j<nn;j++) printf("%d ",c.a[i][j]); printf("%d\n",c.a[i][nn]); } return 0; }
题目链接:1242
与这题一样:矩阵快速幂【模板】求Fibonacci数列
原理请看链接的题--
下面这个代码进行了乘法优化--
代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define LL long long LL yu,n; struct node{ int n; LL a[5][5]; void clear(){ memset(a,0,sizeof(a)); } node operator * (const node &b)const{ node c;c.clear(); c.n=n; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) { for (int k=1;k<=n;k++) { c.a[i][j]+=a[i][k]*b.a[k][j]; c.a[i][j]%=yu; } } return c; } }a,lp; int main() { yu=1000000009; scanf("%lld",&n);lp.n=2;a.n=2; lp.a[1][1]=lp.a[2][2]=1;lp.a[1][2]=lp.a[2][1]=0; a.a[1][1]=a.a[1][2]=a.a[2][1]=1;a.a[2][2]=0; while (n) { if (n%2) { lp=lp*a; } a=a*a; n/=2; } printf("%lld\n",lp.a[2][1]); return 0; }
相关文章推荐
- (noip 模拟 Matrix)<逃避矩阵乘法的好方法#滑稽>
- 【DP】poj1651 <矩阵链乘法>
- OpenCL案例研究之一<矩阵乘法>
- 利用CUDA的矩阵乘法1 <利用 Kahan's Summation Formula 来提高CUDA 的浮点数运算精确度>
- <Python> Numpy中的矩阵乘法问题
- 数据结构之(动态规划二)之<矩阵链乘法>
- <模板> Hdu 1402 A * B Problem Plus 大数乘法
- 《Windows程序设计》读书笔记------------->>窗口和消息 -- 代码结构和消息机制<<
- [矩阵乘法+KMP] BZOJ1009: [HNOI2008]GT考试
- HTML基础知识一(HTML、常见方式、文档结构、<meta>、 网站文件命名、文本修饰、文本样式、段落标记、居中标记、水平线标记、特殊字符、列表、图像)
- <模拟电子学习1>Multisim 12.0 结构和仿真51最小的单芯片系统
- <include>标签和BaseActivity联合使用简化代码结构
- 数据结构与算法之散列(线性/平方/双平方探测法)<八>
- < Unity 3D专栏 >Transform结构图
- <复习>数据结构中的结构体
- <四>java数据结构与算法 插入排序
- <sdut-ACM>数据结构实验之查找六:顺序查找
- 拓扑结构相同子树-------------->_<
- 微信小程序开发<二>文件结构解析
- <zz>模式识别中分类器性能评估参数的解释---混淆矩阵,ROC