原题地址: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的更多相关文章

  1. Spring-1-I 233 Matrix(HDU 5015)解题报告及测试数据

    233 Matrix Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Descript ...

  2. HDU - 5015 233 Matrix(杨辉三角/前缀+矩阵快速幂)

    233 Matrix In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23 ...

  3. HDU 5015 233 Matrix(网络赛1009) 矩阵快速幂

    先贴四份矩阵快速幂的模板:http://www.cnblogs.com/shangyu/p/3620803.html http://www.cppblog.com/acronix/archive/20 ...

  4. hdu 5015 233 Matrix (矩阵高速幂)

    233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  5. HDU - 5015 233 Matrix (矩阵快速幂)

    In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...

  6. 233 Matrix(hdu5015 矩阵)

    233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. [HDU5015]233 Matrix

    [HDU5015]233 Matrix 试题描述 In our daily life we often use 233 to express our feelings. Actually, we ma ...

  8. HDU5015 233 Matrix(矩阵高速幂)

    HDU5015 233 Matrix(矩阵高速幂) 题目链接 题目大意: 给出n∗m矩阵,给出第一行a01, a02, a03 ...a0m (各自是233, 2333, 23333...), 再给定 ...

  9. 233 Matrix(矩阵快速幂+思维)

    In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...

随机推荐

  1. 系统OOM复位定位

    定位OOM的工具: 1.多次收集Thread Dump信息kill -3  PID通过对比分析heap 对象信息和Thread信息来定位 2.通过 -Xloggc:D:/gc.log  -XX:+He ...

  2. kali 2016:mount ntfs 分区只读 --Falling back to read-only mount because the NTFS partition is in an unsafe state.

    mount ntfs 分区 mount /dev/sdb1 /mnt/d 提示: The disk contains an unclean file system (0, 0).Metadata ke ...

  3. float 为什么可以表示很大的整数

    1.float型:单精度浮点数在机内占4个字节,用32位二进制描述(注意:计算机中1个字节=8位). 2.浮点数在机内用指数型式表示,分解为:数符,尾数,指数符,指数四部分. 3.可以算出float型 ...

  4. Hibernate : Query.list()、Query.iterator()的区别

    Query上有list()与iterator()方法,两者的差别在于list()方法在读取数据时,并不会利用到快取,而是直接再向数据库查询,而iterator()则将读取到的数据写到快取,并于读取时再 ...

  5. Your app uses or references the following non-public APIs的解决方案

    之前接了一个旧的项目,代码混乱,年代久远,不得不吐槽一波,好不容易改完需求提交代码,说用到了non-public APIs,搞了好久终于找到地方了,下面是我的解决过程,让大家少走弯路: 下面的被驳回的 ...

  6. CocoaPods学习系列4——进阶用法

    这篇文章,记录一下CocoaPods的进阶用法. 进阶用法主要体现在.podspec文件和Podfile的配置上. .podspec文件的进阶配置 以官方的一个.podspec文件示例细说: Pod: ...

  7. JAVA实现计算三角形等平面图形的夹角问题

    问题重现 现在一平面上有三点,分别是x(x1,x2),y(y1,y2),z(z1,z2),图形大致如下 现要求用java代码求出∠YxZ的度数. 问题分析及数学模型 1.要求两直线的夹角,就想到数学中 ...

  8. Standard 1.1.x VM与Standard VM的区别

    在Eclipse或MyEclipse中要设置Installed JREs时,有三个选择: - Execution Environment Description - Standard 1.1.x VM ...

  9. Mac系统下安装ipython分别支持python2和python3

    操作系统:Mac10.11.5 python2.7.13 python3.6.1 安装python2: brew install python 安装python3: brew install pyth ...

  10. Ajax-05 使用XMLHttpRequest和jQuery实现Ajax实例

    需求: (django)使用XMLHttpRequest和jQuery实现Ajax加法运算 url.py: from django.conf.urls import url from hello im ...