hdoj 1757 A Simple Math Problem(转化为矩阵+矩阵乘法)
来源:互联网 发布:淘宝手机排行怎么看 编辑:程序博客网 时间:2024/04/30 04:19
【题目大意】:If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .
先给出ai,和x,m,求f(x)%m
【解题思路】:
根据题目意思构造矩阵,跑矩阵乘法即可
【代码】:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>#include <cmath>#include <string>#include <cctype>#include <map>#include <iomanip> using namespace std; #define eps 1e-8#define pi acos(-1.0)#define inf 1<<30#define linf 1LL<<60#define pb push_back#define lc(x) (x << 1)#define rc(x) (x << 1 | 1)#define lowbit(x) (x & (-x))#define ll long long struct Mart{ int mat[105][105]; }; Mart mm;int n; Mart y; int k,m,i; int sum; Mart mart_multiply(Mart a,Mart b){ Mart c; for(int i=1; i<=10; i++) for(int j=1; j<=10; j++){ c.mat[i][j]=0; for(int k=1; k<=10; k++) c.mat[i][j]=(c.mat[i][j]%m+a.mat[i][k]*b.mat[k][j]%m)%m; } return c; } void copy(Mart p){ for (int i=1; i<=10; i++) for (int j=1; j<=10; j++) y.mat[i][j]=p.mat[i][j]; } void solve_mart_power(int np){ Mart p,q; for(int i=1; i<=10; i++) for(int j=1; j<=10; j++){ p.mat[i][j]=mm.mat[i][j]; if(i==j) q.mat[i][j]=1.0; else q.mat[i][j]=0.0; } if(np==0) copy(q); else { while(np!=1){ if(np&1){ np--; q=mart_multiply(p,q); } else{ np=np/2; p=mart_multiply(p,p); } } p=mart_multiply(p,q); copy(p); } return ; } int main() { for (int i=1; i<=10; i++){mm.mat[i+1][i]=1; mm.mat[1][i]=1;} while (~scanf("%d%d",&k,&m)) { for (int i=1; i<=10; i++) scanf("%d", &mm.mat[1][i]); if (k<10) { printf("%d/n",k%m); } else { solve_mart_power(k-9); sum=0; for (int i=1; i<=10; i++) sum+=y.mat[1][i]*(10-i); printf("%d\n", sum % m); } } return 0; }
- hdoj 1757 A Simple Math Problem(转化为矩阵+矩阵乘法)
- 【矩阵乘法】hdu hdoj 1757 A Simple Math Problem
- HDOJ-1757A Simple Math Problem(矩阵乘法快速幂)
- hdu1757 A Simple Math Problem(矩阵乘法)
- A Simple Math Problem 矩阵乘法
- hdu1757 A Simple Math Problem 矩阵乘法
- hdu 1757 A Simple Math Problem(矩阵乘法)
- hdu 1757 A Simple Math Problem 矩阵乘法解线性方程
- hdu 1757 A Simple Math Problem(矩阵乘法)
- Hdu 1757 A Simple Math Problem//矩阵乘法
- hdoj-1757-A Simple Math Problem【矩阵的快速幂】
- hdoj 1757 A Simple Math Problem 【矩阵快速幂】
- hdoj 1757 A Simple Math Problem 【矩阵快速幂】
- HDOJ 1757 A Simple Math Problem (矩阵快速幂)
- hdu 1757 A Simple Math Problem 矩阵
- hdu 1757 A Simple Math Problem 矩阵
- 【HDU 1757 A Simple Math Problem】+ 矩阵
- hdu1757A Simple Math Problem 矩阵乘法
- Android开发教程:斗地主 [牌桌实现源码]
- 1000万粒子分块渲染
- Node.js集成开发环境Cloud9搭建及CoffeeScript插件使用
- 黑客字典II算法分析
- RadioButtonList的数据绑定故事
- hdoj 1757 A Simple Math Problem(转化为矩阵+矩阵乘法)
- 阮一峰:jQuery最佳实践
- jQuery性能优化的28个建议
- Android sdk安装
- 写给自己,写给项目经理转产品经理的人,呃,项目经理刚转产品经理的人
- 利用Python将word 2007的文档转为pdf文件
- The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local Machine 完美解决
- MediaWiki编辑工具
- [Erlang 学习笔记]ubuntu 11.10 搭建erlang环境