HDU 5015 233 Matrix
题意:给定一个矩阵的第0列的第1到n个数,第一行第1个数开始每个数分别为233, 2333........,求第n行的第m个数。
分析:
其实也没那么难,自己想了半天还没往对的方向想,m最大1e9,应该立即想到要用到快速幂,关键在于递推矩阵。
递推矩阵的作用是依次算出每一列,第1列第一个数233是前一列的23*10+3,将前一列增加一个元素[3, 23, a1, a2,....an],然后第一列第一个数为3,对应向量[1, 0, 0, 0.....0],233对应向量[1, 10, 0, .....0],下一个的数对应[1, 10, 1.....0], 接下来每个数对应的向量是上一个数的向量最后一个1元素后面增加1个1,这样将n+2个向量构成一个(n+2)*(n+2)的矩阵,需要求m次该矩阵,然后和[3, 23, a1, a2, .....an]相乘。
代码:
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#define inf 0x0f0f0f0f
#define pb push_back
#define bug(x) printf("line %d: >>>>>>>>>>>>>>>\n", (x));
#define in freopen("F:\\code\\data\\data.txt", "r", stdin);
#define out freopen("F:\\code\\data\\data_out.txt", "w", stdout); #define SZ(x) ((int)x.size())
#define lson rt<<1, l, m
#define rson rt<<1|1, m+1, r
using namespace std; typedef long long LL;
const int maxn = ;
const int M = ; struct Matrix
{
int n, m;
LL a[maxn][maxn];
Matrix(int n, int m)
{
this->n = n;
this->m = m;
for(int i = ; i < maxn; i++)
for(int j = ; j < maxn; j++)
a[i][j] = ;
}
Matrix operator * (const Matrix &o)const
{
Matrix c(n, o.m);
for(int i = ; i < n; i++)
for(int j = ; j < o.m; j++)
{
for(int k = ; k < m; k++)
c.a[i][j] = (c.a[i][j]+a[i][k]*o.a[k][j]%M)%M;
}
return c;
}
};
int n, m;
int main()
{ while(scanf("%d%d", &n, &m) == )
{
Matrix f(n+, n+), res(n+, n+), tmp(n+, );
tmp.a[][] = ;
tmp.a[][] = ;
for(int i = ; i <= n+; i++)
scanf("%I64d", &tmp.a[i][]);
for(int i = ; i <= n+; i++)
for(int j = ; j <= n+; j++)
{
if(i == )
{
f.a[i][] = ;
break;
}
else if(i == )
{
f.a[i][] = ;
f.a[i][] = ;
break;
}
else
{
if(j < i)
f.a[i][j] = f.a[i-][j];
else if(j == i) f.a[i][j] = ;
}
}
for(int i = ; i < n+; i++)
for(int j = ; j < n+; j++)
if(i == j)
res.a[i][j] = ;
while(m)
{
if(m&)
res = res*f;
f = f*f;
m >>= ;
}
tmp = res*tmp;
printf("%I64d\n", tmp.a[n+][]);
}
return ;
}
HDU 5015 233 Matrix的更多相关文章
- 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 ...
- hdu 5015 233 Matrix(构造矩阵)
http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...
- 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矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=5015 需要构造一个 n+2 维的矩阵. 就是要增加一维去维护2333这样的序列. 可以发现 2333 = 233 ...
- HDU [P5015] 233 Matrix
矩阵快速幂 按列递推 #include <iostream> #include <cstdio> #include <cstdlib> #include <a ...
- Spring-1-I 233 Matrix(HDU 5015)解题报告及测试数据
233 Matrix Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descript ...
随机推荐
- php的传值和传址
有些情况下,可能希望在函数体内对参数的修改在函数体外也能反映; 使用引用传递参数要在参数前加上&符号; 例子: <?php $a=5; function show(&$a){ ...
- js运动
一.offsetWidth / offsetHeight 获取整个块的宽度/高度,包括border 二.clientWidth / clientHeight 获取块的宽度/高度,不包括border 三 ...
- 用EnumDisplaySettings获取显示设置信息
LPDEVMODE pMode = new DEVMODE;//开空间 DWORD dwBitsPerPel=0;//每象素所使用的显存位数(Bits) DWORD dwPelsWidth=0;//水 ...
- Git版本管理:Windows下Git配置与使用指南 Gitlab
简要介绍:Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理. 一.安装 软件:msysGit-fullinstall-1.8.1.2 打开之后设置安装路径,默认为 ...
- OC3_字典
// // main.m // OC3_字典 // // Created by zhangxueming on 15/6/12. // Copyright (c) 2015年 zhangxueming ...
- oracle创建存储过程并返回结果集(附C#调用代码)
使用存储过程中,最常用的莫过于查询数据表,并返回结果集. 在SQL SERVER 中,这类操作最简单,通过简单的select * from xx 即可完成.但是在Oracle中并不支持这种写法,那么我 ...
- Kafka-0.10.0.0入门
搭建环境略(伪集群即可以),但要注意Kafka的配置必须配置的,少配了也一样可以用,但是只能单机使用,外部机器无法连接,网上也有说. host.name=192.168.1.30 advertised ...
- Ant 入门
参考: Ant官网 http://ant.apache.org/ 轻量级java ee企业应用实战(李刚) Ant当前版本1.9.6 Ant基于Java 配置环境变 ...
- 九度OJ 1385 重建二叉树
题目地址:http://ac.jobdu.com/problem.php?pid=1385 题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都 ...
- 如何将C++中的SOCKADDR_IN*参数类型转换成C#中的参数类型
将C++中的参数类型SOCKADDR_IN*映射为C#中的IntPtr参数类型的示例代码如下: IntPtr ptrSockaddr = new IntPtr(); //ip地址 sockaddr_i ...