hdu 5015 233 Matrix 矩阵快速幂
来源:互联网 发布:三端口光纤环形器 编辑:程序博客网 时间:2024/05/22 01:32
233 MatrixTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 98 Accepted Submission(s): 47
Total Submission(s): 98 Accepted Submission(s): 47
Problem Description
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333... (it means a0,1 = 233,a0,2 = 2333,a0,3 = 23333...) Besides, in 233 matrix, we got ai,j = ai-1,j +ai,j-1( i,j ≠ 0). Now you have known a1,0,a2,0,...,an,0, could you tell me an,m in the 233 matrix?
Input
There are multiple test cases. Please process till EOF.
For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 109). The second line contains n integers, a1,0,a2,0,...,an,0(0 ≤ ai,0 < 231).
For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 109). The second line contains n integers, a1,0,a2,0,...,an,0(0 ≤ ai,0 < 231).
Output
For each case, output an,m mod 10000007.
Sample Input
1 112 20 03 723 47 16
Sample Output
234279972937Hint
构造矩阵就行了。具体看代码把,可以用print 把矩阵打出来看;
不知道为什么G++能过,C++数组越界,路过大神求指点。
- #include<stdio.h>
- #include<string.h>
- #define Matr 15 //矩阵大小,注意能小就小 矩阵从1开始 所以Matr 要+1
- #define ll __int64
- struct mat//矩阵结构体,a表示内容,size大小 矩阵从1开始 但size不用加一
- {
- ll a[Matr][Matr];
- mat()//构造函数
- {
- memset(a,0,sizeof(a));
- }
- };
- int Size;
- ll mod= 10000007;
- mat multi(mat m1,mat m2)//两个相等矩阵的乘法,对于稀疏矩阵,有0处不用运算的优化
- {
- mat ans=mat();
- for(int i=1;i<=Size;i++)
- for(int j=1;j<=Size;j++)
- if(m1.a[i][j])//稀疏矩阵优化
- for(int k=1;k<=Size;k++)
- ans.a[i][k]=(ans.a[i][k]+m1.a[i][j]*m2.a[j][k])%mod;//i行k列第j项
- return ans;
- }
- mat quickmulti(mat m,ll n)//二分快速幂
- {
- mat ans=mat();
- int i;
- for(i=1;i<=Size;i++)ans.a[i][i]=1;
- while(n)
- {
- if(n&1)ans=multi(m,ans);
- m=multi(m,m);
- n>>=1;
- }
- return ans;
- }
- void print(mat m)//输出矩阵信息,debug用
- {
- int i,j;
- printf("%d\n",Size);
- for(i=1;i<=Size;i++)
- {
- for(j=1;j<=Size;j++)
- <span style="white-space:pre"> </span>printf("%I64d ",m.a[i][j]);
- printf("\n");
- }
- }
- int main()
- {
- int i;
- mat gou,chu=mat();//构造矩阵 初始矩阵
- int n,m;
- __int64 ans;
- while(scanf("%I64d%I64d",&n,&m)!=EOF)//n+2*n+2
- {
- Size=n+2;
- chu=mat();
- chu.a[1][n+1]=23;
- for(i=1;i<=n;i++)
- {
- scanf("%I64d",&chu.a[1][i]);
- chu.a[1][i]%=mod;
- }
- chu.a[1][n+2]=3;
- gou=mat();
- for(int j=1;j<=n;j++)
- {
- if(j==1)
- {
- gou.a[n+1][j]=10;
- gou.a[n+2][j]=1;
- }
- gou.a[j][j]=1;
- for(int i=1;i<=n+2;i++)
- {
- gou.a[i][j]+=gou.a[i][j-1];
- }
- }
- gou.a[n+1][n+1]=10;
- gou.a[n+2][n+2]=1;
- gou.a[n+2][n+1]=1;
- ans=multi(chu,quickmulti(gou,m)).a[1][n];
- printf("%I64d\n",ans%mod);
- }
- return 0;
- }
0 0
- 【HDU】5015 233 Matrix 矩阵快速幂
- hdu 5015 233 Matrix 矩阵快速幂
- hdu 5015 233 Matrix(矩阵快速幂)
- hdu 5015 Matrix 233 矩阵快速幂
- hdu 5015 233 Matrix(矩阵快速幂)
- HDU 5015 233 Matrix 矩阵快速幂
- [矩阵快速幂] hdu 5015 233 Matrix
- hdu 5015 233 Matrix 矩阵快速幂
- Hdu 5015 233 Matrix 矩阵快速幂
- HDU 5015 233 Matrix --矩阵快速幂
- hdu 5015 233 Matrix (矩阵快速幂)
- HDU 5015-233 Matrix-矩阵快速幂
- HDU 5015 233 Matrix 【矩阵快速幂】
- 233 Matrix hdu 5015(矩阵快速幂)
- HDU 5015 233 Matrix --矩阵快速幂
- hdu 5015-233 Matrix(矩阵快速幂)
- HDU 5015 - 233 Matrix (矩阵构造 矩阵快速幂)
- 矩阵快速幂 HDU 233 Matrix
- 6.2 Git 工具 - 储藏(Stashing)
- ThoughtWorks待遇
- Integer.valueOf(Integer intObj)运行过程及引发的其它问题
- 6.4 Git 工具 - 重写历史
- (mini2440)建立交叉编译环境+配置linux内核
- hdu 5015 233 Matrix 矩阵快速幂
- Best Time to Buy and Sell Stock
- project facet java version 1.8
- Node.js v0.10.31API手册-简介
- HDU 5023 A Corrupt Mayor's Performance Art
- 奇偶剪枝
- Linux 添加开机启动项的两种方法
- 推荐13款JavaScript图形和图表绘制工具
- 数据时代的企业管理——读《量化》的随手札记