HDU - 233 Matrix
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5015
解题思路:一看到题目,感觉是杨辉三角形,然后用组合数学做,不过没想出来怎么做,后来看数据+递推思想,感觉可能是矩阵快速幂,可惜一直不知道a*10+3的 +3怎么处理,果然还是图样图森破啊!如果矩阵能搞出来的话,后面的就简单了,真可惜一直到比赛结束也没想出来,看来这种矩阵的题目做的太少了,真后悔线性代数没有认真学。。
今天晚上又想了一会,完全可以把+3那个放到新的一阶矩阵上,值始终等于3,那么对于233->2333->23333就可以得到矩阵:
1 0
1 10
把这个矩阵和(3,233)相乘就可以得到(3,2333),再乘就得到(3,23333)依次类推。
对于题目中的n,可以构造一个(n+2)*(n+2)的矩阵,可以推出递推矩阵:
1 0 0 0 0.....
1 10 0 0 0...
1 10 1 0 0...
1 10 1 1 0 ...
1 10 1 1 1 ...
前i行的后(i-1)个元素都是0,从第2行开始的每一行的第二个元素都是10,其他都是1。
然后把这个矩阵求p次幂,(用矩阵快速幂)再求出初始向量,最后把向量和刚刚的矩阵快速幂结果相乘,取出结果的最后一个数就是答案啦!
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define FOR(i,n) for(i=0;i<(n);i++)
#define CLR(a) memset(a,0,sizeof(a))
#define CIN(a) scanf("%d",&a)
using namespace std;
#define N 15
#define M 15
#define K 6
using namespace std;
typedef long long ll;
struct matrix
{
ll m[N][M];
int row,col;
};
matrix A,B,C,ANS;
matrix& matrix_mul(matrix &a,matrix &b,ll p,matrix &ans)//a,b矩阵相乘每个数模p
{
for(int i=;i<a.row;i++)
for(int j=;j<b.col;j++)
ans.m[i][j]=;
ans.row=a.row;
ans.col=b.col;
/*for(int i=0;i<a.row;i++)
for(int j=0;j<a.col;j++)
if(j<a.col-1)
printf("%d ",a.m[i][j]);
else printf("%d\n",a.m[i][j]);
cout<<endl;*/
for(int i=;i<a.row;i++)
for(int k=;k<a.col;k++)
{
for(int j=;j<b.col;j++)
ans.m[i][j]=(ans.m[i][j]+a.m[i][k]*b.m[k][j])%p;
}
return ans;
}
void print(matrix& a)
{
printf("->%d %d\n",a.row,a.col);
for(int i=;i<a.row;i++)
for(int j=;j<a.col;j++)
if(j<a.col-)
printf("%d ",a.m[i][j]);
else printf("%d\n",a.m[i][j]);
cout<<endl;
}
matrix z;
matrix &fast_matrixt_mul_mod(matrix &a,int b,ll p,matrix &ans)//矩阵a的b次方模p
{
for(int i=;i<N;i++)
for(int j=;j<M;j++)
if(i==j)ans.m[i][j]=;
else ans.m[i][j]=;
ans.row=a.row;
ans.col=a.col;
//cout<<"=="<<endl;
while(b)
{
if(b&)
{
b--;
ans=matrix_mul(ans,a,p,z);//ans=ans*a%m;
}
else
{
b/=;
a=matrix_mul(a,a,p,z);//a=a*a%m;
}
/*for(int i=0;i<ans.row;i++)
for(int j=0;j<ans.col;j++)
if(j<ans.col-1)
printf("%d ",ans.m[i][j]);
else printf("%d\n",ans.m[i][j]);
cout<<endl;*/
}
return ans;
}
matrix Mat,vec,ans;
#define mod 10000007
int main()
{
int n,p,i,j;
while(scanf("%d%d",&n,&p)!=EOF)
{
vec.row=n+;
vec.col=;
vec.m[][]=;
vec.m[][]=;
for(i=;i<=n+;i++)
{
scanf("%I64d",&vec.m[i][]);
}
Mat.col=Mat.row=n+;
CLR(Mat.m);
for(i=;i<=n+;i++)
{
for(j=;j<=i;j++)
{
if(j!=)Mat.m[i][j]=;
else Mat.m[i][j]=;
}
}
//print(Mat);
fast_matrixt_mul_mod(Mat,p,mod,ans);
matrix_mul(ans,vec,mod,Mat);
//print(Mat);
printf("%I64d\n",Mat.m[n+][]);
}
return ;
}
HDU - 233 Matrix的更多相关文章
- Spring-1-I 233 Matrix(HDU 5015)解题报告及测试数据
233 Matrix Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descript ...
- HDU - 5015 233 Matrix(杨辉三角/前缀+矩阵快速幂)
233 Matrix In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23 ...
- HDU 5015 233 Matrix(网络赛1009) 矩阵快速幂
先贴四份矩阵快速幂的模板:http://www.cnblogs.com/shangyu/p/3620803.html http://www.cppblog.com/acronix/archive/20 ...
- hdu 5015 233 Matrix (矩阵高速幂)
233 Matrix Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- HDU - 5015 233 Matrix (矩阵快速幂)
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...
- 233 Matrix(hdu5015 矩阵)
233 Matrix Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- [HDU5015]233 Matrix
[HDU5015]233 Matrix 试题描述 In our daily life we often use 233 to express our feelings. Actually, we ma ...
- HDU5015 233 Matrix(矩阵高速幂)
HDU5015 233 Matrix(矩阵高速幂) 题目链接 题目大意: 给出n∗m矩阵,给出第一行a01, a02, a03 ...a0m (各自是233, 2333, 23333...), 再给定 ...
- 233 Matrix(矩阵快速幂+思维)
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...
随机推荐
- Java8中时间日期库的20个常用使用示例
除了lambda表达式,stream以及几个小的改进之外,Java 8还引入了一套全新的时间日期API,在本篇教程中我们将通过几个简单的任务示例来学习如何使用Java 8的这套API.Java对日期, ...
- hadoop---Java 网络IO编程总结BIO、NIO、AIO
转载请注明出处:http://blog.csdn.net/anxpp/article/details/51512200,谢谢! 本文会从传统的BIO到NIO再到AIO自浅至深介绍,并附上完整的代码讲解 ...
- linux---(6/27)tr命令和sed命令详解
Tr命令: tr是简单的单个“字符”处理工具,而sed是功能非常强大的“字符串”处理工具. 用于查询,字符串2用于处理各种转换.tr刚执行时,字符串1中的字符被映射到字符串2中的字符,然后转换操作开始 ...
- ASP.NET WebAPI2 发布之后404 Not Found
方法一:首先确保服务器安装.Net FrameWork 4.0 并且注册IIS 方法二:对应应用程序池版本为v4.0,模式为集成 方法三:在web.config中加入 <system.webSe ...
- ORM实例介绍
http://blog.csdn.net/RonoTian/article/details/2900714
- React Native之Fetch简单封装、获取网络状态
1.Fetch的使用 fetch的使用非常简单,只需传入请求的url fetch('https://facebook.github.io/react-native/movies.json'); 当然是 ...
- jz2440存储管理实验【学习笔记】
平台:jz2440 作者:庄泽彬(欢迎转载,请注明作者) 说明:韦东山一期视频学习笔记 简介:先来简单的说明一下这次的实验,看看下图,我们的程序通过烧录器下载到nandflash当中去,之后在启动的时 ...
- Json -- 语法和示例,javascript 解析Json
1. 语法 JSON(JavaScriptObject Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不 ...
- Bootstrap——优秀的开源前端框架
Bootstrap是著名的社交网站.微博的先驱Twitter在2011年8月推出的开源WEB前端框架,集合CSS和HTML,使用了最新的浏览器技术,为快速WEB开发提供了一套前端工具包,包括布局.网格 ...
- Memcached set 命令
Memcached set 命令用于将 value(数据值) 存储在指定的 key(键) 中. 如果set的key已经存在,该命令可以更新该key所对应的原来的数据,也就是实现更新的作用. 语法: s ...