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 ...
随机推荐
- PAT 天梯赛 L1-041. 寻找250 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-041 AC代码 #include <iostream> #include <cstdio&g ...
- python之路——MySQL数据库
1 MySQL相关概念介绍 MySQL为关系型数据库(Relational Database Management System), 这种所谓的"关系型"可以理解为"表格 ...
- Entity FrameWork Code First无法生成数据库 解决办法
我是控制台应用程序,没有connectionStrings,试了几个方法也都不可以. 这是别人的博客用其他方法. http://www.cnblogs.com/Gyoung/archive/2013/ ...
- Javascript作用域详解。
javascript的作用域 是按照 函数来划分的. 网址:http://www.cnblogs.com/rubylouvre/archive/2009/08/21/1551270.html
- CPU与GPU区别 通俗易懂
转:https://blog.csdn.net/xiaolang85/article/details/51500340 有网友在网上提问:“为什么现在更多需要用的是 GPU 而不是 CPU,比如挖矿甚 ...
- Zabiix 监控图形乱码问题
Zabiix切换为中文 配置中文乱码问题 在C:\Windows\Fonts中复制想要的字体,后缀为ttf,若本身问大写,请改成小写的文件后缀ttf,并上传至zabbix服务器的/usr/local/ ...
- react native 之异步请求
第一章 异步请求 fetch的运用 在react native 中异步请求一般用fetch这个方法, fetch的格式如下: const params ={ "charset" ...
- 金中半日baoling游-----stoi
蒟蒻又来水博客了,写个游记啦啦啦啦,好像是第一篇游记咯. 温馨提示:愚人节写的博客看了后会变棒棒哦!(麻麻再也不用担心我被骗) 进入正题 3月31日早6:30左右起床了,然后就是....(此处可省略) ...
- Mysql 分组聚合实现 over partition by 功能
mysql中没有类似oracle和postgreSQL的 OVER(PARTITION BY)功能. 那么如何在MYSQL中搞定分组聚合的查询呢 先说结论: 利用 group_concat + sub ...
- Sort Colors,颜色排序
问题描述:Given an array with n objects colored red, white or blue, sort them so that objects of the same ...