hdu 5015 233 Matrix(构造矩阵)
http://acm.hdu.edu.cn/showproblem.php?pid=5015
由于是个二维的递推式,当时没有想到能够这样构造矩阵。从列上看,当前这一列都是由前一列递推得到。依据这一点来构造矩阵。令b[i]代表第i列,是一个(n+2)*1的矩阵,即b[1] = [1,233......],之所以在加了两行,是要从前一个矩阵b[i-1]得到b[i]中的第二个数2333...,再构造一个转换矩阵a,它是一个(n+2)*(n+2)的矩阵,那么a^(m-1) *
b就是第m列。
/*
a矩阵:
1 0 0 0 0...
3 10 0 0 0...
3 10 1 0 0...
3 10 1 1 0...
3 10 1 1 1...
.. b矩阵:第1列 */
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL __int64
//#define LL long long
#define eps 1e-9
#define PI acos(-1.0)
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 10000007; struct matrix
{
LL mat[15][15];
void init()
{
memset(mat,0,sizeof(mat));
for(int i = 0; i < 15; i++)
{
mat[i][i] = 1;
}
}
}a,b,res; int n,m; matrix mul(matrix a, matrix b)
{
matrix ans;
memset(ans.mat,0,sizeof(ans.mat));
for(int i = 0; i < n+2; i++)
{
for(int k = 0; k < n+2; k++)
{
if(a.mat[i][k] == 0)
continue;
for(int j = 0; j < n+2; j++)
{
ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j])%mod;
ans.mat[i][j] %= mod;
}
}
}
return ans;
} matrix pow(matrix a, int n)
{
matrix ans;
ans.init();
while(n)
{
if(n&1)
ans = mul(ans,a);
n >>= 1;
a = mul(a,a);
}
return ans;
} int main()
{
int x;
while(~scanf("%d %d",&n,&m))
{
memset(b.mat,0,sizeof(b.mat));
b.mat[0][0] = 1;
b.mat[1][0] = 233;
for(int i = 2; i < n+2; i++)
{
scanf("%d",&x);
b.mat[i][0] = (b.mat[i-1][0] + x%mod)%mod;
} memset(a.mat,0,sizeof(a.mat));
a.mat[0][0] = 1;
for(int i = 1; i < n+2; i++)
{
a.mat[i][0] = 3;
a.mat[i][1] = 10;
for(int j = 2; j <= i; j++)
a.mat[i][j] = 1;
}
res = pow(a,m-1);
LL anw = 0;
for(int i = 0; i < n+2; i++)
{
anw += (res.mat[n+1][i] * b.mat[i][0])%mod;
anw %= mod;
}
printf("%I64d\n",anw);
}
return 0;
}
hdu 5015 233 Matrix(构造矩阵)的更多相关文章
- 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(网络赛1009) 矩阵快速幂
先贴四份矩阵快速幂的模板:http://www.cnblogs.com/shangyu/p/3620803.html http://www.cppblog.com/acronix/archive/20 ...
- HDU - 5015 233 Matrix (矩阵快速幂)
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...
- 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 233Matrix (构造矩阵)
233 Matrix Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- HDU 5015 233 Matrix --矩阵快速幂
题意:给出矩阵的第0行(233,2333,23333,...)和第0列a1,a2,...an(n<=10,m<=10^9),给出式子: A[i][j] = A[i-1][j] + A[i] ...
- HDU 5015 233 Matrix
题意:给定一个矩阵的第0列的第1到n个数,第一行第1个数开始每个数分别为233, 2333........,求第n行的第m个数. 分析: 其实也没那么难,自己想了半天还没往对的方向想,m最大1e9,应 ...
- ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)
Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...
- hdu 4965 Fast Matrix Calculation(矩阵高速幂)
题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...
随机推荐
- 控件风格19种,必须倒背如流——其实就是控件所拥有的能力,即有条件使用VCL框架所提供的(功能)代码
{ New TControlStyles: csNeedsBorderPaint and csParentBackground. These two ControlStyles are only ap ...
- C++中 auto自己主动变量,命名空间,using作用以及作用域
1.autokeyword的用途 A:自己主动变量.能够自己主动获取类型,输出,类似泛型 B:自己主动变量,能够实现自己主动循环一维数组 C:自己主动循环的时候,相应的必须是常量 2.auto自 ...
- 网页 css
css---- 层叠样式表(Cascading Style Sheet) 一,层叠样式表的分类 1,外部样式表:在外部定义样式表,然后在页面head里面附加该样式表 2,内嵌样式表:直接在网页head ...
- Socket 心跳包机制总结
跳包之所以叫心跳包是因为:它像心跳一样每隔固定时间发一次,以此来告诉服务器,这个客户端还活着.事实上这是为了保持长连接,至于这个包的内容,是没有什么特别规定的,不过一般都是很小的包,或者只包含包头的一 ...
- javascript学习笔记--迭代函数
概要 这里的迭代函数指的是对数组对象的操作方法,js数组共有五个迭代函数:every.fifter.forEach.map.some. 1.every every方法,返回值为Boolean类型,tr ...
- [Android学习笔记]Fragment使用
一.android.app.Fragment 与 android.support.v4.app.Fragment 区别 support.v4.app.Fragment是为了给低版本Android使用的 ...
- 【Demo 0005】视图控制器
本章学习要点: 1. 熟悉MVC模型及模型中(Modal, View, Control)用途: 2. 了解iOS中常见的几种视图控制器及使用场景: 3. 掌握 ...
- 3xian退役贴【深思。】
这是原文: 最后一天,漫天飘起了雪花,假装欢送我离去. 这次WF之战不太顺利,早期的C题大概花了1秒钟构思,然而由于输出格式多了一个空格直到两个半小时才逃脱Wrong Answer的纠缠.还好lynn ...
- Java设计模式之适配器模式(Adapter Pattern)
Adapter Pattern的作用是在不改变功能的前提下转换接口.Adapter分为两类,一类是Object Adapter, 还有一类是Class Adapter.因为Class Adapter的 ...
- linux-sfdisk 使用方法
功能说明:硬盘分区工具程序. 语 法:sfdisk [-?Tvx][-d <硬盘>][-g <硬盘>][-l <硬盘>][-s <分区>][-V < ...