ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)
Description
Input
For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).
Output
Sample Input
1
2 2
0 0
3 7
23 47 16
Sample Output
2799
72937
Hint

这个题目由于m数据范围很大,故不能直接暴力计算。此处采用矩阵乘法,由矩阵乘法可以由每一列得到下一列。然后矩阵的乘法使用快速幂加快计算。
由2333可以由233乘10加3,于是打算构造n+2行的方阵。
大致如下:
10 0 0 0 ……0 1
10 1 0 0 ……0 1
10 1 1 0 ……0 1
……
10 1 1 1 ……1 1
0 0 0 0 ……0 1
而所要求的列矩阵大致如下:
23……3
a 1,0
a 2,0
……
a n,0
3
递推的正确性可以通过计算验证
此处矩阵通过结构体,运算符重载完成。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define esp 1e-10
#define N 10000007
#define LL long long using namespace std; struct Mat
{
LL val[15][15];
int len; Mat operator = (const Mat& a)
{
for (int i = 0; i < len; ++i)
for (int j = 0; j < len; ++j)
val[i][j] = a.val[i][j];
len = a.len;
return *this;
} Mat operator * (const Mat& a)
{
Mat x;
memset(x.val, 0, sizeof(x.val));
x.len = len;
for (int i = 0; i < len; ++i)
for (int j = 0; j < len; ++j)
for (int k = 0; k < len; ++k)
if (val[i][k] && a.val[k][j])
x.val[i][j] = (x.val[i][j] + (val[i][k]*a.val[k][j])%N)%N;
return x;
} Mat operator ^ (const int& a)
{
int n = a;
Mat x, p = *this;
memset(x.val, 0, sizeof(x.val));
x.len = len;
for (int i = 0; i < len; ++i)
x.val[i][i] = 1;
while (n)
{
if (n & 1)
x = x * p;
p = p * p;
n >>= 1;
}
return x;
}
}; int n, m;
LL a[15], ans; void Make(Mat &p)
{
p.len = n + 2;
memset(p.val, 0, sizeof(p.val));
for (int i = 0; i <= n; ++i)
p.val[i][0] = 10;
for (int i = 0; i <= n+1; ++i)
p.val[i][n+1] = 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= i; ++j)
p.val[i][j] = 1;
} int main()
{
//freopen("test.txt", "r", stdin);
while (scanf("%d%d", &n, &m) != EOF)
{
Mat p;
Make(p);
p = p ^ m;
a[0] = 23;
a[n+1] = 3;
for (int i = 1; i <= n; ++i)
scanf("%I64d", &a[i]);
ans = 0;
for (int i = 0; i <= n+1; ++i)
ans = (ans + (p.val[n][i]*a[i])%N)%N;
printf("%I64d\n", ans);
}
return 0;
}
ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)的更多相关文章
- HDU5015 233 Matrix —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5015 233 Matrix Time Limit: 10000/5000 MS (Java/Others) Memor ...
- ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)
---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distanc ...
- 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 ...
- 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(矩阵快速幂+思维)
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...
- 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] ...
- ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)
Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...
- ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)
Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...
随机推荐
- URL Handle in Swift (一) -- URL 分解
更新时间: 2018-6-6 在程序开发过程之中, 我们总是希望模块化处理某一类相似的事情. 在 ezbuy 开发中, 我接触到了对于 URL 处理的优秀的代码, 学习.改进.记录下来.希望对你有所帮 ...
- HDU 4930 Fighting the Landlords(扯淡模拟题)
Fighting the Landlords 大意: 斗地主... . 分别给出两把手牌,肯定都合法.每张牌大小顺序是Y (i.e. colored Joker) > X (i.e. Black ...
- caffe编译的问题 找不到opencv的 tiff库文件
解决办法: sudo su cmake .. make -j8 make pycaffe make install 问题解决. 看起来是权限问题导致.
- 02-cookie案例-显示用户上次访问网站的时间
package cookie; import java.io.IOException;import java.io.PrintWriter;import java.util.Date; import ...
- 深入Asyncio(四)Coroutines
Coroutines asyncio在3.4版本添加到Python中,但通过async def和await关键字创建coroutines的语法是3.5才加入的,在这之前,人们把generators当作 ...
- C语言的运算符的优先级与结合性+ASCII表
[0]README 0.1) 内容来源于 C程序设计语言, 旨在整理出C语言的运算符的优先级与结合性, 如下图所示(哥子 记了大半年都没有记住,也是醉了,每次都要去翻): Alert)以下内容转自:h ...
- 模式匹配之surf----特征点检测学习_2(surf算法)
在上篇博客特征点检测学习_1(sift算法) 中简单介绍了经典的sift算法,sift算法比较稳定,检测到的特征点也比较多,其最大的确定是计算复杂度较高.后面有不少学者对其进行了改进,其中比较出名的就 ...
- 微信小程序页面之间的跳转
一.使用标签跳转 index.wxml: 在index.wxml页面添加一个<navigator>元素,在元素里面使用属性url就可以 二. ...
- .net 平台下的AI框架
Aforge.net之旅——开篇:从识别验证码开始 基于AForge.Net框架的扑克牌识别 人工神经网络入门(4) —— AFORGE.NET简介 .NET开源工程推荐(Accord,AForge, ...
- 【BZOJ3435】[Wc2014]紫荆花之恋 替罪点分树+SBT
[BZOJ3435][Wc2014]紫荆花之恋 Description 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从 ...